其他分享
首页 > 其他分享> > ServerSocketChannel用法

ServerSocketChannel用法

作者:互联网

ServerSocketChannel用法

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

打开 ServerSocketChannel

通过调用 ServerSocketChannel.open() 方法来打开ServerSocketChannel.如:

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

关闭 ServerSocketChannel

通过调用ServerSocketChannel.close() 方法来关闭ServerSocketChannel. 如:

serverSocketChannel.close();

监听新进来的连接

通过 ServerSocketChannel.accept() 方法监听新进来的连接。当 accept()方法返回的时候,它返回一个包含新进来的连接的 SocketChannel。因此, accept()方法会一直阻塞到有新连接到达。

通常不会仅仅只监听一个连接,在while循环中调用 accept()方法. 如下面的例子:

while(true){
    SocketChannel socketChannel =
            serverSocketChannel.accept();

    //使用socketChannel做一些工作...
}

当然,也可以在while循环中使用除了true以外的其它退出准则。

非阻塞模式

ServerSocketChannel可以设置成非阻塞模式。在非阻塞模式下,accept() 方法会立刻返回,如果还没有新进来的连接,返回的将是null。 因此,需要检查返回的SocketChannel是否是null.如:

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

serverSocketChannel.socket().bind(new InetSocketAddress(9999));
serverSocketChannel.configureBlocking(false);

while(true){
    SocketChannel socketChannel =
            serverSocketChannel.accept();

    if(socketChannel != null){
        //使用socketChannel做一些工作...
        }
}
package FileChannel演示;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class ServerSocketChannelDemo {
    public static void main(String[] args) throws IOException, InterruptedException {
        //端口号
        int port = 8888;

        //buffer
        ByteBuffer buffer = ByteBuffer.wrap("Hello".getBytes());

        //ServerSocketChannel
        ServerSocketChannel ssc = ServerSocketChannel.open();
        //绑定
        ssc.socket().bind(new InetSocketAddress(port));
        //设置非阻塞模式
        ssc.configureBlocking(false);

        //监听有新链接传入
        while(true) {
            System.out.println("Waiting for connections");
            SocketChannel sc = ssc.accept();
            if(sc == null) { //没有链接传入
                System.out.println("null");
                Thread.sleep(2000);
            } else {
                System.out.println("Incoming connection from:" + sc.socket().getRemoteSocketAddress());
                buffer.rewind();//指针0
                sc.write(buffer);
                sc.close();
            }
        }
    }
}

结果:Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null
Waiting for connections
Incoming connection from:/127.0.0.1:58785
Waiting for connections
Incoming connection from:/127.0.0.1:63031
Waiting for connections
null
Waiting for connections
Incoming connection from:/127.0.0.1:49906
Waiting for connections
Incoming connection from:/127.0.0.1:64096
Waiting for connections
null
Waiting for connections
null
Waiting for connections
null

有Incoming那行是在浏览器输入http://127.0.0.1:8888/

标签:serverSocketChannel,accept,用法,connections,Waiting,null,ServerSocketChannel
来源: https://blog.csdn.net/m0_51001708/article/details/120243525