转自:http://blog.sina.com.cn/s/blog_d459b83c0101ewhe.html
package com.java.dao;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;
public class UDPServer extends Thread {
public void run() {
Selector selector = null;
try {
DatagramChannel channel = DatagramChannel.open();
DatagramSocket socket = channel.socket();
channel.configureBlocking(false);
socket.bind(new InetSocketAddress(5057));
selector = Selector.open();
channel.register(selector, SelectionKey.OP_READ);
} catch (Exception e) {
e.printStackTrace();
}
ByteBuffer byteBuffer = ByteBuffer.allocate(65536);
while (true) {
try {
int eventsCount = selector.select();
if (eventsCount > 0) {
SetselectedKeys = selector.selectedKeys();
Iteratoriterator = selectedKeys.iterator();
while (iterator.hasNext()) {
SelectionKey sk = (SelectionKey) iterator.next();
iterator.remove();
// 读事件
if (sk.isReadable()) {
DatagramChannel datagramChannel = (DatagramChannel) sk
.channel();
datagramChannel.receive(byteBuffer);
byteBuffer.flip();
// 测试:通过将收到的ByteBuffer首先通过缺省的编码解码成CharBuffer 再输出
CharBuffer charBuffer = Charset.defaultCharset()
.decode(byteBuffer);
System.out.println("receive message:"
+ charBuffer.toString());
byteBuffer.clear();
String echo = "This is the reply message from 服务器。";
ByteBuffer buffer = Charset.defaultCharset()
.encode(echo);
datagramChannel.write(buffer);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new UDPServer().start();
}
}
package com.java.dao;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;
public class UDPClient extends Thread {
public void run() {
DatagramChannel channel = null;
Selector selector = null;
try {
channel = DatagramChannel.open();
channel.configureBlocking(false);
SocketAddress sa = new InetSocketAddress("localhost", 5057);
channel.connect(sa);
} catch (Exception e) {
e.printStackTrace();
}
try {
selector = Selector.open();
channel.register(selector, SelectionKey.OP_READ);
channel.write(Charset.defaultCharset().encode("Tell me your time"));
} catch (Exception e) {
e.printStackTrace();
}
ByteBuffer byteBuffer = ByteBuffer.allocate(100);
while (true) {
try {
int eventsCount = selector.select();
if (eventsCount > 0) {
SetselectedKeys = selector.selectedKeys();
Iteratoriterator = selectedKeys.iterator();
while (iterator.hasNext()) {
SelectionKey sk = (SelectionKey) iterator.next();
iterator.remove();
// 读事件
if (sk.isReadable()) {
DatagramChannel datagramChannel = (DatagramChannel) sk
.channel();
datagramChannel.read(byteBuffer);
byteBuffer.flip();
// TODO 将报文转化为RUDP消息并调用RUDP协议处理器来处理
System.out.println(Charset.defaultCharset()
.decode(byteBuffer).toString());
byteBuffer.clear();
datagramChannel.write(Charset.defaultCharset()
.encode("Tell me your time"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new UDPClient().start();
}
}