nioServer:
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.Set;
/**
* NIO服务端
*/
public class NioServer {
public static void main(String[] args) throws Exception{
//====服务地址和端口====
String host = "127.0.0.1";
int port = 8080;
//================================1.向Selector注册Channel===================================================
//打开一个Channel通道
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
//设置为非阻塞
serverSocketChannel.configureBlocking(false);
//绑定端口
serverSocketChannel.socket().bind(new InetSocketAddress(host,port));
//创建Selector选择器
Selector selector = Selector.open();
//将Channel注册到Selector中,并设置对应事件
//SelectionKey.OP_ACCEPT:接收连接进行事件,表示服务器监听到了客户连接,那么服务器可以接收这个连接了
//SelectionKey.OP_CONNECT:连接就绪事件,表示客户与服务器的连接已经建立成功
//SelectionKey.OP_READ:读就绪事件,表示通道中已经有了可读的数据,可以执行读操作了(通道目前有数据,可以进行读操作了)
//SelectionKey.OP_WRITE:写就绪事件,表示已经可以向通道写数据了(通道目前可以用于写操作)
//事件注册后,会存储到一个SelectionKey中
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
//================================2.处理准备就绪的事件===================================================
while (true){
//读取Selector中准备就绪的事件,阻塞到至少有一个通道在你注册的事件上就绪了
selector.select();
//读取准备就绪的事件集合
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> iteratorKeys = selectionKeys.iterator();
while (iteratorKeys.hasNext()){
//注册的事件信息
SelectionKey key = iteratorKeys.next();
//判断时间
if(key.isAcceptable()){
//监听连接
SocketChannel channel = serverSocketChannel.accept();
channel.configureBlocking(false);
//注册事件为准备读就绪事件
channel.register(selector, SelectionKey.OP_READ);
}else if(key.isReadable()){
//读就绪事件,可以读取数据->一般会开一个线程
read(key);
}
//移除已处理的事件
iteratorKeys.remove();
}
}
}
/***
* 读取指定事件的数据
* @param key
*/
public static void read(SelectionKey key) throws Exception{
//获取Channel通道
SocketChannel channel = (SocketChannel) key.channel();
//创建缓冲区,并指定大小
ByteBuffer byteBuffer = ByteBuffer.allocate(256);
if(channel.read(byteBuffer)!=-1){
String content = new String(byteBuffer.array()).trim();
System.out.println("收到的客户端数据:"+content);
//回复数据
String reply = "thanks!";
channel.write(ByteBuffer.wrap( reply.getBytes()));
}else{
//没有数据关闭Channel
channel.close();
}
}
}
nioClient:
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
public class NioClient {
public static void main(String[] args) throws Exception {
//====服务地址和端口====
String host = "127.0.0.1";
int port = 8080;
//================================1.打开指定连接===================================================
//创建socket连接
InetSocketAddress address = new InetSocketAddress(host,port);
//打开连接
SocketChannel channel = SocketChannel.open(address);
//================================2.发送消息===================================================
String message = "hello server!";
ByteBuffer byteBuffer = ByteBuffer.wrap(message.getBytes());
channel.write(byteBuffer);
//================================3.读消息===================================================
ByteBuffer bf = ByteBuffer.allocate(256);
channel.read(bf);
String result = new String(bf.array());
System.out.println("收到服务端返回消息:"+result);
//================================4.关闭Channel===================================================
channel.close();
}
}