网络编程之:UDP客户端

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;

/**
 * UDP客户端
 * 
 */
public class UDPClient {
	// *********常数*********

	private static final int DEFAULT_RECEIVE_BUFFER_SIZE = 8192;
	private static final int DEFAULT_SO_TIMEOUT = 30000;

	// *********成员变量*********

	private SocketAddress serverSocketAddress;
	private byte[] sendData;
	private DatagramPacket output;

	private int receiveBufferSize; // in bytes
	private DatagramSocket ds;

	// *********构造函数*********

	/**
	 * 
	 * 构建一个UDPClient对象。
	 * 
	 * @param serverAddress 服务地址
	 * @param serverPort 服务器监听端口
	 * @throws SocketException
	 */
	public UDPClient(String serverAddress, int serverPort) throws SocketException {
		this(serverAddress, serverPort, new byte[1], DEFAULT_RECEIVE_BUFFER_SIZE, DEFAULT_SO_TIMEOUT);
	}

	/**
	 * 
	 * 构建一个UDPClient对象。
	 * 
	 * @param serverAddress 服务地址
	 * @param serverPort 服务器监听端口
	 * @param sendData 发送的数据
	 * @throws SocketException
	 */
	public UDPClient(String serverAddress, int serverPort, byte[] sendData) throws SocketException {
		this(serverAddress, serverPort, sendData, DEFAULT_RECEIVE_BUFFER_SIZE, DEFAULT_SO_TIMEOUT);
	}

	/**
	 * 
	 * 构建一个UDPClient对象。
	 * 
	 * @param serverAddress 服务地址
	 * @param serverPort 服务器监听端口
	 * @param sendData 发送的数据
	 * @param receiveBufferSize 接收缓存大小
	 * @throws SocketException
	 */
	public UDPClient(String serverAddress, int serverPort, byte[] sendData, int receiveBufferSize)
			throws SocketException {
		this(serverAddress, serverPort, sendData, receiveBufferSize, DEFAULT_SO_TIMEOUT);
	}

	/**
	 * 
	 * 构建一个UDPClient对象。
	 * 
	 * @param serverAddress 服务地址
	 * @param serverPort 服务器监听端口
	 * @param sendData 发送的数据
	 * @param receiveBufferSize 接收缓存大小
	 * @param timeout 超时
	 * @throws SocketException
	 */
	public UDPClient(String serverAddress, int serverPort, byte[] sendData, int receiveBufferSize, int timeout)
			throws SocketException {
		SocketAddress sa = new InetSocketAddress(serverAddress, serverPort);
		this.serverSocketAddress = sa;
		this.sendData = sendData;
		this.output = new DatagramPacket(sendData, sendData.length, sa);

		this.receiveBufferSize = receiveBufferSize;
		this.ds = new DatagramSocket(0);
		this.ds.setSoTimeout(timeout);
	}

	// *********成员方法*********

	/**
	 * 
	 * 构建一个UDPClient对象。
	 * 
	 * @param serverAddress 服务地址
	 * @param serverPort 服务器监听端口
	 * @param sendData 发送的数据
	 * @param receiveBufferSize 接收缓存大小
	 * @param timeout 超时
	 * @throws SocketException
	 */
	public void setServerSocketAddress(SocketAddress serverSocketAddress) {
		this.serverSocketAddress = serverSocketAddress;
	}

	/**
	 * 
	 * 构建一个UDPClient对象。
	 * 
	 * @param serverAddress 服务地址
	 * @param serverPort 服务器监听端口
	 * @param sendData 发送的数据
	 * @param receiveBufferSize 接收缓存大小
	 * @param timeout 超时
	 * @throws SocketException
	 */
	public void setSendData(byte[] sendData) {
		this.sendData = sendData;
	}

	/**
	 * 发送数据并且接受数据。
	 */
	public byte[] sendAndReceive() throws IOException {
		send();
		return receive();
	}

	/**
	 * 只发送数据。
	 */
	public void send() throws IOException {
		output.setSocketAddress(serverSocketAddress);
		output.setData(sendData);
		output.setLength(sendData.length);

		ds.connect(serverSocketAddress);
		ds.send(output);
	}
	
	/**
	 * 接收数据。
	 */
	public byte[] receive() throws IOException {
		byte[] response = null;
		DatagramPacket input = new DatagramPacket(new byte[receiveBufferSize], receiveBufferSize);
		// next line blocks until the response is received
		ds.receive(input);
		int numBytes = input.getLength();
		response = new byte[numBytes];
		System.arraycopy(input.getData(), 0, response, 0, numBytes);

		// may return null
		return response;
	}
	
	/**
	 * 关闭Socket。
	 */
	public void close() {
		this.ds.close();
	}
}


你可能感兴趣的:(编程,.net,socket,asp.net,asp)