package com.nio;
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;
import java.util.Set;
public class NIOClient {
private static int flag = 1;
private static int blockSize = 4096;
private static ByteBuffer sendbuffer = ByteBuffer.allocate(blockSize);
private static ByteBuffer receivebuffer = ByteBuffer.allocate(blockSize);
private final static InetSocketAddress servereAddress = new InetSocketAddress("127.0.0.1", 7080);
public static void main(String[] args) throws IOException {
Selector selector = Selector.open();
SocketChannel socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
socketChannel.register(selector,SelectionKey.OP_CONNECT);
socketChannel.connect(servereAddress);
Set<SelectionKey> selectionKeys;
Iterator<SelectionKey> iterator;
SelectionKey selectionKey;
SocketChannel client;
String receiveText;
String sendText;
while(true){
selector.select();
selectionKeys = selector.selectedKeys();
iterator = selectionKeys.iterator();
while(iterator.hasNext()){
selectionKey = iterator.next();
if(selectionKey.isConnectable()){
System.out.println("client connect");
client = (SocketChannel)selectionKey.channel();
if(client.isConnectionPending()){
client.finishConnect();
System.out.println("客户端完成连接");
sendbuffer.clear();
sendbuffer.put("Hello,Servet".getBytes());
sendbuffer.flip();
client.write(sendbuffer);
}
client.register(selector, SelectionKey.OP_READ);
}
if(selectionKey.isReadable()){
client = (SocketChannel)selectionKey.channel();
receivebuffer.clear();
int count = client.read(receivebuffer);
if(count>0){
receiveText = new String(receivebuffer.array(),0,count);
System.out.println("客户端接收到服务端的数据:"+receiveText);
client.register(selector, selectionKey.OP_WRITE);
}
}
if(selectionKey.isWritable()){
sendbuffer.clear();
client = (SocketChannel)selectionKey.channel();
sendText = "MSG send to Server->"+flag++;
sendbuffer.put(sendText.getBytes());
sendbuffer.flip();
client.write(sendbuffer);
System.out.println("客户端发送数据给客户端:"+sendText);
client.register(selector, selectionKey.OP_READ);
}
iterator.remove();
}
selectionKeys.clear();
}
}
}
package com.nio;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
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;
import java.util.Set;
public class NIOServer {
private int blockSize = 4096;
private ByteBuffer sendbuffer = ByteBuffer.allocate(blockSize);
private ByteBuffer receivebuffer = ByteBuffer.allocate(blockSize);
private static Selector selector;
public NIOServer(int port) throws IOException{
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
ServerSocket serverSocket = serverSocketChannel.socket();
serverSocket.bind(new InetSocketAddress(port));
selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
System.out.println("Server start->"+port);
}
public void listen() throws IOException{
while(true){
selector.select();
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectionKeys.iterator();
while(iterator.hasNext()){
SelectionKey selectionKey = iterator.next();
iterator.remove();
handleKey(selectionKey);
}
}
}
public void handleKey(SelectionKey selectionKey) throws IOException{
ServerSocketChannel server = null;
SocketChannel client = null;
String reciveText;
int count = 0;
if(selectionKey.isAcceptable()){
server = (ServerSocketChannel)selectionKey.channel();
client = server.accept();
client.configureBlocking(false);
client.register(selector,selectionKey.OP_READ);
}else if(selectionKey.isReadable()){
client = (SocketChannel)selectionKey.channel();
count = client.read(receivebuffer);
if(count>0){
reciveText = new String(receivebuffer.array(),0,count);
System.out.println("服务端接收到客户端的信息:"+reciveText);
client.register(selector, selectionKey.OP_WRITE);
}
}else if(selectionKey.isWritable()){
sendbuffer.clear();
client = (SocketChannel) selectionKey.channel();
sendbuffer.put("msg send to client".getBytes());
sendbuffer.flip();
client.write(sendbuffer);
System.out.println("服务端发送数据给客户端: msg send to client");
}
}
public static void main(String[] args) throws Exception {
int port = 7080;
NIOServer nioServer = new NIOServer(port);
nioServer.listen();
}
}