Java study 1:The note of studying Socket which based UDP

來源:互聯網
上載者:User

標籤:

  1. UDP concept:

      UDP 是User Datagram Protocol的簡稱, 中文名是使用者資料包通訊協定,是OSI(Open System Interconnection,開放式系統互聯) 參考模型中一種無串連的傳輸層協議,提供面向事務的簡單不可靠資訊傳送服務,IETF RFC 768是UDP的正式規範。UDP在IP報文的協議號是17。(from baike)

  2. UDP program:

      因爲UDP協議是一種無連接的協議,所以

        (1)、每次發送數據並不需要綁定,只需要使用DatagramPacket()構造好對應的數據包就可以了;

        (2)、沒有像TCP一樣的ServerSocket 與Socket之分,雙方都用DatagramPacket初始化;

         (3)、發送接收用send(數據包),receive(數據包)

  下面上代碼:

  *UDP並無Server與Client之分,但是代碼一個發送一個接受就起了這名字(這並不重要~逃)

  *發送成功之後雙方就建立了一個鏈接(虛),可以互發數據

 1 import java.io.IOException; 2 import java.net.DatagramPacket; 3 import java.net.DatagramSocket; 4 import java.net.SocketException; 5  6 public class UdpServer { 7     public static void main(String[] args) { 8         DatagramSocket dgs = null; 9         try {10             dgs=new DatagramSocket(8888);11             //receive12             byte[] buf=new byte[256];13             int length=256;14             DatagramPacket dgp=new DatagramPacket(buf, length);15             dgs.receive(dgp);16             System.out.println("receive from client:"+new String(buf, 0, length));17             18             //send19             String str="yeah i have receivered~";20             System.out.println(dgp.getAddress());//use last time‘s packet to get the address 21             System.out.println(dgp.getPort());22             DatagramPacket dgp_send=new DatagramPacket(str.getBytes(),str.length(), dgp.getAddress(), dgp.getPort());23             dgs.send(dgp_send);24             dgs.close();25         } catch (SocketException e) {26             e.printStackTrace();27         } catch (IOException e) {28             e.printStackTrace();29         }finally{30             dgs.close();31         }32     }33 }
UdpServer
 1 import java.net.DatagramPacket; 2 import java.net.DatagramSocket; 3 import java.net.InetAddress; 4  5 public class UdpClient { 6     public static void main(String[] args) { 7         DatagramSocket dgs = null; 8         try { 9             dgs=new DatagramSocket();10             //send->server11             String str="so dirty haha~";12             DatagramPacket dgp_send=new DatagramPacket(str.getBytes(),0,str.length(),InetAddress.getByName("localhost"),8888);13             dgs.send(dgp_send);14             //receive from server15             byte[] buf=new byte[256];16             DatagramPacket dgp2_receive=new DatagramPacket(buf, 256);17             dgs.receive(dgp2_receive);18             System.out.println("receive from server:"+new String(buf,0,256));19             dgs.close();20             21             22         } catch (Exception e) {23             System.out.println("error:"+e);24         }25     }26 }
UdpClient


p.s.I:Server 的 send 部分使用之前用來接收數據的包dgp來取得發送者的address以及port,然後再用新的DatagramPacket來發送數據

p.s.II:DatagramPacket的搆造函數中,byte[]用來放數據,length用來放長度,程式簡單所以數字亂寫的(ha

  

Java study 1:The note of studying Socket which based UDP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.