What are the extra bytes in TCP socket messages?

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
gregfjohnson
In need of some credit
In need of some credit
Posts: 3
Joined: Tue Apr 11, 2017 6:28 am

What are the extra bytes in TCP socket messages?

Post by gregfjohnson »

I would like to use wx sockets to interface to another external TCP server. I am using the wxLua wrapper for wxWidgets.

I used the simple client.wx.lua program, and set up a separate server that prints packets from clients.

When I send "XXXXYYYYZZZZ" from the wxWidgets client program, I get the following string at the TCP server:

\0xad\0xde\0xed\0xfe\0x0c\0x00\0x00\0x00XXXXYYYYZZZZ\0xed\0xfe\0xad\0xde\0x00\0x00\0x00\0x00

If I send "abc" I get the following string:

\0xad\0xde\0xed\0xfe\0x03\0x00\0x00\0x00abc\0xed\0xfe\0xad\0xde\0x00\0x00\0x00\0x00

What are the initial eight bytes and the final eight bytes in the above TCP messages?? It looks like byte 5 is the length of the actual message
sent.

Would it be easy or hard to update the external TCP server that I am trying to talk to so that it could exchange TCP data with wxSockets?

Is there any way to configure wxWidgets sockets so that I do not get the extra 8 bytes at the beginning and end of every TCP message?

Thanks,

Greg Johnson
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: What are the extra bytes in TCP socket messages?

Post by ONEEYEMAN »

Hi, Greg,
You need to talk to people that developed/maintain that TCP server.
We do not know what protocol it uses and how it communicates with the other world...

Thank you.
gregfjohnson
In need of some credit
In need of some credit
Posts: 3
Joined: Tue Apr 11, 2017 6:28 am

Re: What are the extra bytes in TCP socket messages?

Post by gregfjohnson »

I realized the problem. I was mistakenly using the method wxSocketBase::WriteMsg(). As the documentation notes:

"WriteMsg() sends a short header before the data so that ReadMsg() knows how much data should be actually read."

This "short header before the data" is what I was seeing when I sent a TCP message from inside wxWidgets to my external server.

(The problem was not with the external server, but rather it was the header data being added by wxWidgets in its WriteMsg() method.)

The solution was to use wxSocketBase::Write(). This way, wxWidgets just sends the bytes I give it, and does not add the header.

On the read side, wxSocketBase::Read(), I needed to attempt to read more bytes than the longest message I expect. wxWidgets
counter-intuitively returns that number, no matter how many bytes were actually read. After the Read(), you can call LastCount() to find
out how many byte really were received off the wire by the Read() operation.
Post Reply