1

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 );
                }
            }
        }
    }
LPD
  • 2,833
  • 2
  • 29
  • 48
  • Please post your code. –  Aug 11 '15 at 05:50
  • 1
    if client side does not explicitly close the socket, then the server side keeps its socket opened until timeout expires. and vice versa. it's a feature of tcp – mangusta Aug 11 '15 at 05:53
  • What would cause what? Define 'sudden drop in connection'. What exactly are the symptoms of that? – user207421 Aug 11 '15 at 06:36
  • I do not have the client code. It is a vendor code. I have posted my server code. Thanks for your replies. Sudden drop in connection is, client is not able to send data to my server, but is able to send once the data once client is restarted. I want to confirm that it is not my code causing that error. – LPD Aug 11 '15 at 07:29
  • Try capturing network traffic on both ends of the connection with tcpdump / wireshark. This will tell you a lot about what is happening. – jrudolph Aug 11 '15 at 07:37
  • @EJP, I mean `netstat` command in the client shows no information on the system, whereas in my server it continues to show tcp 0 0 :::15000 :::* LISTEN tcp 0 0 ::ffff::15000 ::ffff::58961 ESTABLISHED – LPD Aug 11 '15 at 08:28
  • How does that relate to 'not able to send'? What happens instead? – user207421 Aug 11 '15 at 08:29
  • The packets are queued up at client. And unless the vendor restarts the client, the connection is not established and no data received. I want to confirm that my code above will not be the reason for that. thanks for your patience. – LPD Aug 11 '15 at 08:36
  • How do you *know* the packets are queued up at the client? **What are the symptoms?** Two hours since I first asked that. – user207421 Aug 11 '15 at 08:38
  • That is because once the vendor restarts the client, all the data that was due (during the time of interruption) is received by the server. I can confirm that much. There are no symptoms apart from this, as I do not see any errors etc in my log files. – LPD Aug 11 '15 at 08:58
  • That can only mean that the data was sitting in some *database* or *file* of the client. Not 'queued up'. Your question doesn't really make sense, and your refusal to provide actual symptoms and behaviour has not helped. – user207421 Aug 11 '15 at 09:47

0 Answers0