小编wiz*_*iam的帖子

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

我正在使用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)

java sockets nio socketchannel

1
推荐指数
1
解决办法
2955
查看次数

标签 统计

java ×1

nio ×1

socketchannel ×1

sockets ×1