Mercurial > hg > Applications > TightVNC_orginal
diff src/main/java/jp/ac/u_ryukyu/treevnc/server/GetBroadCastProxy.java @ 12:12c3a73be47f
rename package
author | one |
---|---|
date | Tue, 21 Aug 2012 14:24:38 +0900 |
parents | src/main/java/ac/ryukyu/treevnc/server/GetBroadCastProxy.java@57ae9fbb1245 |
children | 406fa09ae645 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/treevnc/server/GetBroadCastProxy.java Tue Aug 21 14:24:38 2012 +0900 @@ -0,0 +1,98 @@ +package jp.ac.u_ryukyu.treevnc.server; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.net.SocketAddress; + +//import TextBoxProxy; + +public class GetBroadCastProxy implements Runnable { + static final String McastAddr = "224.0.0.1"; + static final int Port = 8183; + static final int BufSize = 1024; + private byte[] buf = new byte[BufSize]; + private byte[] resorve = new byte[BufSize]; + private ByteArrayInputStream inputStream; + private boolean stopFlag = false; + private VncProxyService vps; + private BroadCastProxy bCast; + private String address; + + public GetBroadCastProxy(VncProxyService _vps){ + vps = _vps; + bCast = new BroadCastProxy(vps.rfb.acceptPort+":"+vps.host+":" + +vps.rfb.desktopName+":"+vps.acc.getMyAddress()+":"); + } + + private synchronized void getData() { + try { + InetAddress mAddr = InetAddress.getByName(McastAddr); + MulticastSocket soc = new MulticastSocket(Port); + DatagramPacket recvPacket = new DatagramPacket(buf, BufSize); + soc.joinGroup(mAddr); + while (true) { + soc.receive(recvPacket); + address = getAddress(recvPacket.getSocketAddress()); + inputStream = new ByteArrayInputStream(recvPacket.getData()); + inputStream.read(resorve); + if("who".equals(castString(resorve))){ + replyBroadCast(); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void replyBroadCast() { + Runnable sender = new Runnable() { + public void run() { + bCast.createSocket(address); + } + }; + new Thread(sender).start(); + } + + private String getAddress(SocketAddress addr) { + String str = addr.toString(); + str = str.substring(1,str.indexOf(":")); + return str; + } +/* + private int castBytoToInt(byte[] a) { + int value = 0; + ByteArrayInputStream bais = new ByteArrayInputStream(a); + DataInputStream dis = new DataInputStream(bais); + try { + value = dis.readInt(); + } catch (IOException e) { + } + System.out.println(value); + return value; + } +*/ + private String castString(byte[] a) { + String recover = new String(a); + recover = recover.replace("¥n", ""); + recover = recover.trim(); +// System.out.println(recover); + return recover; + } + + + public void run() { + getData(); + } + + public void setStopFlag(boolean stopFlag) { + this.stopFlag = stopFlag; + } + + public boolean isStopFlag() { + return stopFlag; + } + +}