1.阻塞模式:
服务器代码:
package com.ityin.netty.c1;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;
import static com.ityin.netty.c1.ByteBufferUtil.debugRead;
@Slf4j
public class Server {
public static void main(String[] args) throws IOException {
//使用nio来理解阻塞模式,单线程
ByteBuffer buffer =ByteBuffer.allocate(16);
//1.创建了服务器
ServerSocketChannel ssc=ServerSocketChannel.open();
//2.绑定监听端口
ssc.bind(new InetSocketAddress(8080));
//3.连接集合
List<SocketChannel> channels=new ArrayList<>();
while(true){
//4.accept建立与客户端连接,SocketChannel用来与客户端之间通信
log.debug("connecting...");
SocketChannel sc=ssc.accept();//阻塞方法,线程停止运行
log.debug("connecting...{}",sc);
channels.add(sc);
for(SocketChannel channel:channels){
//5.接收客户端发送的数据
log.debug("before read...{}",channel);
channel.read(buffer);
buffer.flip();
debugRead(buffer);
buffer.clear();
log.debug("after read...{}",channel);
}
}
}
}
客户端代码:
package com.ityin.netty.c1;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
//
//public class Client {
// public static void main(String[] args) throws IOException {
// SocketChannel sc=SocketChannel.open();
// sc.connect(new InetSocketAddress("localhost",8080));
// System.out.println("waiting...");
// }
//}
public class Client {
public static void main(String[] args) {
try (SocketChannel socketChannel = SocketChannel.open()) {
// 建立连接
socketChannel.connect(new InetSocketAddress("localhost", 8080));
System.out.println("waiting...");
} catch (IOException e) {
e.printStackTrace();
}
}
}
通过debug后的evaluate expression建立连接:
得到以下的结果:
2.非阻塞模式
客户端代码修改为:
package com.ityin.netty.c1;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;
import static com.ityin.netty.c1.ByteBufferUtil.debugRead;
@Slf4j
public class Server {
public static void main(String[] args) throws IOException {
//使用nio来理解阻塞模式,单线程
ByteBuffer buffer =ByteBuffer.allocate(16);
//1.创建了服务器
ServerSocketChannel ssc=ServerSocketChannel.open();
ssc.configureBlocking(false);//切换成非阻塞模式
//2.绑定监听端口
ssc.bind(new InetSocketAddress(8080));
//3.连接集合
List<SocketChannel> channels=new ArrayList<>();
while(true){
//4.accept建立与客户端连接,SocketChannel用来与客户端之间通信
log.debug("connecting...");
SocketChannel sc=ssc.accept();//非阻塞方法,线程运行
if(sc!=null){
log.debug("connected...{}",sc);
sc.configureBlocking(false);
channels.add(sc);
}
// log.debug("connecting...{}",sc);
for(SocketChannel channel:channels){
//5.接收客户端发送的数据
log.debug("before read...{}",channel);
int read=channel.read(buffer);//非阻塞,线程仍然会继续运行,如果没有读到数据,read返回0
if(read>0){
buffer.flip();
debugRead(buffer);
buffer.clear();
}
log.debug("after read...{}",channel);
}
}
}
}
主要修改了两个方面,分别是: