e175. writing to a socketchannel.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 20 行

TXT
20
字号
See also e173 Creating a Non-Blocking Socket. 
    // Create a direct buffer to get bytes from socket.
    // Direct buffers should be long-lived and be reused as much as possible.
    ByteBuffer buf = ByteBuffer.allocateDirect(1024);
    
    try {
        // Fill the buffer with the bytes to write;
        // see e160 Putting Bytes into a ByteBuffer
        buf.put((byte)0xFF);
    
        // Prepare the buffer for reading by the socket
        buf.flip();
    
        // Write bytes
        int numBytesWritten = socketChannel.write(buf);
    } catch (IOException e) {
        // Connection may have been closed
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?