找传奇、传世资源到传世资源站!

Java实现内网穿透

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

内网穿透、路由转发、代理Buffer实现循环缓冲提高效率,运行参数:本地端口 内网地址(域名) 内网端口 端口类型(固定1)

while (selectionKeyIterator.hasNext()) { SelectionKey selectionKey = selectionKeyIterator.next(); SelectableChannel channel = selectionKey.channel(); HostConnectionAttachment channelAttach = (HostConnectionAttachment) selectionKey.attachment(); CircleBufferDelegate sendBufferDelegate = channelAttach.sendBufferDelegate; CircleBufferDelegate recvBufferDelegate = channelAttach.recvBuffDelegate; if (selectionKey.isValid() && selectionKey.isReadable() && channel instanceof SocketChannel) { SocketChannel socketChannel = (SocketChannel) channel; int read = -1; try { while ((read = socketChannel.read(sendBufferDelegate.getBuffer())) > 0) { SocketChannel otherChannel = channelAttach.socketChannel; sendBufferDelegate.flip(); while (sendBufferDelegate.hasRemaining()) { // if ( i % 4 == 3) break;//模拟数据未读取完毕 int w = otherChannel.write(sendBufferDelegate.getBuffer()); if (w < 0) { throw new FCenterException("host write error"); } if (w == 0) { break; } } sendBufferDelegate.back(); } if (read == -1) { closeHostConnect(socketChannel, channelAttach); } else { sendBufferDelegate.flip(); if (sendBufferDelegate.hasRemaining()) channelAttach.socketKey.interestOps(channelAttach.socketKey.interestOps() | SelectionKey.OP_WRITE); sendBufferDelegate.back(); } } catch (Exception e) { closeHostConnect(socketChannel, channelAttach); logger.error("host connection thread read error,cause:" e.getMessage()); } } else if (selectionKey.isValid() && selectionKey.isWritable() && channel instanceof SocketChannel) { SocketChannel socketChannel = (SocketChannel) channel; recvBufferDelegate.flip(); while (recvBufferDelegate.hasRemaining()) { // if ( j % 4 == 3) break;//模拟数据未读取完毕 int w = socketChannel.write(recvBufferDelegate.getBuffer()); if (w < 0) { throw new FCenterException("host write error"); } } if (!recvBufferDelegate.hasRemaining()) selectionKey.interestOps(selectionKey.interestOps() ^ SelectionKey.OP_WRITE); recvBufferDelegate.back(); } selectionKeyIterator.remove(); }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复