There is a simple TCP Client Server processes working on the system.
Occasionally there is a sudden drop in connection on the client side, but the server machine continues to show the connection as ESTABLISHED.
The data transfer resumes only when the client machine is restarted.
What would cause this behaviour?
I do not have access to client code. Below is my server code :
public void startWorking() {
ServerSocket server = null;
try {
server = new ServerSocket( portNo );
logger.info( "Server started" );
while ( true ) {
final Socket socket = server.accept();
new MessageHandler( socket );
}
}
catch ( Exception e ) {
logger.error( "something went wrong here!!!", e );
}
finally {
try {
server.close();
}
catch ( IOException e ) {
logger.error( "something went wrong here!!!", e );
}
}
}
class MessageHandler extends Thread {
private Socket socket;
public MessageHandler( Socket socket ) throws IOException {
this.socket = socket;
start();
}
@Override
public void run() {
logger.info( "Connected on ----> " + socket );
try {
logger.info( "Fetching input stream...." );
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1];
logger.info( "Reading data...." );
int pipeCount = 0;
while ( inputStream.read( b ) != -1 ) {
bos.write( b );
if ( ( char ) b[0] == '|' ) {
pipeCount++;
}
if ( pipeCount == 4 ) {
syso( bos.toByteArray() );
bos.reset();
pipeCount = 0;
}
}
logger.info( "Finished reading on socket ----> " + socket );
}
catch ( Exception e ) {
logger.error( "something went wrong here!!!", e );
}
finally {
try {
socket.close();
}
catch ( IOException e ) {
logger.error( "something went wrong here!!!", e );
}
}
}
}