雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

【说站】java中SocketChannel是什么

2024-10-09 16

java中SocketChannel是什么

1、说明

SocketChannel代表套接字通道,实例是通过其静态方法创建的。

SocketChannel是SelectableChannel的子类,假如没有配置阻塞模式,那么SocketChannel对象默认为阻塞模式,那么open(SocketAddressremote)的方法实际上就是阻塞打开服务器连接。而SocketChannel上的任何I/O操作都是阻塞的。

2、实例

    public static SocketChannel open() throws IOException {
        return SelectorProvider.provider().openSocketChannel();
    }
 
    public static SocketChannel open(SocketAddress remote)
        throws IOException
    {
        // 1. ceate socket channel
        SocketChannel sc = open();
        try {
            // 2. connect channel's socket, blocking until connected or error
            sc.connect(remote);
        } catch (Throwable x) {
            try {
                sc.close();
            } catch (Throwable suppressed) {
                x.addSuppressed(suppressed);
            }
            throw x;
        }
        assert sc.isConnected();
        return sc;
    }

以上就是java中SocketChannel的介绍,希望对大家有所帮助。更多Java学习指路:Java基础

本教程操作环境:windows7系统、java10版,DELL G3电脑。

更新于:9天前
赞一波!

文章评论

评论问答