Java NIO 案例

Server 端:

package NIO;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.Iterator;

public class Server {
    public static void main(String[] args) throws Exception{
        // 获取服务器通道
        ServerSocketChannel ssChannel = ServerSocketChannel.open();
        // 配置通道非阻塞
        ssChannel.configureBlocking(false);
        // 绑定端口
        ssChannel.bind(new InetSocketAddress(9898));
        // 获取选择器
        Selector selector = Selector.open();
        // 将服务器通道注册到selector
        ssChannel.register(selector, SelectionKey.OP_ACCEPT);
        // 有通道事件存在
        while (selector.select()>0){
            System.out.println("asking beginning :");
            // 获取通道事件
            Iterator<SelectionKey> it = selector.selectedKeys().iterator();
            // 遍历通道事件
            while (it.hasNext()){
                SelectionKey sk = it.next();
                // 处理连接事件
                if (sk.isAcceptable()){
                    // 获取客户端通道
                    SocketChannel acChannel = ssChannel.accept();
                    // 设置客户端通道位非阻塞
                    acChannel.configureBlocking(false);
                    // 将客户端通道注册到selector
                    acChannel.register(selector,SelectionKey.OP_READ);
                }
                // 处理可读事件
                if (sk.isReadable()){
                    // 获取该客户端通道
                    SocketChannel acChannel = (SocketChannel) sk.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    int len=0;
                    // 读该通道内容到buffer
                    while ((len = acChannel.read(buffer))!=0){
                        buffer.flip();
                        System.out.println(new String(buffer.array(),0,buffer.limit()));
                        buffer.clear();
                    }
                }
                // 事件处理结束,移除事件列表
                it.remove();
            }
        }
    }
}

Clint 端:

package NIO;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class Clint {
    public static void main(String[] args) throws Exception{
        // 获取客户端通道,需要绑定 ip:端口
        SocketChannel sc = SocketChannel.open(new InetSocketAddress("127.0.0.1",9898));
        // 设置为非阻塞模式
        sc.configureBlocking(false);

        Scanner scanner = new Scanner(System.in);
        ByteBuffer buff = ByteBuffer.allocate(1024);
        // 发送消息
        while (true){
            String s = scanner.nextLine();
            // 将输入内容写道buff
            buff.put(s.getBytes());
            // 指针置位
            buff.flip();
            // 写入通道
            sc.write(buff);
            buff.clear();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值