Server
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public class Server {
private Selector selector;
public void initServer() throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
serverSocketChannel.socket().bind(new InetSocketAddress(8989));
selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
}
public void listen() throws IOException {
System.out.println("server started");
while (true) {
selector.select();
Iterator iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = (SelectionKey) iterator.next();
iterator.remove();
if (selectionKey.isAcceptable()) {
ServerSocketChannel serverSocketChannel = (ServerSocketChannel) selectionKey.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.write(ByteBuffer.wrap("hello client, from server".getBytes()));
socketChannel.register(selector, SelectionKey.OP_READ);
} else if (selectionKey.isReadable()) {
read(selectionKey);
}
}
}
}
public void read(SelectionKey key) throws IOException {
SocketChannel socketChannel = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(100);
socketChannel.read(byteBuffer);
byteBuffer.flip();
System.out.println("get client message:" + new String(byteBuffer.array()));
socketChannel.write(ByteBuffer.wrap("i got your msg client, from server".getBytes()));
}
public static void main(String[] args) throws IOException {
Server server = new Server();
server.initServer();
server.listen();
}
}
Client
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public class Client {
private Selector selector;
public void initClient() throws IOException {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("localhost", 8989));
selector = Selector.open();
socketChannel.register(selector, SelectionKey.OP_CONNECT);
}
public void listen() throws IOException {
while (true) {
selector.select();
Iterator iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = (SelectionKey) iterator.next();
iterator.remove();
if (selectionKey.isConnectable()) {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
if (socketChannel.isConnectionPending()) {
socketChannel.finishConnect();
}
socketChannel.configureBlocking(false);
socketChannel.write(ByteBuffer.wrap("hello server, i want to connect".getBytes()));
socketChannel.register(selector, SelectionKey.OP_READ);
} else if (selectionKey.isReadable()) {
read(selectionKey);
}
}
}
}
public void read(SelectionKey selectionKey) throws IOException {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(100);
socketChannel.read(byteBuffer);
byteBuffer.flip();
System.out.println("get msg from server:" + new String(byteBuffer.array()));
}
public static void main(String[] args) throws IOException {
Client client = new Client();
client.initClient();
client.listen();
}
}
nio reactor线程模型:http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf