udp 学习笔记

public class UdpSend {


public static void main(String[] args) throws Exception {
// TODO: Add your code here
DatagramSocket ds=new DatagramSocket();
String strInfo="hello你好";
ds.send(new DatagramPacket("hello你好".getBytes(),"hello你好".getBytes().length,InetAddress.getByName("127.0.0.1"),3000));
ds.close();
}

}


public class UdpRecv {

 
public static void main(String[] args) throws Exception {
// TODO: Add your code here
 
DatagramSocket  ds=new DatagramSocket(3000);
byte[] buf=new byte[1024];
DatagramPacket dp=new DatagramPacket(buf,1024);
ds.receive(dp);
System.out.println(new String(dp.getData(),0,dp.getLength())+"from"+
dp.getAddress().getHostAddress()+":"+dp.getPort());
ds.close();
}
}

posted @ 2012-09-08 18:04  retacn_yue  阅读(135)  评论(0编辑  收藏  举报