zoukankan      html  css  js  c++  java
  • 网络编程模型之NIO-ServerSocketChannel

    1.基本介绍

      Java NIO中的 ServerSocketChannel 是一个可以监听新进来的TCP连接的通道, 就像标准IO中的ServerSocket一样。ServerSocketChannel类在 java.nio.channels包中。

    2.ServerSocketChannel 的创建

      通过ServerSocketChannel类的静态方法open()获得。

    3.端口的获取

      每个ServerSocketChannel都有一个对应的ServerSocket,通过其socket()方法获得。获得ServerSocket是为了使用其bind()方法绑定监听端口号。若是使用其accept()方法监听请求就和普通Socket的处理模式无异。

    4. 设置是否使用阻塞模式

      true/false。configureBlocking(false)——不适用阻塞模式。阻塞模式不能使用Selector!

    5.利用buffer数组完成读写操作

      5.1 模拟服务端

    /**
     * 1.模拟服务端
     * 2.利用buffer数组完成读写
     */
    public class ArrayBuffer {
        public static void main(String[] args) throws Exception {
            //  1. 创建serverSocketChannel
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            InetSocketAddress inetSocketAddress=new InetSocketAddress(7000);
            //   2.绑定端口
            serverSocketChannel.socket().bind(inetSocketAddress);
            //3.创建buffer数组
            ByteBuffer[]byteBuffers=new ByteBuffer[2];
            //4.设置每个buffer的容量
            byteBuffers[0]=ByteBuffer.allocate(5);
            byteBuffers[1]=ByteBuffer.allocate(3);
            //5.等待客户端(telnet)连接
            SocketChannel socketChannel=serverSocketChannel.accept();
            //6.循环读取
            int messageLength=8; //假设客户端只发送八个字节
            while (true){
                long byteRead=0;
                while (byteRead<messageLength){
                    long num = socketChannel.read(byteBuffers);
                    byteRead+=num;//累计读取的字节数量
                    System.out.println("byteRead="+byteRead);
                    //7.流打印,看当前buffer的position和limit
                    Arrays.asList(byteBuffers).stream().map(buffer->"position:"+buffer.position()+ ",limit:"+buffer.limit()).forEach(System.out::println);
                }
                //8.将所有的buffer进行flip
                Arrays.asList(byteBuffers).forEach(byteBuffer -> byteBuffer.flip());
                //9.将客户端发来的数据信息到服务端控制台
                long byteWrite=0;
                while (byteWrite<messageLength){
                    long write = socketChannel.write(byteBuffers);
                    byteWrite+=write;
                }
                //10.将所有的buffer进行clear
                Arrays.asList(byteBuffers).forEach(byteBuffer -> byteBuffer.clear());
                System.out.println("byteRead:"+byteRead+",byteWrite:"+byteWrite+",messageLength:"+messageLength);
            }
        }
    }

      5.2  telnet模拟客户端

        a.windows-7开启telnet

        

       b.命令行启动

        1.Telnet  服务器ip地址 端口号

        

        2. ctrl+]

        

        3. send +发送的消息

      5.3 控制台打印效果

    byteRead=8
    position:5,limit:5
    position:3,limit:3
    byteRead:8,byteWrite:8,messageLength8
    从上面控制台数据可以看出,buffer数组成功执行了..

  • 相关阅读:
    浅谈网络语音技术(转)
    常用的XMPP服务器
    双码流 主码流子码流(转)
    C++ 程序员必读书目清单
    Error c3876: function call missing argument list; use '' to create a pointer to member
    C#, C++, Java性能对比
    error LNK2001: unresolved external symbol __imp
    error LNK2005: _DllMain@12 already defined in MSVCRTD.lib
    【转】无缝世界网游服务器架构的设计思路
    VS2010生成exe在别的机子上运行提示“丢失MSVCR100D.dll”
  • 原文地址:https://www.cnblogs.com/hyy9527/p/13058191.html
Copyright © 2011-2022 走看看