标签:java 网络编程 datagramsocket datagrampacket 通信
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import org.junit.Test; public class TestUDP { @Test public void send() throws Exception{ String str = "Hello"; DatagramPacket pack = new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getByName("127.0.0.1"),10000); DatagramSocket scoket = new DatagramSocket(); for(int i =0; i< 10;i++){ scoket.send(pack); } scoket.close(); } @Test public void revice() throws Exception{ byte[] by = new byte[1024]; DatagramPacket pack = new DatagramPacket(by,by.length); DatagramSocket scoket = new DatagramSocket(10000); while(true){ scoket.receive(pack); System.out.println(new String(pack.getData())); } } }
java之UDP(datagramsocket,datagramPacket)实例
标签:java 网络编程 datagramsocket datagrampacket 通信
原文地址:http://blog.csdn.net/sd_tz_wzg/article/details/40536711