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进行网络通信?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号