Java中的UDP協議編程

開源框架:

http://mina.apache.org/

Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract · event-driven · asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.

Apache MINA is often called:

  • NIO framework · library,
  • client · server framework · library, or
  • a networking · socket library.

使用java代碼編寫:

參考:http://www.blogjava.net/honzeland/archive/2008/02/15/180097.html

以下轉自:http://www.blogjava.net/amigoxie/archive/2007/08/16/137413.html

一. UDP協議定義
   UDP協議的全稱是用戶數據報,在網絡中它與TCP協議一樣用於處理數據包。在OSI模型中,在第四層——傳輸層,處於IP協議的上一層。UDP有不提供數據報分組、組裝和不能對數據包的排序的缺點,也就是說,當報文發送之後,是無法得知其是否安全完整到達的。

二. 使用UDP的原因
   它不屬於連接型協議,因而具有資源消耗小,處理速度快的優點,所以通常音頻、視頻和普通數據在傳送時使用UDP較多,因爲它們即使偶爾丟失一兩個數據包,也不會對接收結果產生太大影響。比如我們聊天用的ICQ和OICQ就是使用的UDP協議。在選擇使用協議的時候,選擇UDP必須要謹慎。在網絡質量令人不十分滿意的環境下,UDP協議數據包丟失會比較嚴重。

三. 在Java中使用UDP協議編程的相關類
1. InetAddress
    用於描述和包裝一個Internet IP地址。有如下方法返回實例:
    getLocalhost():返回封裝本地地址的實例。

 getAllByName(String host):返回封裝Host地址的InetAddress實例數組。

 getByName(String host):返回一個封裝Host地址的實例。其中,Host可以是域名或者是一個合法的IP地址。
    InetAddress.getByAddress(addr):根據地址串返回InetAddress實例。
    InetAddress.getByAddress(host, addr):根據主機地符串和地址串返回InetAddress實例。

2. DatagramSocket
    用於接收和發送UDP的Socket實例。該類有3個構造函數:
    DatagramSocket():通常用於客戶端編程,它並沒有特定監聽的端口,僅僅使用一個臨時的。程序會讓操作系統分配一個可用的端口。
    DatagramSocket(int port):創建實例,並固定監聽Port端口的報文。通常用於服務端

 DatagramSocket(int port, InetAddress localAddr):這是個非常有用的構建器,當一臺機器擁有多於一個IP地址的時候,由它創建的實例僅僅接收來自LocalAddr的報文。
    DatagramSocket具有的主要方法如下:
    1)receive(DatagramPacket d):接收數據報文到d中。receive方法產生一個“阻塞”。“阻塞”是一個專業名詞,它會產生一個內部循環,使程序暫停在這個地方,直到一個條件觸發。

 2)send(DatagramPacket dp):發送報文dp到目的地。

 3)setSoTimeout(int timeout):設置超時時間,單位爲毫秒。

 4)close():關閉DatagramSocket。在應用程序退出的時候,通常會主動釋放資源,關閉Socket,但是由於異常地退出可能造成資源無法回收。所以,應該在程序完成時,主動使用此方法關閉Socket,或在捕獲到異常拋出後關閉Socket。

3. DatagramPacket
    用於處理報文,它將Byte數組、目標地址、目標端口等數據包裝成報文或者將報文拆卸成Byte數組。應用程序在產生數據包是應該注意,TCP/IP規定數據報文大小最多包含65507個,通常主機接收548個字節,但大多數平臺能夠支持8192字節大小的報文。DatagramPacket類的構建器共有4個:
    DatagramPacket(byte[] buf, int length):將數據包中Length長的數據裝進Buf數組,一般用來接收客戶端發送的數據。
    DatagramPacket(byte[] buf, int offset, int length):將數據包中從Offset開始、Length長的數據裝進Buf數組。
    DatagramPacket(byte[] buf, int length, InetAddress clientAddress, int clientPort):從Buf數組中,取出Length長的數據創建數據包對象,目標是clientAddress地址,clientPort端口,通常用來發送數據給客戶端。

 DatagramPacket(byte[] buf, int offset, int length, InetAddress clientAddress, int clientPort):從Buf數組中,取出Offset開始的、Length長的數據創建數據包對象,目標是clientAddress地址,clientPort端口,通常用來發送數據給客戶端。
    主要的方法如下:
     1)getData(): 從實例中取得報文的Byte數組編碼。
     2)setDate(byte[]  buf):將byte數組放入要發送的報文中。

四. 實例解析
    下面讓我們來看一個UDP的服務端和客戶端交互通信的例子,在本例中,服務端循環等待客戶端發送的信息,並對其進行迴應,客戶端向服務端發送信息,並接收服務端的迴應信息。代碼如下:
    1. UDP的服務端程序

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服務類.     
 * 
@author <a href="mailto:[email protected]">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;

    
/**
     * 構造函數,綁定主機和端口.
     * 
@param host 主機
     * 
@param port 端口
     * 
@throws Exception
     
*/

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

    
    
public final String getOrgIp() {
        
return orgIp;
    }


    
/**
     * 設置超時時間,該方法必須在bind方法之後使用.
     * 
@param timeout 超時時間
     * 
@throws Exception
     
*/

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


    
/**
     * 獲得超時時間.
     * 
@return 返回超時時間.
     * 
@throws Exception
     * 
@author <a href="mailto:[email protected]">AmigoXie</a>
     * Creation date: 2007-8-16 - 下午10:34:36
     
*/

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


    
/**
     * 綁定監聽地址和端口.
     * 
@param host 主機IP
     * 
@param port 端口
     * 
@throws SocketException
     * 
@author <a href="mailto:[email protected]">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);
    }



    
/**
     * 接收數據包,該方法會造成線程阻塞.
     * 
@return 返回接收的數據串信息
     * 
@throws IOException
     * 
@author <a href="mailto:[email protected]">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;
    }


    
/**
     * 將響應包發送給請求端.
     * 
@param bytes 迴應報文
     * 
@throws IOException
     * 
@author <a href="mailto:[email protected]">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);
    }


    
/**
     * 設置報文的緩衝長度.
     * 
@param bufsize 緩衝長度
     * 
@author <a href="mailto:[email protected]">AmigoXie</a>
     * Creation date: 2007-8-16 - 下午10:47:49
     
*/

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


    
/**
     * 獲得發送迴應的IP地址.
     * 
@return 返回迴應的IP地址
     * 
@author <a href="mailto:[email protected]">AmigoXie</a>
     * Creation date: 2007-8-16 - 下午10:48:27
     
*/

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


    
/**
     * 獲得迴應的主機的端口.
     * 
@return 返回迴應的主機的端口.
     * 
@author <a href="mailto:[email protected]">AmigoXie</a>
     * Creation date: 2007-8-16 - 下午10:48:56
     
*/

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


    
/**
     * 關閉udp監聽口.
     * 
@author <a href="mailto:[email protected]">AmigoXie</a>
     * Creation date: 2007-8-16 - 下午10:49:23
     
*/

    
public final void close() {
        
try {
            ds.close();
        }
 catch (Exception ex) {
            ex.printStackTrace();
        }

    }


    
/**
     * 測試方法.
     * 
@param args
     * 
@throws Exception
     * 
@author <a href="mailto:[email protected]">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客戶端程序,用於對服務端發送數據,並接收服務端的迴應信息.
 * 
@author <a href="mailto:[email protected]">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;

    
/**
     * 構造函數,創建UDP客戶端
     * 
@throws Exception
     
*/

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

    
    
/**
     * 設置超時時間,該方法必須在bind方法之後使用.
     * 
@param timeout 超時時間
     * 
@throws Exception
     * 
@author <a href="mailto:[email protected]">AmigoXie</a>
     * Creation date: 2007-8-16 - 下午10:55:12
     
*/

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


    
/**
     * 獲得超時時間.
     * 
@return 返回超時時間
     * 
@throws Exception
     * 
@author <a href="mailto:[email protected]">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;
    }


    
/**
     * 向指定的服務端發送數據信息.
     * 
@param host 服務器主機地址
     * 
@param port 服務端端口
     * 
@param bytes 發送的數據信息
     * 
@return 返回構造後俄數據報
     * 
@throws IOException
     * 
@author <a href="mailto:[email protected]">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;
    }


    
/**
     * 接收從指定的服務端發回的數據.
     * 
@param lhost 服務端主機
     * 
@param lport 服務端端口
     * 
@return 返回從指定的服務端發回的數據.
     * 
@throws Exception
     * 
@author <a href="mailto:[email protected]">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;
    }


    
/**
     * 關閉udp連接.
     * 
@author <a href="mailto:[email protected]">AmigoXie</a>
     * Creation date: 2007-8-16 - 下午10:53:52
     
*/

    
public final void close() {
        
try {
            ds.close();
        }
 catch (Exception ex) {
            ex.printStackTrace();
        }

    }


    
/**
     * 測試客戶端發包和接收回應信息的方法.
     * 
@param args
     * 
@throws Exception
     * 
@author <a href="mailto:[email protected]">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(
"服務端迴應數據:" + info);
    }

}


   參考文章:   http://topic.csdn.net/t/20060204/19/4539686.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章