您的位置:首页 > 理论基础 > 计算机网络

TCP java nio code TBD

2016-01-15 00:46 471 查看
public class NIOServer {
//通道管理器
private Selector selector;

/**
* 获得一个ServerSocket通道,并对该通道做一些初始化的工作
* @param port  绑定的端口号
* @throws IOException
*/
public void initServer(int port) throws IOException {
// 获得一个ServerSocket通道
ServerSocketChannel serverChannel = ServerSocketChannel.open();
// 设置通道为非阻塞
serverChannel.configureBlocking(false);
// 将该通道对应的ServerSocket绑定到port端口
serverChannel.socket().bind(new InetSocketAddress(port));
// 获得一个通道管理器
this.selector = Selector.open();
//将通道管理器和该通道绑定,并为该通道注册SelectionKey.OP_ACCEPT事件,注册该事件后,
//当该事件到达时,selector.select()会返回,如果该事件没到达selector.select()会一直阻塞。
serverChannel.register(selector, SelectionKey.OP_ACCEPT);
}
<pre name="code" class="html">public static void main(String[] args) throws IOException, InterruptedException {
NIOServer server = new NIOServer();
server.initServer(8000);
//server.listen();
while(true){

System.out.println("select " + server.selector.select() + "event");
Iterator ite = server.selector.selectedKeys().iterator();  <strong><span style="color:#FF0000;">//服务端一旦select 变会向客户端 发送 accpct 事件,客户端必须要finish,客户端的
//selector 才会返回0 阻塞(正常),不然会想select jdk描述中一样认为 ready-operation sets were updated 不断返回1, 完成finish后 会不断返回0 ? 必须添加个感兴
//趣 时间后,才会block住 才认为状态没updated, 如果客户端注册的是 读事件,服务器通过channel write了,客户端这边 通过channel 读到了buffer中但是因为是nonblock的读
//可能每次只读了部分 如1000个字节只读了10个字节,那么selector 会认为那个 read事件 只是被updated 还没被消化 还会返回1 ,直到数据读完 才block住,也可以写while直接用
//channel 读完这个 read事件</span></strong>

 while (ite.hasNext()) {
SelectionKey key = (SelectionKey) ite.next();
// 删除已选的key,以防重复处理
ite.remove();
// 客户端请求连接事件
if (key.isAcceptable()) {
System.out.println("ssssssssss");
//Thread.sleep(5000000);
System.out.println("yyyyy");
ServerSocketChannel serverch = (ServerSocketChannel) key
.channel();

// 获得和客户端连接的通道
SocketChannel channel = serverch.accept();
// 设置成非阻塞
System.out.println("debug1:" +channel.socket().getPort());
channel.configureBlocking(false);
String msg = "jjjjjjjjjjjj";
ByteBuffer outBuffer = ByteBuffer.wrap(msg.getBytes());
channel.write(outBuffer);// 将消息回送给客户端
}
}
}

//在这里可以给客户端发送信息哦
//channel.write(ByteBuffer.wrap(new String("hello chickens").getBytes()));
//在和客户端连接成功之后,为了可以接收到客户端的信息,需要给通道设置读的权限。
//channel.register(this.selector, SelectionKey.OP_READ);

}

}



客户端

public class NIOClient {
// 通道管理器
private Selector selector;

/**
* 获得一个Socket通道,并对该通道做一些初始化的工作
*
* @param ip
*            连接的服务器的ip
* @param port
*            连接的服务器的端口号
* @throws IOException
*/
public void initClient(String ip, int port) throws IOException {
// 获得一个Socket通道
SocketChannel channel = SocketChannel.open();
// 设置通道为非阻塞
channel.configureBlocking(false);
// 获得一个通道管理器
this.selector = Selector.open();

// <strong><span style="color:#FF0000;">客户端连接服务器,其实方法执行并没有实现连接,需要在listen()方法中调,用channel.finishConnect();才能完成连接,才能将 select的connect事件消化,否则会
//一直返回1,且状态属于updated</span></strong>
channel.connect(new InetSocketAddress(ip, port));
// 将通道管理器和该通道绑定,并为该通道注册SelectionKey.OP_CONNECT事件。
channel.register(selector, SelectionKey.OP_CONNECT);
}
public static void main(String[] args) throws IOException,
            InterruptedException {
        NIOClient client = new NIOClient();
        client.initClient("localhost", 8000);
        while (true) {
            System.out.println("clientselect " + client.selector.select()
                    + "event");
            Iterator ite = client.selector.selectedKeys().iterator();
            while (ite.hasNext()) {
                SelectionKey key = (SelectionKey) ite.next();
                // 删除已选的key,以防重复处理
                ite.remove();
                // 客户端请求连接事件
                Thread.sleep(1000);
                if (key.isConnectable()) {

                    System.out.println("connectable event");
                    SocketChannel channel = (SocketChannel) key.channel();
                    // 如果正在连接,则完成连接
                    if (channel.isConnectionPending()) {
                        channel.finishConnect();
                        channel.register(client.selector, SelectionKey.OP_READ);// 加了一个稳定的注册时间它就不返回0了
                    }
                    // System.out.println("debug1:"
                    // +channel.socket().getLocalPort());
                    // 获得和客户端连接的通道
                    // SocketChannel channel = serverch.accept();
                    // 设置成非阻塞
                    // channel.configureBlocking(false);
                } else if (key.isReadable()) {
                    SocketChannel channel = (SocketChannel) key.channel();
                    // 创建读取的缓冲区
                    ByteBuffer buffer = ByteBuffer.allocate(1010);
                    channel.read(buffer);
                    byte[] data = buffer.array();
                    String msg = new String(data).trim();
                    System.out.println("client收到信息:" + msg);
                }
            }

        }

        // client.listen();
    }


简单说下 nio 和 老io 除了reactor的区别

同步的 socket tcp三次握手比( 1.socketclient 连接  2.socket服务端接受发送 3.socketclient read服务端发送的信息  这三个步骤是阻塞的 阻塞在read上。)

而nio 中, channel client不用阻塞的去read, 可以采用 selecter去 select 到READ请求, 再从selectedKeys中拿到 对应的channel 去读数据。

  其实这边channel 和
hadoop rpc 异步   的call 设计的比较像




TBD
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: