我正在使用SocketChannel与远程服务器通信.我使用socketChannel.write()发送数据,没有错误和异常,但是,服务器日志表明没有收到数据; client tcp traffic monitor还显示ByteBuffer中的字符串消息未发送.
任何人都可以给我一个提示,为什么会这样?谢谢!
public class Client implements Runnable {
SocketChannel socketChannel;
Selector selector;
SelectionKey key;
ByteBuffer inbuf, outbuf;
int id;
@Override
public void run() {
try {
// prepare inbuf and outbuf
inbuf = ByteBuffer.allocate(10000);
outbuf = ByteBuffer.allocate(10000);
// prepare a socket channel for communication
socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("<remote server ip>", ));
selector = Selector.open();
socketChannel.configureBlocking(false);
key = socketChannel.register(selector, SelectionKey.OP_READ
| SelectionKey.OP_WRITE);
while (selector.select() > 0) {
if (key.isReadable()) {
// read from channel when server …Run Code Online (Sandbox Code Playgroud)