一般說明
在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實例.
<!-- ======== NESTED CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><!-- ======== CONSTRUCTOR SUMMARY ======== --> <!-- -->
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
(intport)
Constructs a datagram socket and binds it to the specified port on the local host machine. |
|
DatagramSocket
(intport,
InetAddress
laddr)
Creates a datagram socket, bound to the specified local address. |
|
DatagramSocket
(
SocketAddress
bindaddr)
Creates a datagram socket, bound to the specified local socket address. |
其中,port指明socket所使用的端口號,如果未指明端口號,則把socket連接到本地主機上一個可用的端口。laddr指明一個可用的本地地址。給出端口號時要保證不發生端口沖突,否則會生成SocketException類例外。
用數據報方式編寫通信程序時,通信雙方,首先都要建立一個DatagramSocket對象,用來接收或發送數據報,然后使用DatagramPacket類對象作為傳輸數據的載體。下面看一下DatagramPacket的構造方法 :
Constructor Summary |
DatagramPacket
(byte[]buf, intlength)
Constructs a
DatagramPacket
for receiving packets of length
length
.
|
DatagramPacket
(byte[]buf, intlength,
InetAddress
address, intport)
Constructs a datagram packet for sending packets of length
length
to the specified port number on the specified host.
|
DatagramPacket
(byte[]buf, intoffset, intlength)
Constructs a
DatagramPacket
for receiving packets of length
length
, specifying an offset into the buffer.
|
DatagramPacket
(byte[]buf, intoffset, intlength,
InetAddress
address, intport)
Constructs a datagram packet for sending packets of length
length
with offset
ioffset
to the specified port number on the specified host.
|
DatagramPacket
(byte[]buf, intoffset, intlength,
SocketAddress
address)
Constructs a datagram packet for sending packets of length
length
with offset
ioffset
to the specified port number on the specified host.
|
DatagramPacket
(byte[]buf, intlength,
SocketAddress
address)
Constructs a datagram packet for sending packets of length
length
to the specified port number on the specified host.
|
可以看出,有兩個供接收的構造器和四個供發送的構造器.其中,buf中存放數據報數據,length為數據報中數據的長度,address和port旨明目的地址,offset指明了數據報的位移量。
Java組播支持
MulticastSocket 多播數據報套接字。這個組播套接字對于收發IP多播數據包是很有用的,它擴展了DatagramSocket,在其上附加了加入internet上多播組的方法。一個多播組由D類IP地址和標準UDP端口指定,D類IP范圍是224.0.0.0 to
239.255.255.255,其中224.0.0.0被保留不為它用。
它有三個構造器:
Constructor Summary |
MulticastSocket
()
Create a multicast socket. |
MulticastSocket
(intport)
Create a multicast socket and bind it to a specific port. |
MulticastSocket
(
SocketAddress
bindaddr)
Create a MulticastSocket bound to the specified socket address. |
基本上,沒有指定端口,只為發送,指定端口可收發,多址主機會用套接字地址。
一,DatagramSocket類;DatagramPacket類;InetAddress類
構造函數 public DatagramSocket();public DatagramSocket(int port);
public DatagramSocket(InetAddress laddr);
close()方法
send(DatagramPacket p)
receive(DatagramPacket p)
接受方的 DatagramPacket public DatagramPacket (byte[]buf, intlength)發送方的 DatagramPacket public DatagramPacket (byte[]buf, intlength, InetAddressaddress, intport
最簡單的UDP程序:
發送程序:UdpSend.java














接收程序:UdpRecv.java















更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
