杰點

          hello java

          UDP通信

          DatagramSocket類   用于創建收發UDP數據包的Socket對象

          構造函數
          DatagramSocket() //用于發送方,系統分配端口號
          DatagramSocket(int port) //用于接收方
          DatagramSocket(int port,InetAddress laddr)
          //udp在多個Ip地址的計算機,用于發送接收

          方法
          close() //關閉對象,釋放資源

          send(DatagramPacket p)  //發送數據包

          receive(DatagramPacket p) //接收數據包

           

          DatagramPacket類  創建UDP數據包

          構造函數
          public DatagramPacket(byte[] buf,int length)
          //內存緩存區,大小    接收數據對象

          public Datagrampacket(byte[] buf,int length,
          InetAddress address,int port)
          //內存緩存區,大小,ip,端口   發送數據對象

          getInetAddress
          getPort

          getData   返回buf
          getLength 返回length

           

          InetAddress類

          getByName() 靜態方法
          //根據字符串格式,返回一個相應的實例對象
          getHostAddress()
          //返回.分格的IP

           

           1 //創建基于的UDP發送方
           2 import java.net.*;
           3 public class UdpSend {
           4 //發送方
           5     /**
           6      * @param args
           7      */
           8     public static void main(String[] args) throws Exception{
           9         // TODO Auto-generated method stub
          10         DatagramSocket ds = new DatagramSocket();//創建socket發送對象,發送端口號由系統分配。
          11         String strInfo="hello china 中國";//定義要發送的字符串
          12         DatagramPacket dp=new DatagramPacket(strInfo.getBytes(),strInfo.getBytes().length,
          13                 InetAddress.getByName("192.168.1.111"),3333); //構建數據包
          14         ds.send(dp);//發送數據包
          15         ds.close();
          16 
          17     }
          18 
          19 }
          20 

           1 //基于UDP協議的接收方
           2 import java.net.*;
           3 public class UdpRev {
           4     
           5     public static void main(String[] args) throws Exception{
           6         //創建socket接收對象
           7         byte[] buff=new byte[1024];
           8         DatagramSocket ds= new DatagramSocket(3333);
           9         DatagramPacket p= new DatagramPacket(buff,1024);
          10         ds.receive(p);
          11         System.out.println("content:\t"+new String(buff,0,p.getLength())
          12         +"\nfrom\t"+p.getAddress().getHostAddress()+":"+p.getPort());
          13         ds.close();
          14     }
          15 
          16 }

          posted on 2010-12-29 12:30 杰點 閱讀(140) 評論(0)  編輯  收藏 所屬分類: JAVA


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          留言簿

          文章分類

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 普陀区| 古田县| 都江堰市| 浦城县| 古交市| 汝城县| 乌鲁木齐县| 长汀县| 茂名市| 平塘县| 崇信县| 财经| 正定县| 绥化市| 新巴尔虎右旗| 临沂市| 永新县| 乡宁县| 洛浦县| 阳东县| 灯塔市| 额尔古纳市| 杭锦旗| 安乡县| 阜宁县| 平武县| 繁昌县| 昌吉市| 乐清市| 白水县| 芦溪县| 元谋县| 井陉县| 长兴县| 韶山市| 宜州市| 南雄市| 天柱县| 宣威市| 邛崃市| 华蓥市|