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 閱讀(308) 評論(0)  編輯  收藏 所屬分類: JAVA計算機綜合

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 916114
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 盐亭县| 大港区| 合肥市| 徐州市| 渭南市| 江油市| 林周县| 仙游县| 英山县| 民县| 楚雄市| 平山县| 南京市| 多伦县| 花莲市| 库尔勒市| 陇西县| 白朗县| 商河县| 晋中市| 威信县| 阜宁县| 府谷县| 光山县| 汾阳市| 屏山县| 松溪县| 乌鲁木齐县| 连云港市| 乳源| 启东市| 黄冈市| 南木林县| 伊春市| 衡阳县| 平乐县| 深圳市| 察雅县| 双柏县| 高要市| 乐安县|