NIO之简易服务器与客户端

采用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了
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值