0

I created a ServerSocket and read messages from different clients. One of the clients insisted that my application does not behave correctly after they sent their messages. I managed to get a TCP trace and saw that they really sent their messages, but my server application did not read any data from the socket. I get a thread dump and everything seems to be ok there:

"Reader Thread" daemon prio=3 tid=0x0ae8a000 nid=0x999 runnable [0x14525000]
   java.lang.Thread.State: RUNNABLE
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:129)

What may be the reason that I could not read messages from the stream? Other clients do not suffer this kind of problem by the way.

private byte[] readByte(int readCount) throws Exception {
    int b;
    int readedCount = readCount;
    byte[] receiveBuffer = new byte[readCount];
    while( true ) {
        if( readCount > 0 ) {
            b = inputStream.read(receiveBuffer, readCount - readedCount, readedCount);
        } else {
            b = 0;
        }
        if( b < 0 ) {
            throw new UnrecoverableSocketException("Connection is Broken");
        } else {
            readedCount = readedCount - b;
        }
        if( readedCount == 0 ) {
            return receiveBuffer;
        }

    }
}
1
  • By the way, I can see TCP ACK messages in TCP dump Commented Aug 2, 2011 at 7:39

2 Answers 2

1

It's possible the readCount does not match how much data is actually available on the stream. If your client sent 10 bytes but you were expecting 12 your code would get stuck waiting for those two extra bytes.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, this is an implementation of SMPP protocol. In this protocol, client must sent a prefined number of bytes, which is header information. This header tells us the number of bytes whole message contains. So application tries to read the remaining bytes. I examined the TCP dump using wireshark, messages seems to be correct.
0

Most of your readByte() method could be implemented using DataInputStream.readFully(), and you would get the benefit of several million times the amount of testing you've done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.