Mercurial > hg > Members > nobuyasu > tightVNCProxy
comparison src/myVncProxy/GetBroadCast.java @ 189:545183e14d4e
add GetBroadCast.java
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 22 Nov 2011 13:58:58 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
188:d69a89f6d14a | 189:545183e14d4e |
---|---|
1 package myVncProxy; | |
2 | |
3 import java.io.ByteArrayInputStream; | |
4 import java.io.DataInputStream; | |
5 import java.io.IOException; | |
6 import java.net.DatagramPacket; | |
7 import java.net.InetAddress; | |
8 import java.net.MulticastSocket; | |
9 | |
10 import myVncProxy.TextBox; | |
11 | |
12 public class GetBroadCast 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 BroadCast bCast; | |
22 | |
23 public GetBroadCast(VncProxyService _vps){ | |
24 vps = _vps; | |
25 bCast = new BroadCast(vps.rfb.acceptPort+"*"+vps.host+"*" | |
26 +vps.rfb.desktopName+"*"+vps.acc.getMyAddress()+"*"); | |
27 } | |
28 | |
29 private void getData() { | |
30 try { | |
31 InetAddress mAddr = InetAddress.getByName(McastAddr); | |
32 MulticastSocket soc = new MulticastSocket(Port); | |
33 DatagramPacket recvPacket = new DatagramPacket(buf, BufSize); | |
34 soc.joinGroup(mAddr); | |
35 bCast.createSocket(); | |
36 while (true) { | |
37 soc.receive(recvPacket); | |
38 inputStream = new ByteArrayInputStream(recvPacket.getData()); | |
39 inputStream.read(resorve); | |
40 if("who".equals(castString(resorve))){ | |
41 for(int i=0;i<4;i++){ | |
42 bCast.sendData(); | |
43 } | |
44 } | |
45 } | |
46 } catch (IOException e) { | |
47 e.printStackTrace(); | |
48 } | |
49 } | |
50 | |
51 private int castInt(byte[] a) { | |
52 int value = 0; | |
53 ByteArrayInputStream bais = new ByteArrayInputStream(a); | |
54 DataInputStream dis = new DataInputStream(bais); | |
55 try { | |
56 value = dis.readInt(); | |
57 } catch (IOException e) { | |
58 } | |
59 System.out.println(value); | |
60 return value; | |
61 } | |
62 | |
63 private String castString(byte[] a) { | |
64 String recover = new String(a); | |
65 recover = recover.replace("¥n", ""); | |
66 recover = recover.trim(); | |
67 System.out.println(recover); | |
68 return recover; | |
69 } | |
70 | |
71 | |
72 @Override | |
73 public void run() { | |
74 getData(); | |
75 } | |
76 | |
77 public void setStopFlag(boolean stopFlag) { | |
78 this.stopFlag = stopFlag; | |
79 } | |
80 | |
81 public boolean isStopFlag() { | |
82 return stopFlag; | |
83 } | |
84 | |
85 } |