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 阅读(297)
评论(0) 编辑 收藏 所属分类:
JAVA 、
计算机综合