啦啦啦
Class : Service
package lime.pri.limeNio.optimize.socket;
import java.io.ByteArrayOutputStream;
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.Date;
import java.util.Iterator;
import java.util.Set;
/**
* 单线程NIO
*
* @author lime
*
*/
public class Service {
public static void main(String[] args) throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(9999));
serverSocketChannel.configureBlocking(false);
Selector selector = Selector.open();
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
System.out.println("监听端口@9999,等待客户端连接...");
int n = selector.select();
System.out.println("事件就绪通道个数 : " + n);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> iterator = selectedKeys.iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
iterator.remove();
if (selectionKey.isAcceptable()) {
System.out.println("-- -- -- 处理Acceptable事件");
ServerSocketChannel ssc = (ServerSocketChannel) selectionKey.channel();
SocketChannel sc = ssc.accept();
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ);
} else if (selectionKey.isReadable()) {
System.out.println("-- -- -- 处理Readable事件");
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
byteBuffer.clear();
SocketChannel sc = (SocketChannel) selectionKey.channel();
sc.read(byteBuffer);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(byteBuffer.array());
System.out.println("客户端( " + sc.getRemoteAddress() + " ) 请求 : " + bos.toString());
sc.register(selector, SelectionKey.OP_WRITE);
} else if (selectionKey.isWritable()) {
System.out.println("-- -- -- 处理Writable事件");
String response = "服务端响应 : " + new Date().toString();
ByteBuffer byteBuffer = ByteBuffer.wrap(response.getBytes());
SocketChannel sc = (SocketChannel) selectionKey.channel();
sc.write(byteBuffer);
sc.close();
}
}
}
}
}
Class : Client
package lime.pri.limeNio.optimize.socket;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
public class Client {
public static void main(String[] args) throws IOException {
for (int i = 0; i < 10; i++) {
new Thread() {
{
setDaemon(false);
}
public void run() {
try {
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("127.0.0.1", 9999));
socketChannel.write(ByteBuffer.wrap("Query Date".getBytes()));
socketChannel.shutdownOutput();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
socketChannel.read(byteBuffer);
System.out.println(new String(byteBuffer.array()));
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}
}
}
Console : Server
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 1
-- -- -- 处理Acceptable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 1
-- -- -- 处理Readable事件
客户端( /127.0.0.1:2689 ) 请求 : Query Date
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 1
-- -- -- 处理Writable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 1
-- -- -- 处理Acceptable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 2
-- -- -- 处理Readable事件
客户端( /127.0.0.1:2690 ) 请求 : Query Date
-- -- -- 处理Acceptable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 3
-- -- -- 处理Writable事件
-- -- -- 处理Readable事件
客户端( /127.0.0.1:2691 ) 请求 : Query Date
-- -- -- 处理Acceptable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 3
-- -- -- 处理Writable事件
-- -- -- 处理Readable事件
客户端( /127.0.0.1:2692 ) 请求 : Query Date
-- -- -- 处理Acceptable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 3
-- -- -- 处理Readable事件
客户端( /127.0.0.1:2693 ) 请求 : Query Date
-- -- -- 处理Writable事件
-- -- -- 处理Acceptable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 3
-- -- -- 处理Writable事件
-- -- -- 处理Readable事件
客户端( /127.0.0.1:2694 ) 请求 : Query Date
-- -- -- 处理Acceptable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 3
-- -- -- 处理Readable事件
客户端( /127.0.0.1:2695 ) 请求 : Query Date
-- -- -- 处理Writable事件
-- -- -- 处理Acceptable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 3
-- -- -- 处理Writable事件
-- -- -- 处理Readable事件
客户端( /127.0.0.1:2696 ) 请求 : Query Date
-- -- -- 处理Acceptable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 3
-- -- -- 处理Readable事件
客户端( /127.0.0.1:2697 ) 请求 : Query Date
-- -- -- 处理Writable事件
-- -- -- 处理Acceptable事件
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 2
-- -- -- 处理Writable事件
-- -- -- 处理Readable事件
客户端( /127.0.0.1:2698 ) 请求 : Query Date
监听端口@9999,等待客户端连接...
事件就绪通道个数 : 1
-- -- -- 处理Writable事件
监听端口@9999,等待客户端连接...
Console : Client
Thread[Thread-7,5,main] 服务端响应 : Sat Jun 24 16:00:41 CST 2017
Thread[Thread-0,5,main] 服务端响应 : Sat Jun 24 16:00:41 CST 2017
Thread[Thread-3,5,main] 服务端响应 : Sat Jun 24 16:00:41 CST 2017
Thread[Thread-9,5,main] 服务端响应 : Sat Jun 24 16:00:41 CST 2017
Thread[Thread-1,5,main] 服务端响应 : Sat Jun 24 16:00:41 CST 2017
Thread[Thread-4,5,main] 服务端响应 : Sat Jun 24 16:00:41 CST 2017
Thread[Thread-6,5,main] 服务端响应 : Sat Jun 24 16:00:41 CST 2017
Thread[Thread-5,5,main] 服务端响应 : Sat Jun 24 16:00:41 CST 2017
Thread[Thread-8,5,main] 服务端响应 : Sat Jun 24 16:00:41 CST 2017
Thread[Thread-2,5,main] 服务端响应 : Sat Jun 24 16:00:41 CST 2017
啦啦啦