SocketChannel write()返回时没有错误,但实际上没有发送任何数据



我正在使用SocketChannel与远程服务器通信。我使用socketChannel.write()发送数据,没有错误和异常,但是,服务器日志显示没有收到任何数据;客户端tcp流量监视器还显示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 sends data
read();
}
if (key.isWritable()) {
// write
Random r = new Random(500);
write("b", r.nextInt(), r.nextInt());
for (int i = 0; i < 10; i++) {
// write a message to server after 1 second
Thread.sleep(1000);
write("m", r.nextInt(), r.nextInt());
}
write("e", r.nextInt(), r.nextInt());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void write(String action, int x, int y) throws IOException {
String msg = String.format("%s:%d:%d:%d", action, id, x, y);
int r=outbuf.remaining();
outbuf.put(msg.getBytes());
int rBytes = outbuf.remaining();
boolean connected = socketChannel.isConnected();
Socket sock = socketChannel.socket();
if (connected && sock.isConnected() && !sock.isOutputShutdown())
>>>>>>>>>>    socketChannel.write(outbuf);
else
System.out.println("Connection broken!");
System.out.printf("Client %d told server:%sn", id, msg);
//outbuf.clear();
}
... //read omitted here

将数据放入Buffer或读取数据后,必须翻转缓冲区才能写入或从中获取数据。请检查Buffer类中的flip()方法。医生说

翻转此缓冲区。将限制设置为当前位置,然后将位置设置为零。如果定义了标记,则会将其丢弃。在一系列通道读取或放置操作之后,调用此方法为一系列通道写入或相对获取操作做准备。

所以在put之后添加一个buffer.flip()就可以了:)

相关内容

最新更新