1 import java.net.*;
 2 
 3 public class UdpRecv
 4 {
 5     public static void main(String args[]) throws Exception
 6     {
 7         DatagramSocket ds = new DatagramSocket(3000);
 8         byte [] buf = new byte[1024];
 9         DatagramPacket dp = new DatagramPacket(buf,1024);
10         ds.receive(dp);
11         String strRecv = new String (dp.getData(),0,dp.getLength(),"gb2312")+" from "+dp.getAddress().getHostAddress() + ":"+dp.getPort();
12         System.out.println(strRecv);
13         ds.close();
14     }
15     
16 }

 1 import java.net.*;
 2 
 3 public class UdpSend
 4 {
 5     public static void main(String args[])throws Exception
 6     {
 7         DatagramSocket ds = new DatagramSocket();
 8         String str = "hello ,this is a test";
 9         DatagramPacket dp = new DatagramPacket (str.getBytes(),str.length(),InetAddress.getByName("127.0.0.1"),3000);
10         ds.send(dp);
11         ds.close();
12     }
13 
14 }

posted on 2007-02-08 13:40 -274°C 阅读(292) 评论(0)  编辑  收藏 所属分类: JAVA计算机综合

只有注册用户登录后才能发表评论。


网站导航:
 

常用链接

留言簿(21)

随笔分类(265)

随笔档案(242)

相册

JAVA网站

关注的Blog

搜索

  •  

积分与排名

  • 积分 - 908974
  • 排名 - 40

最新评论