// shall I write some keywords here to boost search engine ranking?

Sunday, October 19, 2008

Java: Check if Socket Connection Broken

Most common ways of check if a socket connection to server was broken is by send a heartbeat message to server. However, for application that connect to server that do not support heardbeat, we can check connection by listen to the InputStream:

('is' refer to java.io.InputStream)


int bytesRead = is.read(receiveBuffer, 0, 1024);

// this is the trick to identify connection drop
if(bytesRead == -1){
throw new IOException("byteRead = -1, Connection closed.");
}

1 comment:

marin said...

thanks alot guy
it works for me properly