标签:rgs import imp 协议 pre method udp协议 blog local
UDP发送端:
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPSend { /** * @param args */ public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub byte[] data = "你好UDP".getBytes(); DatagramPacket dp = new DatagramPacket(data, data.length,InetAddress.getLocalHost(),8000); // DatagramPacket dp = new DatagramPacket DatagramSocket ds= new DatagramSocket(); ds.send(dp); ds.close(); } }
UDP接收端:
public class UDPReceive { /** * @param args */ public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub DatagramSocket ds = new DatagramSocket(8000); byte[] data = new byte[1024]; DatagramPacket dp = new DatagramPacket(data, data.length); ds.receive(dp); String add = dp.getAddress().getHostAddress(); int port = dp.getPort(); int len = dp.getLength(); ds.close(); System.out.println(new String(data,0,len)+"...."+add+":"+port); } }
标签:rgs import imp 协议 pre method udp协议 blog local
原文地址:http://www.cnblogs.com/mumunuo/p/7214525.html