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

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網(wǎng)站

          關(guān)注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914066
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 仙游县| 金乡县| 大宁县| 旬邑县| 拉萨市| 巴马| 南丰县| 山丹县| 阜南县| 中江县| 富民县| 财经| 玉环县| 延寿县| 阜南县| 兰溪市| 福安市| 德昌县| 花垣县| 麻江县| 光山县| 南安市| 巴林右旗| 荣成市| 景德镇市| 松滋市| 宁国市| 庆安县| 龙陵县| 封丘县| 滦平县| 兴业县| 泗阳县| 潮州市| 新晃| 十堰市| 开江县| 教育| 金寨县| 靖西县| 安康市|