ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ServerSocketChannel用法

2021-09-11 21:31:07  阅读:182  来源: 互联网

标签:serverSocketChannel accept 用法 connections Waiting null 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有