Mercurial > hg > Members > nobuyasu > tightVNCClient
changeset 108:e66137b514eb Version-1.0
add BroadCast.java
author | Yu Taninari <you@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 22 Nov 2011 14:02:49 +0900 |
parents | 40efe6c25ff6 |
children | 13b430dae64a |
files | src/myVncClient/BroadCast.java |
diffstat | 1 files changed, 57 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/myVncClient/BroadCast.java Tue Nov 22 14:02:49 2011 +0900 @@ -0,0 +1,57 @@ +package myVncClient; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.InetAddress; +import java.net.MulticastSocket; +import java.net.UnknownHostException; + +public class BroadCast implements Runnable { + final int BUFSIZE = 1024; + final String MCASTADDR = "224.0.0.1"; + final int PORT = 8183; + private byte[] buf = new byte[BUFSIZE]; + private InetAddress mAddr; + private MulticastSocket soc; + private String str; + + public BroadCast(String _str) { + str = _str; + } + + void createSocket() { + try { + mAddr = InetAddress.getByName(MCASTADDR); + soc = new MulticastSocket(); + soc.setTimeToLive(1); + } catch (IOException e) { + e.printStackTrace(); + } + } + + void sendData() { + buf = str.getBytes(); + // System.out.println("strlen"+str.length()); + // System.out.println("str"+str); + DatagramPacket sendPacket = new DatagramPacket(buf, str.length(), + mAddr, PORT); + try { + soc.send(sendPacket); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void run() { + createSocket(); + while (true) { + sendData(); + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + System.out.println(e); + } + } + } +}