Mercurial > hg > Members > you > BroadCastTreeVNC
view src/treeVnc/GetBroadCastProxy.java @ 5:970d5ac80256
add MyDataInputStream.java and DataInputStream1.java
author | one |
---|---|
date | Mon, 23 Apr 2012 21:10:23 +0900 |
parents | 756bfaf731f3 |
children |
line wrap: on
line source
package treeVnc; 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; } }