一. UDP協(xié)議定義 UDP協(xié)議的全稱是用戶數(shù)據(jù)報(bào),在網(wǎng)絡(luò)中它與TCP協(xié)議一樣用于處理數(shù)據(jù)包。在OSI模型中,在第四層——傳輸層,處于IP協(xié)議的上一層。UDP有不提供數(shù)據(jù)報(bào)分組、組裝和不能對(duì)數(shù)據(jù)包的排序的缺點(diǎn),也就是說(shuō),當(dāng)報(bào)文發(fā)送之后,是無(wú)法得知其是否安全完整到達(dá)的。
二. 使用UDP的原因 它不屬于連接型協(xié)議,因而具有資源消耗小,處理速度快的優(yōu)點(diǎn),所以通常音頻、視頻和普通數(shù)據(jù)在傳送時(shí)使用UDP較多,因?yàn)樗鼈兗词古紶杹G失一兩個(gè)數(shù)據(jù)包,也不會(huì)對(duì)接收結(jié)果產(chǎn)生太大影響。比如我們聊天用的ICQ和OICQ就是使用的UDP協(xié)議。在選擇使用協(xié)議的時(shí)候,選擇UDP必須要謹(jǐn)慎。在網(wǎng)絡(luò)質(zhì)量令人不十分滿意的環(huán)境下,UDP協(xié)議數(shù)據(jù)包丟失會(huì)比較嚴(yán)重。
三. 在Java中使用UDP協(xié)議編程的相關(guān)類1. InetAddress
用于描述和包裝一個(gè)Internet IP地址。有如下方法返回實(shí)例:
getLocalhost():返回封裝本地地址的實(shí)例。
getAllByName(String host):返回封裝Host地址的InetAddress實(shí)例數(shù)組。
getByName(String host):返回一個(gè)封裝Host地址的實(shí)例。其中,Host可以是域名或者是一個(gè)合法的IP地址。
InetAddress.getByAddress(addr):根據(jù)地址串返回InetAddress實(shí)例。
InetAddress.getByAddress(host, addr):根據(jù)主機(jī)地符串和地址串返回InetAddress實(shí)例。
2. DatagramSocket
用于接收和發(fā)送UDP的Socket實(shí)例。該類有3個(gè)構(gòu)造函數(shù):
DatagramSocket():通常用于客戶端編程,它并沒(méi)有特定監(jiān)聽(tīng)的端口,僅僅使用一個(gè)臨時(shí)的。程序會(huì)讓操作系統(tǒng)分配一個(gè)可用的端口。
DatagramSocket(int port):創(chuàng)建實(shí)例,并固定監(jiān)聽(tīng)Port端口的報(bào)文。通常用于服務(wù)端
DatagramSocket(int port, InetAddress localAddr):這是個(gè)非常有用的構(gòu)建器,當(dāng)一臺(tái)機(jī)器擁有多于一個(gè)IP地址的時(shí)候,由它創(chuàng)建的實(shí)例僅僅接收來(lái)自LocalAddr的報(bào)文。
DatagramSocket具有的主要方法如下:
1)receive(DatagramPacket d):接收數(shù)據(jù)報(bào)文到d中。receive方法產(chǎn)生一個(gè)“阻塞”。“阻塞”是一個(gè)專業(yè)名詞,它會(huì)產(chǎn)生一個(gè)內(nèi)部循環(huán),使程序暫停在這個(gè)地方,直到一個(gè)條件觸發(fā)。
2)send(DatagramPacket dp):發(fā)送報(bào)文dp到目的地。
3)setSoTimeout(int timeout):設(shè)置超時(shí)時(shí)間,單位為毫秒。
4)close():關(guān)閉DatagramSocket。在應(yīng)用程序退出的時(shí)候,通常會(huì)主動(dòng)釋放資源,關(guān)閉Socket,但是由于異常地退出可能造成資源無(wú)法回收。所以,應(yīng)該在程序完成時(shí),主動(dòng)使用此方法關(guān)閉Socket,或在捕獲到異常拋出后關(guān)閉Socket。
3. DatagramPacket
用于處理報(bào)文,它將Byte數(shù)組、目標(biāo)地址、目標(biāo)端口等數(shù)據(jù)包裝成報(bào)文或者將報(bào)文拆卸成Byte數(shù)組。應(yīng)用程序在產(chǎn)生數(shù)據(jù)包是應(yīng)該注意,TCP/IP規(guī)定數(shù)據(jù)報(bào)文大小最多包含65507個(gè),通常主機(jī)接收548個(gè)字節(jié),但大多數(shù)平臺(tái)能夠支持8192字節(jié)大小的報(bào)文。DatagramPacket類的構(gòu)建器共有4個(gè):
DatagramPacket(byte[] buf, int length):將數(shù)據(jù)包中Length長(zhǎng)的數(shù)據(jù)裝進(jìn)Buf數(shù)組,一般用來(lái)接收客戶端發(fā)送的數(shù)據(jù)。
DatagramPacket(byte[] buf, int offset, int length):將數(shù)據(jù)包中從Offset開(kāi)始、Length長(zhǎng)的數(shù)據(jù)裝進(jìn)Buf數(shù)組。
DatagramPacket(byte[] buf, int length, InetAddress clientAddress, int clientPort):從Buf數(shù)組中,取出Length長(zhǎng)的數(shù)據(jù)創(chuàng)建數(shù)據(jù)包對(duì)象,目標(biāo)是clientAddress地址,clientPort端口,通常用來(lái)發(fā)送數(shù)據(jù)給客戶端。
DatagramPacket(byte[] buf, int offset, int length, InetAddress clientAddress, int clientPort):從Buf數(shù)組中,取出Offset開(kāi)始的、Length長(zhǎng)的數(shù)據(jù)創(chuàng)建數(shù)據(jù)包對(duì)象,目標(biāo)是clientAddress地址,clientPort端口,通常用來(lái)發(fā)送數(shù)據(jù)給客戶端。
主要的方法如下:
1)getData(): 從實(shí)例中取得報(bào)文的Byte數(shù)組編碼。
2)setDate(byte[] buf):將byte數(shù)組放入要發(fā)送的報(bào)文中。
四. 實(shí)例解析
下面讓我們來(lái)看一個(gè)UDP的服務(wù)端和客戶端交互通信的例子,在本例中,服務(wù)端循環(huán)等待客戶端發(fā)送的信息,并對(duì)其進(jìn)行回應(yīng),客戶端向服務(wù)端發(fā)送信息,并接收服務(wù)端的回應(yīng)信息。代碼如下:
1. UDP的服務(wù)端程序
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;


/** *//**
* Copyright 2007 GuangZhou Cotel Co. Ltd.
* All right reserved.
* UTP服務(wù)類.
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* @version 1.0
* Creation date: 2007-8-16 - 下午10:32:31
*/

public class UdpServerSocket
{
private byte[] buffer = new byte[1024];
private DatagramSocket ds = null;

private DatagramPacket packet = null;

private InetSocketAddress socketAddress = null;

private String orgIp;


/** *//**
* 構(gòu)造函數(shù),綁定主機(jī)和端口.
* @param host 主機(jī)
* @param port 端口
* @throws Exception
*/

public UdpServerSocket(String host, int port) throws Exception
{
socketAddress = new InetSocketAddress(host, port);
ds = new DatagramSocket(socketAddress);
System.out.println("服務(wù)端啟動(dòng)!");
}

public final String getOrgIp()
{
return orgIp;
}


/** *//**
* 設(shè)置超時(shí)時(shí)間,該方法必須在bind方法之后使用.
* @param timeout 超時(shí)時(shí)間
* @throws Exception
*/

public final void setSoTimeout(int timeout) throws Exception
{
ds.setSoTimeout(timeout);
}


/** *//**
* 獲得超時(shí)時(shí)間.
* @return 返回超時(shí)時(shí)間.
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:34:36
*/

public final int getSoTimeout() throws Exception
{
return ds.getSoTimeout();
}


/** *//**
* 綁定監(jiān)聽(tīng)地址和端口.
* @param host 主機(jī)IP
* @param port 端口
* @throws SocketException
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:36:17
*/

public final void bind(String host, int port) throws SocketException
{
socketAddress = new InetSocketAddress(host, port);
ds = new DatagramSocket(socketAddress);
}



/** *//**
* 接收數(shù)據(jù)包,該方法會(huì)造成線程阻塞.
* @return 返回接收的數(shù)據(jù)串信息
* @throws IOException
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:38:24
*/

public final String receive() throws IOException
{
packet = new DatagramPacket(buffer, buffer.length);
ds.receive(packet);
orgIp = packet.getAddress().getHostAddress();
String info = new String(packet.getData(), 0, packet.getLength());
System.out.println("接收信息:" + info);
return info;
}


/** *//**
* 將響應(yīng)包發(fā)送給請(qǐng)求端.
* @param bytes 回應(yīng)報(bào)文
* @throws IOException
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午11:05:31
*/

public final void response(String info) throws IOException
{
System.out.println("客戶端地址 : " + packet.getAddress().getHostAddress()
+ ",端口:" + packet.getPort());
DatagramPacket dp = new DatagramPacket(buffer, buffer.length, packet
.getAddress(), packet.getPort());
dp.setData(info.getBytes());
ds.send(dp);
}


/** *//**
* 設(shè)置報(bào)文的緩沖長(zhǎng)度.
* @param bufsize 緩沖長(zhǎng)度
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:47:49
*/

public final void setLength(int bufsize)
{
packet.setLength(bufsize);
}


/** *//**
* 獲得發(fā)送回應(yīng)的IP地址.
* @return 返回回應(yīng)的IP地址
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:48:27
*/

public final InetAddress getResponseAddress()
{
return packet.getAddress();
}


/** *//**
* 獲得回應(yīng)的主機(jī)的端口.
* @return 返回回應(yīng)的主機(jī)的端口.
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:48:56
*/

public final int getResponsePort()
{
return packet.getPort();
}


/** *//**
* 關(guān)閉udp監(jiān)聽(tīng)口.
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:49:23
*/

public final void close()
{

try
{
ds.close();

} catch (Exception ex)
{
ex.printStackTrace();
}
}


/** *//**
* 測(cè)試方法.
* @param args
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:49:50
*/

public static void main(String[] args) throws Exception
{
String serverHost = "127.0.0.1";
int serverPort = 3344;
UdpServerSocket udpServerSocket = new UdpServerSocket(serverHost, serverPort);

while (true)
{
udpServerSocket.receive();
udpServerSocket.response("你好,sterning!");
}
}
}

2. UDP客戶端程序
import java.io.*;
import java.net.*;


/** *//**
* Copyright 2007 GuangZhou Cotel Co. Ltd.
* All right reserved.
* UDP客戶端程序,用于對(duì)服務(wù)端發(fā)送數(shù)據(jù),并接收服務(wù)端的回應(yīng)信息.
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* @version 1.0
* Creation date: 2007-8-16 - 下午10:54:23
*/

public class UdpClientSocket
{
private byte[] buffer = new byte[1024];

private DatagramSocket ds = null;


/** *//**
* 構(gòu)造函數(shù),創(chuàng)建UDP客戶端
* @throws Exception
*/

public UdpClientSocket() throws Exception
{
ds = new DatagramSocket();
}

/** *//**
* 設(shè)置超時(shí)時(shí)間,該方法必須在bind方法之后使用.
* @param timeout 超時(shí)時(shí)間
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:55:12
*/

public final void setSoTimeout(final int timeout) throws Exception
{
ds.setSoTimeout(timeout);
}


/** *//**
* 獲得超時(shí)時(shí)間.
* @return 返回超時(shí)時(shí)間
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:55:25
*/

public final int getSoTimeout() throws Exception
{
return ds.getSoTimeout();
}


public final DatagramSocket getSocket()
{
return ds;
}


/** *//**
* 向指定的服務(wù)端發(fā)送數(shù)據(jù)信息.
* @param host 服務(wù)器主機(jī)地址
* @param port 服務(wù)端端口
* @param bytes 發(fā)送的數(shù)據(jù)信息
* @return 返回構(gòu)造后俄數(shù)據(jù)報(bào)
* @throws IOException
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午11:02:41
*/
public final DatagramPacket send(final String host, final int port,

final byte[] bytes) throws IOException
{
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress
.getByName(host), port);
ds.send(dp);
return dp;
}


/** *//**
* 接收從指定的服務(wù)端發(fā)回的數(shù)據(jù).
* @param lhost 服務(wù)端主機(jī)
* @param lport 服務(wù)端端口
* @return 返回從指定的服務(wù)端發(fā)回的數(shù)據(jù).
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:52:36
*/
public final String receive(final String lhost, final int lport)

throws Exception
{
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
ds.receive(dp);
String info = new String(dp.getData(), 0, dp.getLength());
return info;
}


/** *//**
* 關(guān)閉udp連接.
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午10:53:52
*/

public final void close()
{

try
{
ds.close();

} catch (Exception ex)
{
ex.printStackTrace();
}
}


/** *//**
* 測(cè)試客戶端發(fā)包和接收回應(yīng)信息的方法.
* @param args
* @throws Exception
* @author <a href="mailto:xiexingxing1121@126.com">AmigoXie</a>
* Creation date: 2007-8-16 - 下午11:03:54
*/

public static void main(String[] args) throws Exception
{
UdpClientSocket client = new UdpClientSocket();
String serverHost = "127.0.0.1";
int serverPort = 3344;
client.send(serverHost, serverPort, ("你好,阿蜜果!").getBytes());
String info = client.receive(serverHost, serverPort);
System.out.println("服務(wù)端回應(yīng)數(shù)據(jù):" + info);
}
}

參考文章:
http://java.photoshopjiaocheng.com/sun-applet-class/sdk-api-code/virtual-machine-programming-9178.html http://www.eoot.cn/html/pro/java/20070511/25276.html http://topic.csdn.net/t/20060204/19/4539686.html