一般說明
在TCP/IP協(xié)議族中,UDP和TCP同樣位于傳輸層,用戶數(shù)據(jù)報(bào)是UDP協(xié)議中的概念.
UDP協(xié)議提供面向事務(wù)的簡單不可靠信息傳送服務(wù),它不提供對(duì) IP 協(xié)議的可靠機(jī)制、流控制以及錯(cuò)誤恢復(fù)功能.
UDP 協(xié)議基本上是IP 協(xié)議與上層協(xié)議的接口,從整個(gè)用戶數(shù)據(jù)在各層的包裝看,UDP報(bào)文格式相當(dāng)簡單:
16 | 32bit |
Source port源端口 | Destination port目標(biāo)端口 |
Length 報(bào)文長度(單位是字節(jié),包括首部和用戶數(shù)據(jù)區(qū)) | Checksum(校驗(yàn)和) |
Data |
由于校驗(yàn)和的原因,UDP還引入了偽首部,這導(dǎo)致了UDP和IP層的關(guān)系過于密切,破壞了分層原則.
Java數(shù)據(jù)報(bào)支持
包java.net中提供了兩個(gè)類DatagramSocket和DatagramPacket用來支持?jǐn)?shù)據(jù)報(bào)通信,DatagramSocket用于在程序之間建立傳送數(shù)據(jù)報(bào)的通信連接, DatagramPacket則用來表示一個(gè)數(shù)據(jù)報(bào)。
DatagramSocket代表發(fā)送和接收數(shù)據(jù)報(bào)的套接字,一個(gè)數(shù)據(jù)報(bào)套接字是為包遞送服務(wù)的發(fā)送和接收點(diǎn),在一個(gè)數(shù)據(jù)報(bào)套接字上,每個(gè)被發(fā)送和接收的包都被獨(dú)立的尋址和路由,從一臺(tái)機(jī)器到另一臺(tái)機(jī)器上發(fā)送的多個(gè)包有不同的路由,任意的抵達(dá)順序.
對(duì)于DatagramSocket,UDP廣播發(fā)送總是使能的(那是缺省設(shè)置).為了接收廣播包這個(gè)類實(shí)例應(yīng)該綁定到通用地址(wildcard address).在某些實(shí)現(xiàn)中,當(dāng)被綁定到更多特定地址上的時(shí)候廣播包也可以接收.
例如:
DatagramSocket s = new DatagramSocket(null);
s.bind(new InetSocketAddress(8888));
這等同于:
DatagramSocket s = new DatagramSocket(8888);
兩種情況都會(huì)創(chuàng)建一個(gè)能在端口8888上接收廣播的DatagramSocket實(shí)例.
<!-- ======== 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所使用的端口號(hào),如果未指明端口號(hào),則把socket連接到本地主機(jī)上一個(gè)可用的端口。laddr指明一個(gè)可用的本地地址。給出端口號(hào)時(shí)要保證不發(fā)生端口沖突,否則會(huì)生成SocketException類例外。
用數(shù)據(jù)報(bào)方式編寫通信程序時(shí),通信雙方,首先都要建立一個(gè)DatagramSocket對(duì)象,用來接收或發(fā)送數(shù)據(jù)報(bào),然后使用DatagramPacket類對(duì)象作為傳輸數(shù)據(jù)的載體。下面看一下DatagramPacket的構(gòu)造方法 :
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.
|
可以看出,有兩個(gè)供接收的構(gòu)造器和四個(gè)供發(fā)送的構(gòu)造器.其中,buf中存放數(shù)據(jù)報(bào)數(shù)據(jù),length為數(shù)據(jù)報(bào)中數(shù)據(jù)的長度,address和port旨明目的地址,offset指明了數(shù)據(jù)報(bào)的位移量。
Java組播支持
MulticastSocket 多播數(shù)據(jù)報(bào)套接字。這個(gè)組播套接字對(duì)于收發(fā)IP多播數(shù)據(jù)包是很有用的,它擴(kuò)展了DatagramSocket,在其上附加了加入internet上多播組的方法。一個(gè)多播組由D類IP地址和標(biāo)準(zhǔn)UDP端口指定,D類IP范圍是224.0.0.0 to
239.255.255.255,其中224.0.0.0被保留不為它用。
它有三個(gè)構(gòu)造器:
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. |
基本上,沒有指定端口,只為發(fā)送,指定端口可收發(fā),多址主機(jī)會(huì)用套接字地址。
一,DatagramSocket類;DatagramPacket類;InetAddress類
構(gòu)造函數(shù) public DatagramSocket();public DatagramSocket(int port);
public DatagramSocket(InetAddress laddr);
close()方法
send(DatagramPacket p)
receive(DatagramPacket p)
接受方的 DatagramPacket public DatagramPacket (byte[]buf, intlength)發(fā)送方的 DatagramPacket public DatagramPacket (byte[]buf, intlength, InetAddressaddress, intport
最簡單的UDP程序:
發(fā)送程序:UdpSend.java














接收程序:UdpRecv.java















更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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