上一篇 <<<BIO模型的缺陷
下一篇 >>>select、poll、epoll的区别
产生缘由
底层原理关键词
Nio技术多路IO复用底层实现原理
手写伪NIO代码
public class SocketNioTcpServer {
private static List<SocketChannel> listSocketChannel = new ArrayList<>();
private static ByteBuffer byteBuffer = ByteBuffer.allocate(512);
public static void main(String[] args) {
try {
// 1.创建一个ServerSocketChannel
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
// 2. 绑定地址
ServerSocketChannel bind = serverSocketChannel.bind(new InetSocketAddress(9090));
serverSocketChannel.configureBlocking(false);
while (true) {
/**channel管道:TCP数据传输的通道*/
SocketChannel socketChannel = serverSocketChannel.accept();
if (socketChannel != null) {
/**通道加入到选择器中*/
socketChannel.configureBlocking(false);
listSocketChannel.add(socketChannel);
}
/**轮询选择器下的所有通道信息,利用buffer缓存机制读取数据*/
for (SocketChannel scl : listSocketChannel) {
try {
int read = scl.read(byteBuffer);
if (read > 0) {
byteBuffer.flip();
Charset charset = Charset.forName("UTF-8");
String receiveText = charset.newDecoder().decode
(byteBuffer.asReadOnlyBuffer()).toString();
System.out.println(Thread.currentThread().getName()+" receiveText:" + receiveText);
}
listSocketChannel.remove(scl);
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
推荐阅读:
<<<OSI七层模型与层上协议
<<<TCP的三次握手建立链接和四次挥手释放链接
<<<TCP、UDP及Socket代码示例
<<<Https的1.0、2.0协议及长短链接区别
<<<Linux系统的五种IO模型
<<<BIO和NIO区别
<<<BIO模型的缺陷
<<<select、poll、epoll的区别
<<<Redis为什么单线程能够支持高并发
<<<Netty初识
<<<Netty的粘包和拆包问题分析
<<<粘包和拆包问题解决方案汇总
<<<序列化与反序列化知识点汇总
<<<MessagePack反序列化使用示例
<<<Marshalling在Netty中的使用