0
|
1 package treeVnc;
|
|
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;
|
|
9
|
|
10 //import TextBoxProxy;
|
|
11
|
|
12 public class GetBroadCastProxy implements Runnable {
|
|
13 static final String McastAddr = "224.0.0.1";
|
|
14 static final int Port = 8183;
|
|
15 static final int BufSize = 1024;
|
|
16 private byte[] buf = new byte[BufSize];
|
|
17 private byte[] resorve = new byte[BufSize];
|
|
18 private ByteArrayInputStream inputStream;
|
|
19 private boolean stopFlag = false;
|
|
20 private VncProxyService vps;
|
|
21 private BroadCastProxy bCast;
|
|
22 private String address;
|
|
23
|
|
24 public GetBroadCastProxy(VncProxyService _vps){
|
|
25 vps = _vps;
|
|
26 bCast = new BroadCastProxy(vps.rfb.acceptPort+":"+vps.host+":"
|
|
27 +vps.rfb.desktopName+":"+vps.acc.getMyAddress()+":");
|
|
28 }
|
|
29
|
|
30 private synchronized void getData() {
|
|
31 try {
|
|
32 InetAddress mAddr = InetAddress.getByName(McastAddr);
|
|
33 MulticastSocket soc = new MulticastSocket(Port);
|
|
34 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize);
|
|
35 soc.joinGroup(mAddr);
|
|
36 while (true) {
|
|
37 soc.receive(recvPacket);
|
|
38 address = getAddress(recvPacket.getSocketAddress());
|
|
39 inputStream = new ByteArrayInputStream(recvPacket.getData());
|
|
40 inputStream.read(resorve);
|
|
41 if("who".equals(castString(resorve))){
|
|
42 replyBroadCast();
|
|
43 }
|
|
44 }
|
|
45 } catch (IOException e) {
|
|
46 e.printStackTrace();
|
|
47 }
|
|
48 }
|
|
49
|
|
50 private void replyBroadCast() {
|
|
51 Runnable sender = new Runnable() {
|
|
52 public void run() {
|
|
53 bCast.createSocket(address);
|
|
54 }
|
|
55 };
|
|
56 new Thread(sender).start();
|
|
57 }
|
|
58
|
|
59 private String getAddress(SocketAddress addr) {
|
|
60 String str = addr.toString();
|
|
61 str = str.substring(1,str.indexOf(":"));
|
|
62 return str;
|
|
63 }
|
|
64 /*
|
|
65 private int castBytoToInt(byte[] a) {
|
|
66 int value = 0;
|
|
67 ByteArrayInputStream bais = new ByteArrayInputStream(a);
|
|
68 DataInputStream dis = new DataInputStream(bais);
|
|
69 try {
|
|
70 value = dis.readInt();
|
|
71 } catch (IOException e) {
|
|
72 }
|
|
73 System.out.println(value);
|
|
74 return value;
|
|
75 }
|
|
76 */
|
|
77 private String castString(byte[] a) {
|
|
78 String recover = new String(a);
|
|
79 recover = recover.replace("¥n", "");
|
|
80 recover = recover.trim();
|
|
81 System.out.println(recover);
|
|
82 return recover;
|
|
83 }
|
|
84
|
|
85
|
|
86 public void run() {
|
|
87 getData();
|
|
88 }
|
|
89
|
|
90 public void setStopFlag(boolean stopFlag) {
|
|
91 this.stopFlag = stopFlag;
|
|
92 }
|
|
93
|
|
94 public boolean isStopFlag() {
|
|
95 return stopFlag;
|
|
96 }
|
|
97
|
|
98 }
|