NIO踩坑和难点,关于Selector和SocketChannel

1.对于 NIO 的 SocketChannel 每次触发 OP_READ 事件时,发送端不一定仅仅写入了一次,同理,发送端如果一次发送数据包过大,那么发送端的一次写入也可能会被拆分成两次 OP_READ 事件,所以 OP_READ 事件和发送端的 OP_WRITE 事件并不是一一对应的2.ServerSocketCSelector(选择器)用于监听多个通道的事件(比如:连接打开,数据到达)hannel

就像ServerSocketChannel绑定的是连接打开事件accept

  • accept 产生连接时触发
  • connect 客户端建立连接时出发
  • read 收到客户端消息时产生可读事件
  • 可写事件

这个时候一但又新的SocketChannel连接上了ServerSocketChannel才会触发这个事件,如果只是老的Tcp连接是不会触发的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Selector实现SocketChannel的文件传输和接收,可以按照以下步骤进行操作: 1. 创建一个Selector对象,并将SocketChannel注册到该Selector上。可以使用`Selector.open()`方法创建Selector对象,然后使用`channel.register(selector, SelectionKey.OP_CONNECT)`将SocketChannel注册到Selector上,指定感兴趣的事件为连接操作。 2. 在循环中,使用Selector的`select()`方法等待就绪的事件。如果有就绪的事件,可以使用`selector.selectedKeys()`方法获取就绪的SelectionKey集合。 3. 遍历就绪的SelectionKey集合,处理每个就绪的事件。如果事件是连接操作(`SelectionKey.OP_CONNECT`),则完成连接并将SocketChannel注册为读操作。 4. 如果事件是读操作(`SelectionKey.OP_READ`),则从SocketChannel中读取数据并写入文件。可以使用`FileChannel.write()`方法将读取到的数据写入文件。 5. 检查是否已经将文件的所有数据接收完毕。可以使用FileChannel的`position()`方法获取当前文件的写入位置,如果写入位置等于文件大小,则表示所有数据已经接收完毕。 6. 关闭文件通道和SocketChannel,并释放资源。 下面是一个简单的示例代码: ```java import java.io.FileOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.Selector; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; import java.util.Iterator; public class FileTransferExample { public static void main(String[] args) { try { Selector selector = Selector.open(); SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); socketChannel.connect(new InetSocketAddress("localhost", 12345)); socketChannel.register(selector, SelectionKey.OP_CONNECT); while (true) { selector.select(); Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator(); while (keyIterator.hasNext()) { SelectionKey key = keyIterator.next(); keyIterator.remove(); if (key.isConnectable()) { SocketChannel channel = (SocketChannel) key.channel(); if (channel.isConnectionPending()) { channel.finishConnect(); } channel.register(selector, SelectionKey.OP_READ); } if (key.isReadable()) { FileChannel fileChannel = new FileOutputStream("path/to/destination/file").getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); int bytesRead = socketChannel.read(buffer); if (bytesRead == -1) { fileChannel.close(); socketChannel.close(); System.out.println("File transfer completed."); return; } buffer.flip(); fileChannel.write(buffer); buffer.clear(); } } } } catch (IOException e) { e.printStackTrace(); } } } ``` 请注意,这只是一个简单的示例代码,并没有处理所有的异常情况。在实际开发中,还需要考虑处理连接错误、读写错误等异常情况,并进行适当的错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值