catch (Exception e)
{
throw new RuntimeException("发送端建立失败");
}
}
}
class Receive implements Runnable
{
private DatagramSocket ds;
public static boolean flag = true;
public Receive(DatagramSocket ds)
{
this.ds = ds;
}
public void run()
{
try
{
while(flag)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String (dp.getData(),0,dp.getLength());
System.out.println(ip+"::"+data);
}
}
catch (Exception e)
{
throw new RuntimeException("接收端创建失败");
}
}
}
public class ChartDemo
{
public static void main(String[] args) throws Exception
{
DatagramSocket sendSocket = new DatagramSocket();
DatagramSocket receiveSocket = new DatagramSocket(10003);
new Thread(new Send(sendSocket)).start();
new Thread(new Receive(receiveSocket)).start();