Java資料報編程之概說

來源:互聯網
上載者:User

一般說明
在TCP/IP協議族中,UDP和TCP同樣位於傳輸層,使用者資料報是UDP協議中的概念.
UDP協議提供面向事務的簡單不可靠資訊傳送服務,它不提供對 IP 協議的可靠機制、流量控制以及錯誤恢複功能.
UDP 協議基本上是IP 協議與上層協議的介面,從整個使用者資料在各層的封裝看,UDP報文格式相當簡單:
16
32bit
Source port源連接埠
Destination port目標連接埠
Length 報文長度(單位是位元組,包括首部和使用者資料區)
Checksum(校正和)
Data
由於校正和的原因,UDP還引入了偽首部,這導致了UDP和IP層的關係過於密切,破壞了分層原則.
Java資料報支援
包java.net中提供了兩個類DatagramSocket和DatagramPacket用來支援資料報通訊,DatagramSocket用於在程式之間建立傳送資料報的通訊串連, DatagramPacket則用來表示一個資料報。
DatagramSocket代表發送和接收資料報的通訊端,一個資料通訊端是為包遞送服務的發送和接收點,在一個資料通訊端上,每個被發送和接收的包都被獨立的定址和路由,從一台機器到另一台機器上發送的多個包有不同的路由,任意的抵達順序.
對於DatagramSocket,UDP廣播發送總是使能的(那是預設設定).為了接收廣播包這個類執行個體應該綁定到通用地址(wildcard address).在某些實現中,當被綁定到更多特定地址上的時候廣播包也可以接收.
例如:
DatagramSocket s = new DatagramSocket(null);
s.bind(new InetSocketAddress(8888));
這等同於:
DatagramSocket s = new DatagramSocket(8888);
兩種情況都會建立一個能在連接埠8888上接收廣播的DatagramSocket執行個體.
Constructor Summary
DatagramSocket()
          Constructs a datagram socket and binds it to any available port on the local host machine.
protected
DatagramSocket(DatagramSocketImpl impl)
          Creates an unbound datagram socket with the specified DatagramSocketImpl.
DatagramSocket(int port)
          Constructs a datagram socket and binds it to the specified port on the local host machine.
DatagramSocket(int port, InetAddress laddr)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.