Mercurial > hg > Members > riono > TreeVNC_ja_comment
annotate src/main/java/jp/ac/u_ryukyu/treevnc/server/GetBroadCastProxy.java @ 69:21108e863b8a
update client list.
author | one |
---|---|
date | Tue, 11 Mar 2014 15:29:52 +0900 |
parents | e6550afe663b |
children | fc984746f958 |
rev | line source |
---|---|
12 | 1 package jp.ac.u_ryukyu.treevnc.server; |
11 | 2 |
3 import java.io.ByteArrayInputStream; | |
4 import java.io.IOException; | |
5 import java.net.DatagramPacket; | |
6 import java.net.InetAddress; | |
7 import java.net.MulticastSocket; | |
8 import java.net.SocketAddress; | |
13 | 9 import java.net.UnknownHostException; |
11 | 10 |
11 //import TextBoxProxy; | |
12 | |
13 public class GetBroadCastProxy implements Runnable { | |
14 static final String McastAddr = "224.0.0.1"; | |
15 static final int Port = 8183; | |
16 static final int BufSize = 1024; | |
17 private ByteArrayInputStream inputStream; | |
18 private boolean stopFlag = false; | |
19 private VncProxyService vps; | |
20 private BroadCastProxy bCast; | |
21 private String address; | |
68 | 22 private String str = "who"; |
69 | 23 private MulticastSocket soc; |
11 | 24 |
13 | 25 public GetBroadCastProxy(VncProxyService _vps,String desktopName,String host){ |
11 | 26 vps = _vps; |
61
d9cf08c6415c
During implementation change screen.
Taninari YU <you@cr.ie.u-ryukyu.ac.jp>
parents:
60
diff
changeset
|
27 bCast = new BroadCastProxy(vps.getRfb().getAcceptPort()+":"+host+":" |
13 | 28 +desktopName+":"+getMyAddress()+":"); |
11 | 29 } |
30 | |
31 private synchronized void getData() { | |
55 | 32 byte[] buf = new byte[BufSize]; |
33 byte[] resorve = new byte[BufSize]; | |
11 | 34 try { |
35 InetAddress mAddr = InetAddress.getByName(McastAddr); | |
69 | 36 soc = new MulticastSocket(Port); |
11 | 37 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize); |
38 soc.joinGroup(mAddr); | |
55 | 39 while (!stopFlag) { |
11 | 40 soc.receive(recvPacket); |
41 address = getAddress(recvPacket.getSocketAddress()); | |
42 inputStream = new ByteArrayInputStream(recvPacket.getData()); | |
43 inputStream.read(resorve); | |
68 | 44 if(str.equals(castString(resorve))){ |
11 | 45 replyBroadCast(); |
46 } | |
38 | 47 if(stopFlag) break; |
11 | 48 } |
49 } catch (IOException e) { | |
50 e.printStackTrace(); | |
51 } | |
52 } | |
53 | |
54 private void replyBroadCast() { | |
55 Runnable sender = new Runnable() { | |
56 public void run() { | |
57 bCast.createSocket(address); | |
58 } | |
59 }; | |
60 new Thread(sender).start(); | |
61 } | |
62 | |
63 private String getAddress(SocketAddress addr) { | |
64 String str = addr.toString(); | |
65 str = str.substring(1,str.indexOf(":")); | |
66 return str; | |
67 } | |
55 | 68 |
11 | 69 private String castString(byte[] a) { |
70 String recover = new String(a); | |
60 | 71 recover = recover.replace("������n", ""); |
11 | 72 recover = recover.trim(); |
73 return recover; | |
74 } | |
75 | |
76 | |
77 public void run() { | |
78 getData(); | |
79 } | |
80 | |
81 public void setStopFlag(boolean stopFlag) { | |
82 this.stopFlag = stopFlag; | |
83 } | |
84 | |
85 public boolean isStopFlag() { | |
86 return stopFlag; | |
87 } | |
13 | 88 |
89 String getMyAddress () { | |
90 InetAddress addr = null; | |
91 try { | |
92 addr = InetAddress.getLocalHost(); | |
93 } catch (UnknownHostException e) { | |
94 e.printStackTrace(); | |
95 } | |
96 return new String(addr.getHostAddress()); | |
97 } | |
11 | 98 } |