[code]
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* UDP服务器
*
*/
public abstract class UDPServer implements Runnable {
// *********常数*********
private static final int MAX_BUFFER_SIZE = 8096;// 8k
private static final int DEFAULT_PORT = 9090;
// *********成员变量*********
private int bufferSize;
protected DatagramSocket socket;
private Thread runner = null;
private volatile boolean shouldStop = false;
// *********构造函数*********
/**
* 使用默认监听端口(9090)和默认的接收缓存大小(8096字节)构建一个UDPServer对象。
*
*/
protected UDPServer() throws SocketException {
this(DEFAULT_PORT, MAX_BUFFER_SIZE);
}
/**
*
* 构建一个UDPServer对象。使用默认的接收缓存大小(8096字节)
*
* @param port 服务监听端口
* @throws SocketException
*/
protected UDPServer(int port) throws SocketException {
this(port, MAX_BUFFER_SIZE);
}
/**
*
* 构建一个UDPServer对象。
*
* @param port 服务监听端口
* @param bufferSize 接收缓存大小(单位字节)
* @throws SocketException 端口被占用或没有权限时抛出的例外
*/
protected UDPServer(int port, int bufferSize) throws SocketException {
this.bufferSize = bufferSize;
this.socket = new DatagramSocket(port);
}
// *********成员方法*********
/**
* 开始UDP服务
*/
public synchronized void start() {
if (runner == null) {
runner = new Thread(this);
runner.setDaemon(false);
runner.start();
}
}
/**
* 停止UDP服务
*/
public synchronized void stop() {
if (socket != null) {
shouldStop = true;
runner.interrupt();
runner = null;
socket.close();
socket = null;
}
}
/**
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
byte[] buffer = new byte[bufferSize];
while (!shouldStop) {
DatagramPacket input = new DatagramPacket(buffer, buffer.length);
try {
socket.receive(input);
requestHandle(input);
} catch (IOException ioe) {
// igonre
}
}
}
/**
* 处理UDP请求
*
* @param input UDP数据报
*/
public abstract void requestHandle(DatagramPacket input);
}
[/code]
[url=http://www.51.la/?1613417][img]http://img.users.51.la/1613417.asp[/img][/url]
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* UDP服务器
*
*/
public abstract class UDPServer implements Runnable {
// *********常数*********
private static final int MAX_BUFFER_SIZE = 8096;// 8k
private static final int DEFAULT_PORT = 9090;
// *********成员变量*********
private int bufferSize;
protected DatagramSocket socket;
private Thread runner = null;
private volatile boolean shouldStop = false;
// *********构造函数*********
/**
* 使用默认监听端口(9090)和默认的接收缓存大小(8096字节)构建一个UDPServer对象。
*
*/
protected UDPServer() throws SocketException {
this(DEFAULT_PORT, MAX_BUFFER_SIZE);
}
/**
*
* 构建一个UDPServer对象。使用默认的接收缓存大小(8096字节)
*
* @param port 服务监听端口
* @throws SocketException
*/
protected UDPServer(int port) throws SocketException {
this(port, MAX_BUFFER_SIZE);
}
/**
*
* 构建一个UDPServer对象。
*
* @param port 服务监听端口
* @param bufferSize 接收缓存大小(单位字节)
* @throws SocketException 端口被占用或没有权限时抛出的例外
*/
protected UDPServer(int port, int bufferSize) throws SocketException {
this.bufferSize = bufferSize;
this.socket = new DatagramSocket(port);
}
// *********成员方法*********
/**
* 开始UDP服务
*/
public synchronized void start() {
if (runner == null) {
runner = new Thread(this);
runner.setDaemon(false);
runner.start();
}
}
/**
* 停止UDP服务
*/
public synchronized void stop() {
if (socket != null) {
shouldStop = true;
runner.interrupt();
runner = null;
socket.close();
socket = null;
}
}
/**
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
byte[] buffer = new byte[bufferSize];
while (!shouldStop) {
DatagramPacket input = new DatagramPacket(buffer, buffer.length);
try {
socket.receive(input);
requestHandle(input);
} catch (IOException ioe) {
// igonre
}
}
}
/**
* 处理UDP请求
*
* @param input UDP数据报
*/
public abstract void requestHandle(DatagramPacket input);
}
[/code]
[url=http://www.51.la/?1613417][img]http://img.users.51.la/1613417.asp[/img][/url]