MessageHandler read message from socket from the middle and not from the beginning

175 Views Asked by At

My MessageHandler receive message from socket. First of all it reads header (code of method readMessageHeader() is in the end of the post). If header was read successfully, it reads message itself. The problem is that it starts reading header not from the beginning. Here on the picture example of the message that MassageHandler received. Header and message are highlighted with blue colour. HEADER_MESSAGE_SIZE is 20 bytes and it stores 5 integers. Header is fallowed by message. So header should be:

Var1 = 0x00000003

Var2 = 0x00000000

Var3 = 0x00000000

Var4 = 0x00000030

Var5 = 0x00000000

Message

But from some reason MessageHandler started reading from the last line (line 00a0), and when he read 11 bytes, he reached end, and continued reading from the beginning. So finally he read:

Var1 = 0x2D646973

Var2 = 0x636F6E6E

Var3 = 0x65637400 (656374 are last 3 bytes of message and 00 is first byte of the message)

Var4 = 0x00000300

Var5 = 0x00000000

MessageHandler finishes reading successfully and return truth.

What coud be a reason of such behaviour? How can I fix it?

private boolean readMessageHeader() throws Exception{

        try {
            byte msg_bytes[]  = new byte[HEADER_MESSAGE_SIZE];

            // Read in the bytes
            int offset = 0;
            int numRead = 0;
            while (offset < HEADER_MESSAGE_SIZE
                    && (numRead=dataInputStream.read(msg_bytes, offset, msg_bytes.length-offset)) >= 0) {
                offset += numRead;
            }

            // Ensure all the bytes have been read in
            if (offset < HEADER_MESSAGE_SIZE) {
                if (offset==0){
                    LogUtils.info(thisClass, "socket peer closed" );
                } else {
                    LogUtils.warn(thisClass, "Failed to read header from socket" );
                }
                if (socket!=null){
                    try {
                        socket.close();
                    } catch (IOException ex) {
                    }
                    socket=null;
                }
                return false;
            }

            if(LogUtils.isDebugEnabled(this.getClass())) LogUtils.debug(thisClass, "Got new message"); 

            DataInputStream reader = new DataInputStream( new ByteArrayInputStream(msg_bytes));         
            msgHead.readFromStream(reader);

            if(LogUtils.isDebugEnabled(this.getClass())) LogUtils.debug(thisClass, "message header: " + msgHead.toDebugString()); 

        } catch (IOException e) {
            LogUtils.warn(thisClass, "Failed read msg header:" + e.getMessage());
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException ex) {
                }
                socket=null;
            }
            return false;
        }

        return true;
    }

readMessageHeader() return truth.

0

There are 0 best solutions below