采用NIO的方式写一个简易的服务器与客户端。
服务器
public class NIOServer {
public static void main(String[] args) throws IOException {
ServerSocketChannel ssc = ServerSocketChannel.open();
Selector selector = Selector.open();
// 将ServerSocketChannel绑定监听端口
ssc.socket().bind(new InetSocketAddress(8888));
// 将ServerSocketChannel设置为非阻塞模式
ssc.configureBlocking(false);
// 把ServerSocketChannel注册到多路复用器上
ssc.register(selector, SelectionKey.OP_ACCEPT);
while (true) {
if (selector.select(1000) == 0) {
System.out.println("服务器等待了一秒,无连接!");
continue;
}
// 如果select()返回值大于0,表示已经取到关注的事件
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> it = keys.iterator();
while (it.hasNext()) {
SelectionKey key = it.next();
// 如果是 OP_ACCEPT,表示有新客户端连接
if (key.isAcceptable()) {
SocketChannel sc = ssc.accept();
System.out.println("客户端连接服务器成功,该客户端的SocketChannel为" + sc.toString());
sc.configureBlocking(false);
// 将SocketChannel注册到 selector,关注事件为 OP_READ,同时给SocketChannel关联一个buffer
sc.register(selector,SelectionKey.OP_READ, ByteBuffer.allocate(1024));
}
if (key.isReadable()) {
// 通过key 反向获取对应的channel
SocketChannel sc = (SocketChannel) key.channel();
// 获取该channel关联的buffer
ByteBuffer buffer = (ByteBuffer) key.attachment();
sc.read(buffer);
System.out.println("服务器收到客户端的消息:" + new String(buffer.array()));
System.out.println("该客户端的SocketChannel为" + sc.toString());
}
// 移除处理完成的key
it.remove();
}
}
}
}
客户端
public class NIOClient {
public static void main(String[] args) throws IOException {
SocketChannel sc = SocketChannel.open();
sc.configureBlocking(false);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 8888);
// 连接服务器
if (!sc.connect(address)) {
while (!sc.finishConnect()) {
System.out.println("因为连接需要时间,客户端不会阻塞,可以做其他工作。");
}
}
// 连接成功之后,客户端发送信息
// ByteBuffer.wrap()方法不用计算缓冲区长度
ByteBuffer buffer = ByteBuffer.wrap("hello!".getBytes());
sc.write(buffer);
System.out.println("客户端已发送消息!");
// 先让程序停在这
while (true) {}
}
}
运行服务器并起两个客户端的运行结果:
服务器等待了一秒,无连接!
客户端连接服务器成功,该客户端的SocketChannel为java.nio.channels.SocketChannel[connected local=/127.0.0.1:8888 remote=/127.0.0.1:52072]
服务器收到客户端的消息:hello!
该客户端的SocketChannel为java.nio.channels.SocketChannel[connected local=/127.0.0.1:8888 remote=/127.0.0.1:52072]
服务器等待了一秒,无连接!
服务器等待了一秒,无连接!
服务器等待了一秒,无连接!
服务器等待了一秒,无连接!
客户端连接服务器成功,该客户端的SocketChannel为java.nio.channels.SocketChannel[connected local=/127.0.0.1:8888 remote=/127.0.0.1:52079]
服务器收到客户端的消息:hello!
该客户端的SocketChannel为java.nio.channels.SocketChannel[connected local=/127.0.0.1:8888 remote=/127.0.0.1:52079]
注意:idea中默认一个类只能同时运行一个,若要同时运行2个同一个类,需要进行设置。详细步骤如下。
第一步:右击需要并行运行的类,选择edit
第二步:右上角Allow parallel run打上勾就OK了