【从NIO到Netty】5.NIO EchoServer
2021-01-29 21:15
标签:serve charset else code channel port sele locking datetime 完成了对NIO三大组件,Buffer,Channle,Selector的介绍,现在可以介绍一个完整的NIO EchoServer的例子了 这里再次重新介绍一下channel,因为无论服务端还是客户端, 在读数据的时候,channel都是从SelectionKey反向拿到的,可能第一次看不明白,其实这里反向拿到的channel,就是register时注册的channel 但是这段代码存在粘包和拆包的问题,所谓粘包和拆包的问题,是TCP协议下的Nagle算法,为了优化传输效率,可能将多个间隔时间端,且规模较小的包,合并成一个包进行发送。 如何解决粘包拆包问题,留到介绍NIO时再来解决 Server Client 【从NIO到Netty】5.NIO EchoServer 标签:serve charset else code channel port sele locking datetime 原文地址:https://www.cnblogs.com/heben/p/13199314.htmlpackage org.scaventz.nio.mine;
import io.netty.util.CharsetUtil;
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.time.LocalDateTime;
import java.util.Iterator;
import java.util.Set;
public class NioEchoServer {
public static void main(String[] args) throws IOException {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(8080));
// 设置为非阻塞
serverSocketChannel.configureBlocking(false);
// 获得一个 Selector 对象
Selector selector = Selector.open();
// 将我们的 serverSocketChannel 注册到 selector上,注册的事件为 ACCEPT事件
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
// 阻塞等待,也可以选择不阻塞 比如select(1000),等待1ms
selector.select();
Set
package org.scaventz.nio.mine;
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.time.LocalDateTime;
import java.util.Iterator;
import java.util.Set;
public class NioEchoClient {
private final String HOST = "localhost";
private final int PORT = 8080;
SocketChannel socketChannel = null;
Selector selector = null;
public static void main(String[] args) throws IOException, InterruptedException {
NioEchoClient client = new NioEchoClient();
client.go();
}
public void go() throws IOException, InterruptedException {
selector = Selector.open();
// 一个线程专门负责发送信息给服务器端
new Thread(() -> {
try {
socketChannel = SocketChannel.open(new InetSocketAddress(HOST, PORT));
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
for (int i = 0; i ) {
String msg = "hello world" + i;
socketChannel.write(ByteBuffer.wrap(msg.getBytes()));
System.out.println("[" + LocalDateTime.now() + "]sent: " + msg);
}
} catch (Exception e) {
e.printStackTrace();
try {
socketChannel.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}).start();
// 一个线程专门负责处理服务端回送的消息
new Thread(() -> {
while (true) {
try {
if (selector.select(2000) > 0) {
Set
文章标题:【从NIO到Netty】5.NIO EchoServer
文章链接:http://soscw.com/index.php/essay/48856.html