ServerSocketChannel类API详解(二)

netty中的ServerSocketChannel详解 Netty的通过封装Java NIO组件并引入事件驱动、责任链和主从线程模型,显著简化了高并发服务器的开发复杂度。其核心价值在于将底层I/O操作与业务逻辑解耦,开发者只需关注的实现即可构建高性能服务端。实际使用时需结合业务场景调整线程模型和参数配置,以充分发挥异步非阻塞架构的优势。 阅读详情
执行Connect连接操作

public abstract boolean connect( SocketAddress remote)方法的作用是连接到远程通道的Socket。

  • 如果此通道处于非阻塞模式,则此方法的调用将启动非阻塞连接操作。

  • 如果通道呈阻塞模式,则立即发起连接;如果呈非阻塞模式,则不是立即发起连接,而是在随后的某个时间才发起连接。

  • 如果连接是立即建立的,说明通道是阻塞模式,当连接成功时,则此方法返回true,连接失败出现异常。如果此通道处于阻塞模式,则此方法的调用将会阻塞,直到建立连接或发生I/O错误。

  • 如果连接不是立即建立的,说明通道是非阻塞模式,则此方法返回 false,并且以后必须通过调用 finish Connecto方法来验证连接是否完成。

虽然可以随时调用此方法,但如果在调用此方法时调用此通道上的读或写操作,则该操作将首先阻止,直到此调用完成为止。如果已尝试连接但失败,即此方法的调用引发检查异常,则该通道将关闭。

返回值代表如果建立了连接,则为true。如果此通道处于非阻塞模式且连接操作正在进行中,则为 false

阻塞的客户端:

long beginTime = 0;
        long endTime = 0;
        boolean connectResult = false;

        try {
            //SocketChannel是阻塞模式
            //在发生错误或连接到目标之前,connect()方法一直是阻塞的
            SocketChannel socketChannel = SocketChannel.open();
            beginTime = System.currentTimeMillis();
            connectResult = socketChannel.connect(new InetSocketAddress("localhost", 8088));
            endTime = System.currentTimeMillis();
            System.out.println("正常连接耗时:" + (endTime - beginTime) + " connectResult:" + connectResult);
            socketChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
            endTime = System.currentTimeMillis();
            System.out.println("异常连接耗时:" + (endTime - beginTime) + " connectResult:" + connectResult);
        }

在这里插入图片描述
出现异常说明连接建立失败,输出 false的原因是因为变量 connectResult的初始值为false,在程序执行的过程中并未对这个变量再次进行赋值。

添加服务端:

try {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.bind(new InetSocketAddress("localhost", 8088));
            SocketChannel socketChannel = serverSocketChannel.accept();
            socketChannel.close();
            serverSocketChannel.close();
            System.out.println("server end!");
        } catch (IOException e) {
            e.printStackTrace();
        }

先运行服务端,在运行客户端:
在这里插入图片描述

非阻塞的客户端:

long beginTime = 0;
        long endTime = 0;
        boolean connectResult = false;
        try {

            SocketChannel socketChannel = SocketChannel.open();
            //SocketChannel是非阻塞模式
            socketChannel.configureBlocking(false);
            beginTime = System.currentTimeMillis();
            connectResult = socketChannel.connect(new InetSocketAddress("localhost", 8088));
            endTime = System.currentTimeMillis();
            System.out.println("正常连接耗时:" + (endTime - beginTime) + " connectResult:" + connectResult);
            socketChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
            endTime = System.currentTimeMillis();
            System.out.println("异常连接耗时:" + (endTime - beginTime) + " connectResult:" + connectResult);
        }

在这里插入图片描述
输岀 false说明此通道处于非阻塞模式且连接操作正在进行中,此时 connectO方法返回flse。

先运行服务度,再运行非阻塞的客户端:
在这里插入图片描述

从两者的输出时间来看,耗时的差距不大,但阻塞模式由于正确连接到服务器,因此返回值为true,而非阻塞模式由于正在连接服务器,因此返回 false。

判断此通道上是否正在进行连接操作

public abstract boolean is Connection Pending()方法的作用是判断此通道上是否正在进行连接操作。返回值是true代表当且仅当已在此通道上发起连接操作,但是尚未通过调用 finish Connect()方法完成连接。还可以是在通道 accepto之后通道 closed之前,is ConnectionPending()方法的返回值都是true。

服务端代码:

try {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.bind(new InetSocketAddress("localhost", 8088));
            SocketChannel socketChannel = serverSocketChannel.accept();
            socketChannel.close();
            serverSocketChannel.close();
            System.out.println("server end!");
        } catch (IOException e) {
            e.printStackTrace();
        }

1. 阻塞通道,IP不存在:

//1. 阻塞通道,IP不存在
        SocketChannel socketChannel = null;

        try {
            socketChannel = SocketChannel.open();
            System.out.println(socketChannel.isConnectionPending());
            //192.168.0.123此IP不存在
            socketChannel.connect(new InetSocketAddress("192.168.0.123", 8088));
            socketChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("catch " + socketChannel.isConnectionPending());
            try {
                socketChannel.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

通道建立失败,catch中输出false,表示当前并没有进行连接。

2. 非阻塞通道,IP不存在:

//2. 非阻塞通道,IP不存在
        SocketChannel socketChannel = null;
        socketChannel = SocketChannel.open();
        //非阻塞
        socketChannel.configureBlocking(false);
        System.out.println(socketChannel.isConnectionPending());
        //192.168.0.123此IP不存在
        socketChannel.connect(new InetSocketAddress("192.168.0.123", 8088));
        System.out.println(socketChannel.isConnectionPending());
        socketChannel.close();
        

在这里插入图片描述
最后输出true,说明非阻塞通道正在建立连接。

3. 阻塞通道,IP存在:

SocketChannel socketChannel = null;

        try {
            socketChannel = SocketChannel.open();
            System.out.println(socketChannel.isConnectionPending());
            //192.168.0.123此IP不存在
            socketChannel.connect(new InetSocketAddress("localhost", 8088));
            System.out.println(socketChannel.isConnectionPending());
            socketChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("catch " + socketChannel.isConnectionPending());
            try {
                socketChannel.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

先运行服务端,在运行客户端:
在这里插入图片描述
上述程序运行结果并未出现异常,最后输出值为 false,说明阻塞通道并没有正在建立连接。

4. 非阻塞通道,IP存在:

SocketChannel socketChannel = null;
        socketChannel = SocketChannel.open();
        //非阻塞
        socketChannel.configureBlocking(false);
        System.out.println(socketChannel.isConnectionPending());
        //192.168.0.123此IP不存在
        socketChannel.connect(new InetSocketAddress("localhost", 8088));
        System.out.println(socketChannel.isConnectionPending());
        socketChannel.close();

在这里插入图片描述
最后输出true,说明非阻塞通道正在建立连接。

完成套接字通道的连接过程

public abstract boolean finish Connect()方法的作用是完成套接字通道的连接过程。通过将套接字通道置于非阻塞模式,然后调用其 connecto方法来发起非阻塞连接操作。

  • 如果连接操作失败,则调用此方法将导致抛出 IOException
    一旦建立了连接,或者尝试已失败,该套接字通道就变为可连接的,并且可调用此方法完成连接序列。

  • 如果已连接了此通道,则不阻塞此方法并且立即返回true。

  • 如果此通道处于非阻塞模式,那么当连接过程尚未完成时,此方法将返回 false;

  • 如果此通道处于阻塞模式,当连接成功之后返回true,连接失败时抛出描述该失败的、经过检查的异常。在连接完成或失败之前都将阻塞此方法。

虽然可在任意时间调用此方法,但如果正在调用此方法时在此通道上调用读取或写入操作,则在此调用完成前将首先阻塞该操作。
如果试图发起连接但失败了,也就是说,调用此方法导致抛出经过检查的异常,则关闭此通道。返回值当且仅当已连接此通道的套接字时才返回true。

类FileChannel中的long transferTo(position, count, WritableByteChannel)方法的使用

方法transfer To()的作用是试图读取此通道文件中给定 position处开始的 count个字节,并将其写入目标通道中,但是此方法的调用不一定传输所有请求的字节,是否传输取决于通道的性质和状态。
服务端:

public static void main(String[] args) throws Exception{
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        SocketChannel socketChannel = null;
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.bind(new InetSocketAddress("localhost", 8088));

        Selector selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        boolean isRun = true;
        while (isRun == true) {
            selector.select();
            Set<SelectionKey> set = selector.selectedKeys();
            Iterator<SelectionKey> it = set.iterator();
            while (it.hasNext()) {
                SelectionKey key = it.next();
                it.remove();
                if (key.isAcceptable()) {
                    socketChannel = serverSocketChannel.accept();
                    socketChannel.configureBlocking(false);
                    socketChannel.register(selector, SelectionKey.OP_WRITE);
                }
                if (key.isWritable()) {
                    RandomAccessFile file = new RandomAccessFile("/Users/acton_zhang/software/elasticsearch-5.6.8.tar.gz", "rw");
                    System.out.println("file.length=" + file.length());
                    FileChannel fileChannel = file.getChannel();
                    fileChannel.transferTo(0, file.length(), socketChannel);
                    fileChannel.close();
                    file.close();
                    socketChannel.close();
                }
            }
        }
        serverSocketChannel.close();
    }

客户端:

public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        socketChannel.connect(new InetSocketAddress("localhost", 8088));
        Selector selector = Selector.open();
        socketChannel.register(selector, SelectionKey.OP_CONNECT);
        boolean isRun = true;
        while (isRun == true) {
            System.out.println("begin selector");
            if (socketChannel.isOpen() == true) {
                selector.select();
                System.out.println("end selector");
                Set<SelectionKey> set = selector.selectedKeys();
                Iterator<SelectionKey> it = set.iterator();
                while (it.hasNext()) {
                    SelectionKey key = it.next();
                    it.remove();
                    if (key.isConnectable()) {
                        while (!socketChannel.finishConnect()){}
                        socketChannel.register(selector, SelectionKey.OP_READ);
                    }
                    if (key.isReadable()) {
                        ByteBuffer byteBuffer = ByteBuffer.allocate(50000);
                        int readLength = socketChannel.read(byteBuffer);
                        byteBuffer.flip();
                        long count = 0;
                        while (readLength != -1) {
                            count += readLength;
                            readLength = socketChannel.read(byteBuffer);
                            System.out.println("count=" + count + " readLength=" + readLength);
                            byteBuffer.clear();
                        }
                        System.out.println("读取结束");
                        socketChannel.close();
                    }
                }
            } else {
                break;
            }

        }
    }

服务端输出:

file.length=38041600

客户端输出:

begin selector
end selector
begin selector
end selector
count=50000 readLength=50000
count=100000 readLength=50000
count=150000 readLength=50000
count=200000 readLength=50000
count=250000 readLength=19868
count=269868 readLength=-1
读取结束
begin selector
open(SocketAddress remote)方法与SocketOption的执行顺序

如果先调用public static SocketChannel open(SocketAddress remote)方法,然后设置SocketPotion,则不会出现逾期的效果,因为在open()方法中已经自动执行了connect()方法。
在这里插入图片描述

在设置某些SocketOption特性时,需要在connect()方法执行之前进行初始化:

SocketChannel socketChannel = SocketChannel.open()
socketChannel.setOption(StandardSocketOptions.SO_REVBUF, 1234);
socketChannel.connet(new InetSocketAddress("localhost", 8088));
验证read和write方法是非阻塞的

执行代码configureBlocking(false)代表当前的I/O为非阻塞的,NIO 就是同步非阻塞模型,所以read和write方法也呈现此特性,下面开始实验。

测试read为非阻塞特性:

public static void main(String[] args) {
        ServerSocketChannel serverSocketChannel = null;
        try {
            serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.configureBlocking(false);
            serverSocketChannel.bind(new InetSocketAddress("localhost", 7077));

            Selector selector = Selector.open();
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            selector.select();
            Set<SelectionKey> set = selector.selectedKeys();
            Iterator<SelectionKey> iterator = set.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                ServerSocketChannel channel = (ServerSocketChannel) key.channel();
                SocketChannel socketChannel = channel.accept();
                socketChannel.configureBlocking(false);
                ByteBuffer byteBuffer = ByteBuffer.allocate(100);
                System.out.println("begin " + System.currentTimeMillis());
                socketChannel.read(byteBuffer);
                System.out.println("end " + System.currentTimeMillis() + " byteBuffer.position()=" + byteBuffer.position());
            }
            serverSocketChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


public static void main(String[] args) {
        try {
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress("localhost", 7077));
            socketChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

服务端输出:

begin 1591706395799
end 1591706395801 byteBuffer.position()=0
//read并没有读取到数据就继续向下运行,说明read具有非阻塞特性

测试write为非阻塞特性:

public static void main(String[] args) {
        try {
            ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
            serverSocketChannel.configureBlocking(false);
            serverSocketChannel.bind(new InetSocketAddress("localhost", 7077));

            Selector selector = Selector.open();
            serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
            selector.select();
            Set<SelectionKey> set = selector.selectedKeys();
            Iterator<SelectionKey> iterator = set.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                ServerSocketChannel channel = (ServerSocketChannel) key.channel();
                SocketChannel socketChannel = channel.accept();
                channel.configureBlocking(false);
                ByteBuffer byteBuffer = ByteBuffer.allocate(Integer.MAX_VALUE / 10);
                System.out.println("byteBuffer.limit()=" + byteBuffer.limit());
                System.out.println("begin " + System.currentTimeMillis());
                socketChannel.write(byteBuffer);
                System.out.println("end " + System.currentTimeMillis() + " byteBuffer.position()=" + byteBuffer.position());
            }
            serverSocketChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

public static void main(String[] args) {
        try {
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.configureBlocking(false);
            socketChannel.connect(new InetSocketAddress("localhost", 7077));
            socketChannel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

在这里插入图片描述
write方法并没有写到数据就继续向下运行。

岭回归的MATLAB步骤 MATLAB 实现框架,它涵盖了从数据导入到岭回归的步骤,包括计算共线性、使用 MAE、MSE、R²、MAPE 进行评价,以及绘制相应的可视化图表。 阅读详情

相关推荐

防火墙学习5---虚拟系统和跟系统通过路由表互访

虚拟系统与跟系统互访可分为虚拟系统访问根系统和跟系统访问虚拟系统。

weixin_48030849的博客 1035

Netty源码解析-NioSocketChannel之connect、read

前言: 前一篇中我们介绍了NioServerSocketChannel,NioServerSocketChannel主要负责两件事:绑定(bind)到本地port,作为一个Endpoint;监听客户端连接事件,将获取到的连接注册到EventLoop中。 那么数据的读写呢?NioServerSocketChannel不负责数据的读写,那么数据读写都交由NioSocketChannel来负责。 本文主要讲解NioSocketChannel 处理连接(connect)、读(read)...

恐龙弟旺仔的博客 1502

FDTD仿真进阶:液晶偏振调控与双折射效应全解析

本文深入解析了在Lumerical FDTD仿真中如何对液晶材料进行偏振调控与双折射效应建模。通过单轴液晶和螺旋扭曲液晶(TN-LCD)两大实战案例,详细阐述了从各向异性材料参数设置、LC网格属性配置到偏振椭圆分析的全流程,帮助工程师精准模拟液晶的光开关行为,优化如显示器等器件的光学性能。

chair的专栏 750

SocketChannel用法

SocketChannel用法 Java NIO中的SocketChannel是一个连接到TCP网络套接字的通道 SocketChannel支持设定参数 SO_SNDBUF 套接字发送缓冲区大小 SO_RCVBUF 套接字接收缓冲区大小 SO_KEEPALIVE 保护连接 O_REUSEADDR 复用地址 SO_LINGER 有数据传输时延缓关闭Channel(非阻塞模式下有用) TCP_NODELAY 禁用Nagle算法 (1)创建SocketChannel 第一种方式: SocketChannel so

m0_51001708的博客 4057

CVE-2025-25249漏洞深度解析:FortiOS认证绕过与实战缓解

远程代码执行(RCE)是网络安全中最危险的漏洞类型之一,其核心往往源于认证机制失效或内存安全缺陷。CVE-2025-25249正属于此类高危漏洞——它不依赖用户凭证,也不需要交互触发,而是通过超长HTTP头(如X-Forwarded-For)引发内存越界写入,直接绕过FortiOS和FortiSwitchManager的完整认证流程。该漏洞影响广泛、CVSS评分高达9.8,凸显了嵌入式设备在HTTP协议栈实现中的深层风险。在企业网络中,FortiGate等设备常作为边界守门人,一旦失陷将导致root级控制权

weixin_33694620的博客 527

Java NIO系列教程(8)-SocketChannel的最佳实践

打开一个SocketChannel,并连接到网络上的某台服务器一个新连接到达ServerSocketChannel时,会创建一个SocketChannel打开 SocketChannel下面是SocketChannel的打开方式的简单用法:看看Tomcat#NioEndpoint是如何使用的关闭 SocketChannel当用完SocketChannel之后调用关闭SocketChannel:从 SocketChannel 读取数据。

2401_84046876的博客 1219

Windows Server 2012评估版过期自动关机问题:两种高效解决方案详解

本文针对Windows Server 2012评估版过期导致的自动关机问题,提供了两种高效解决方案。一是通过slmgr.vbs命令重置授权,临时延长评估期;二是使用DISM工具将评估版转换为正式版,一劳永逸。文章详细解析了两种方法的操作步骤、注意事项及适用场景,帮助用户快速恢复服务器稳定运行。

pytorchlight8的博客 1199

NIO源码解析-SocketChannel

前言: SocketChannel作为网络套接字的通道,与之前我们学习到的FileChannel有很多不同之处(就是两个大类别的通道)。 没有SocketChannel之前,我们创建网络连接一般都是通过Socket和ServerSocket,这些都是BIO类别,性能的扩展会受到影响。 借助NIO相关实现SocketChannel和ServerSocketChannel,我们可以管理大量连接并且实现更小的性能损失。 本文就来介绍下SocketChannel的相关...

恐龙弟旺仔的博客 825

TC397 EB MCAL开发从0开始系列 之 [13.1] Dma配置实战 -实现M2M数据传输(读存储写存储)

TC3XX TC397 MCAL DMA 从0搭建测试用例-实现M2M 存储到存储数据传输

yy九歌的博客 1925

Unity-RetargetPro3-动画插件试用

一个名气大于实战的插件。

大话程序员 717

【SocketChannel详解】

SocketChannel是一种面向流的连接socket套接字的可选择通道。SocketChannel 作为网络 IO 通道,具体负责进行读写操作。NIO把缓冲区的数据写入通道,或者把通道里的数据读到缓冲区。具有特点:是用来连接Socket套接字的,主要用途用来处理网络I/O的通道,是基于TCP连接传输,SocketChannel实现了可选择通道,可以被多路复用的。

m0_72397750的博客 1630

使用xlwings在Python中对Excel表格进行操作是非常方便的,可以实现自动化处理、批量数据处理等功能

使用xlwings在Python中对Excel表格进行操作是非常方便的,可以实现自动化处理、批量数据处理等功能。本篇文章主要介绍如何使用xlwings对Excel表格进行多行多列数据设置和操作,并给出相应的源代码。接着,我们可以对这个范围内的所有单元格进行操作,如设置字体颜色、填充颜色、对齐方式等。以上就是使用xlwings对Excel表格进行多行多列数据操作和设置字体颜色、填充色和对齐方式的全部内容。最后,我们可以将数据写入到单元格中,也可以从单元格中读取数据。在Excel中,可以通过。

BUG?不存在的! 717

ServerSocketChannel类API详解(一)

ServerSocketChannel类 获得ServerSocketChannel与ServerSocket对象 ServerSocketChannel类是抽象的,并不能直接new实例化,但API中提供了public static ServerSocketChannel open()方法来创建ServerSocketChannel类的实例。open() 方法是静态的,作用是打开服务器套接.字通道。新通道的套接字最初是未绑定的;可以接受连接之前,必须通过它的某个套接字的bind()方法将其绑定到具体的地址。

吴声子夜歌的博客 5934

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【ServerSocketChannel详解】

ServerSocketChannel是面向流的监听socket套接字的可选择性通道。具有以下特点:具有阻塞和非阻塞两种模式;可以注册到多路复用器上(且一般都与复用器配合使用);基于TCP连接;需要绑定到特定端口上;是线程安全的。SocketChannel提供了连接到socket的通道,提供了类似于java.net包中对于网络操作的api的功能。既然已经有连接到Socket套接字的通道,可以主动发起连接、传输数据(client端),功能就是接收连接、接收数据的通道(server端)。

m0_72397750的博客 1148

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

ServerSocketChannel

ServerSocketChannel 让我们从最简单的ServerSocketChannel来开始对socket通道类的讨论 ServerSocketChannel是一个基于通道的socket监听器。它同我们所熟悉的java.net.ServerSocket执行相同的基本任务,不过它增加了通道语义,因此能够在非阻塞模式下运行。 用静态的open( )工厂方法创建一个新的Se

q243021856的专栏 631

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

NIO学习之ServerSocketChannel和SocketChannel

NIO学习Socket 通道ServerSocketChannelSocketChannel与ServerSocketChannel区别 Socket 通道 (1)新的 socket 通道类可以运行非阻塞模式并且是可选择的,可以激活大程序(如网络服务器和中间件组件)巨大的可伸缩性和灵活性。本节中我们会看到,再也没有为每个 socket 连接使用一个线程的必要了,也避免了管理大量线程所需的上下文交换开销。 借助新的 NIO 类,一个或几个线程就可以管理成百上千的活动 socket 连接了并且只有很少甚至可能

m0_53157173的博客 3793

ServerSocketChannel API用法

java.nio.channels 类 ServerSocketChannel java.lang.Object java.nio.channels.spi.AbstractInterruptibleChannel java.nio.channels.SelectableChannel java.nio.channels.spi.AbstractSelecta...

weixin_33966365的博客 103

【Java.NIO】API —— Channel接口 —— ServerSocketChannel类

java.nio.channels public abstract class ServerSocketChann extends AbstractSelectableChannel implements NetworkChannel ServerSocketChannel从SelectableChannel中继承了configureBlocking()和register()方法。 S

王晓斌的专栏 871

【Java源码阅读系列48】深度解读Java ServerSocketChannel 源码

Java NIO(New IO)通过非阻塞 IO 模型和多路复用机制,显著提升了网络编程的性能。ServerSocketChannel 作为 NIO 中服务端监听 TCP 连接的核心类,是构建高性能服务端的关键组件。本文将结合 JDK 1.8 源码,从类设计、关键方法、设计模式及实战示例四个维度,深入解析其实现逻辑与应用场景。

专注Java和MySQL领域知识分享 1045

NIO Channel SocketChannel ServerSocketChannel

ServerSocketChannel:   ServerSocketChannel是一个基于通道的socket监听器。它同我们所熟悉的java.net.ServerSocket执行相同的基本任务,不过它增加了通道语义,因此能够在非阻塞模式下运行。用静态的open( )工厂方法创建一个新的ServerSocketChannel对象,将会返回同一个未绑定的java.net.ServerS...

dimei8552的博客 159

简单聊天室SocketChannel ServerSocketChannel

利用SocketChannel ServerSocketChannel实现简单的聊天室 package com.mock; import org.junit.jupiter.api.Test; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import jav...

周莫客的博客 584

socket通道类ServerSocketChannel和DatagramChannel

NIO Channel中的DataGramChannel和ServerScoketChannel

weixin_46543456的博客 463

java.nio.channels.ServerSocketChannel

public abstract class ServerSocketChannel extends AbstractSelectableChannel java.lang.Object ? java.nio.channels.spi.AbstractInterruptibleChannel ? java.nio.channels.SelectableChannel ...

Marks' JavaEye 208
上一篇: ServerSocketChannel类API详解(一)
下一篇: Selector类的使用(一)
吴声子夜歌
博客等级 码龄9年 2690粉丝 2284原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值