gpt4 book ai didi

Java NIO 从远程机器读取和写入

转载 作者:行者123 更新时间:2023-12-01 15:18:28 25 4
gpt4 key购买 nike

我想使用 NIO 向远程计算机发送数据/从远程计算机接收数据。我可以随时发送或接收数据,当我需要发送数据时,我只是发送数据,而不需要来自远程机器的任何查询,并且远程机器定期向我发送数据。我不明白NIO机制。什么在 Selector SelectionKey 上生成并读取或写入事件?是否可以在我这边仅使用一个 ServerSocketChannel 从远程机器读取数据并写入数据?这就是我的理解,但我不知道如何触发写入事件...谢谢您的解释。

我已经做了一些编码,我可以读取来自远程机器的数据,但无法写入。我使用选择器,我不知道如何写入数据。记录的消息“handle write”永远不会被写入,但在wireshark中我可以看到我的数据包。

    public class ServerSelector {

private static final Logger logger = Logger.getLogger(ServerSelector.class.getName());
private static final int TIMEOUT = 3000; // Wait timeout (milliseconds)
private static final int MAXTRIES = 3;
private final Selector selector;

public ServerSelector(Controller controller, int... servPorts) throws IOException {
if (servPorts.length <= 0) {
throw new IllegalArgumentException("Parameter(s) : <Port>...");
}
Handler consolehHandler = new ConsoleHandler();
consolehHandler.setLevel(Level.INFO);
logger.addHandler(consolehHandler);

// Create a selector to multiplex listening sockets and connections
selector = Selector.open();

// Create listening socket channel for each port and register selector
for (int servPort : servPorts) {
ServerSocketChannel listnChannel = ServerSocketChannel.open();
listnChannel.socket().bind(new InetSocketAddress(servPort));

listnChannel.configureBlocking(false); // must be nonblocking to register
// Register selector with channel. The returned key is ignored
listnChannel.register(selector, SelectionKey.OP_ACCEPT);
}

// Create a handler that will implement the protocol
IOProtocol protocol = new IOProtocol();

int tries = 0;
// Run forever, processing available I/O operations
while (tries < MAXTRIES) {
// Wait for some channel to be ready (or timeout)
if (selector.select(TIMEOUT) == 0) { // returns # of ready chans
System.out.println(".");
tries += 1;
continue;
}

// Get iterator on set of keys with I/O to process
Iterator<SelectionKey> keyIter = selector.selectedKeys().iterator();
while (keyIter.hasNext()) {
SelectionKey key = keyIter.next(); // Key is a bit mask
// Server socket channel has pending connection requests?
if (key.isAcceptable()) {
logger.log(Level.INFO, "handle accept");
protocol.handleAccept(key, controller);
}

// Client socket channel has pending data?
if (key.isReadable()) {
logger.log(Level.INFO, "handle read");
protocol.handleRead(key);
}

// Client socket channel is available for writing and
// key is valid (i.e., channel not closed) ?
if (key.isValid() && key.isWritable()) {
logger.log(Level.INFO, "handle write");
protocol.handleWrite(key);
}
keyIter.remove(); // remove from set of selected keys
tries = 0;
}
}
}
}

协议(protocol)

    public class IOProtocol implements Protocol {

private static final Logger logger = Logger.getLogger(IOProtocol.class.getName());

IOProtocol() {
Handler consolehHandler = new ConsoleHandler();
consolehHandler.setLevel(Level.INFO);
logger.addHandler(consolehHandler);
}

/**
*
* @param key
* @throws IOException
*/
@Override
public void handleAccept(SelectionKey key, Controller controller) throws IOException {
SocketChannel clntChan = ((ServerSocketChannel) key.channel()).accept();
clntChan.configureBlocking(false); // Must be nonblocking to register
controller.setCommChannel(clntChan);
// Register the selector with new channel for read and attach byte buffer
SelectionKey socketKey = clntChan.register(key.selector(), SelectionKey.OP_READ | SelectionKey.OP_WRITE, controller);
}

/**
* Client socket channel has pending data
*
* @param key
* @throws IOException
*/
@Override
public void handleRead(SelectionKey key) throws IOException {
Controller ctrller = (Controller)key.attachment();
try {
ctrller.readData();
} catch (CommandUnknownException ex) {
logger.log(Level.SEVERE, null, ex);
}
key.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
}

/**
* Channel is available for writing, and key is valid (i.e., client channel
* not closed).
*
* @param key
* @throws IOException
*/
@Override
public void handleWrite(SelectionKey key) throws IOException {

Controller ctrl = (Controller)key.attachment();
ctrl.writePendingData();
if (!buf.hasRemaining()) { // Buffer completely written ?
// Nothing left, so no longer interested in writes
key.interestOps(SelectionKey.OP_READ);
}
buf.compact();
}
}

Controller

    /**
* Fill buffer with data.
* @param msg The data to be sent
* @throws IOException
*/
private void writeData(AbstractMsg msg) throws IOException {
//
writeBuffer = ByteBuffer.allocate(msg.getSize() + 4);
writeBuffer.putInt(msg.getSize());
msg.writeHeader(writeBuffer);
msg.writeData(writeBuffer);
logger.log(Level.INFO, "Write data - message size : {0}", new Object[]{msg.getSize()});
logger.log(Level.INFO, "Write data - message : {0}", new Object[]{msg});
}

/**
* Write to the SocketChannel
* @throws IOException
*/
public void writePendingData() throws IOException {
commChannel.write(writeBuffer);
}

最佳答案

ServerSocketChannel 用于建立连接,但不发送数据。每个连接都需要一个 ServerSocketChannel 和一个 SocketChannel

使用SocketChannel读写示例:

ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = socketChannel.read(buf);

你的程序将在第二行 hibernate ,直到数据到来。您需要将此代码置于无限循环中并在后台线程中运行它。当数据到来时,您可以从该线程处理它,然后等待另一个数据到来。

ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put("Hello!".getBytes());

buf.flip();

while(buf.hasRemaining()) {
channel.write(buf);
}

没有阻塞方法,因此如果您发送小字节缓冲区,您可以从主线程调用它。

Source

添加:不要在新连接上设置 OP_WRITE 键。仅OP_READ。当您想要写入一些数据时,您需要通知选择器您想要发送一些内容并在事件循环中发送它。好的解决方案是创建一个输出消息的队列。然后按照以下步骤操作:

  • 将数据添加到队列
  • OP_WRITE 设置为 channel key
  • while (keyIter.hasNext()) 循环中,您将拥有可写 key ,从队列中写入所有数据并删除OP_WRITE key .

我很难理解你的代码,但我想你会找出问题所在。另外,如果您只想有一个连接,则无需使用Selector。您绑定(bind)了几个 ServerSocketChannels,这很奇怪。

关于Java NIO 从远程机器读取和写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11309858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com