Detecting When a Non-Blocking Socket Is Closed by the Remote Host - Java Network

Java examples for Network:Socket

Description

Detecting When a Non-Blocking Socket Is Closed by the Remote Host


// Read from socket
int numBytesRead = socketChannel.read(buf);

if (numBytesRead == -1) {
    // No more bytes can be read from the channel
    socketChannel.close();
} else {
    // Read the bytes from the buffer
}

// Write to socket
int numBytesWritten = socketChannel.write(buf);

Related Tutorials