UDP Socket

来源:互联网 发布:c语言调试器 编辑:程序博客网 时间:2024/06/05 11:09
 package TestUDP;import java.net.*;import java.io.*;public class Server {public static void main(String[] args) throws Exception {byte[] buf=new  byte[1024];DatagramPacket dp=new DatagramPacket(buf,buf.length);DatagramSocket ds=new DatagramSocket(5678);while(true){ds.receive(dp);System.out.println(new String(buf,0,dp.getLength()));}}}
package TestUDP;import java.net.*;import java.io.*;public class Client {public static void main(String[] args) throws Exception{byte[] buf="hello".getBytes();DatagramPacket dp=new DatagramPacket(buf,buf.length,new InetSocketAddress("127.0.0.1",5678));DatagramSocket ds=new DatagramSocket(5679);ds.send(dp);ds.close();}}


 

 

原创粉丝点击