标签:
class UdpRecevie { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(10000); while(true) { byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(but, but.length); ds.receive(dp);//阻塞式方法,没数据就等 String ip = dp.getAddress().getHostAdress(); String data = new String(dp.getData(), 0, dp.getLength()); int port = dp.getPort(); } ds.close(); } }
class UdpSend { public static void main(String[] args) throws Exception { DatagramSocket ds = new DatagramSocket(); byte[] data = "udp come in".getbytes(); DatagramPacket dp = new DatagramPacket(data, data.length, InetAddresss.getByName("192.168.1.255"), 1000); ds.send(dp); ds.close(); } }
标签:
原文地址:http://www.cnblogs.com/xxr2015/p/4621230.html