PDA

View Full Version : Reading TCP datagram header by Qt



Kill3rReaper
3rd June 2010, 02:20
Hey!!
Is there way to read header of TCP or UDP datagram by Qt?
I can read only the data, which is sending to me (without header).

Sorry for my english and thanks in advance for help!

tbscope
3rd June 2010, 05:48
Not directly.
Take a look at the code of QTcpSocket or QUdpSocket, maybe it will give you more information.
You might even want to subclass those and expose some public functions to get the headers (if possible of course, check the source first)

tbscope
3rd June 2010, 07:02
Scratch the post above.
Maybe take a look at QNativeSocketEngine, it might give you what you want.
It's not documented though.

squidge
3rd June 2010, 08:54
Applications typically don't see the header. Thats handled by the OS.

However, you can use libpcap to get such information portably.

Kill3rReaper
3rd June 2010, 13:17
fatjuicymole, I think you're right. I'm trying to write a XMPP client (Jabber), and I don't know when packet is fragmented, and when it is whole. Size of packet dependents of MTU. I can't weather with it...

tbscope
3rd June 2010, 15:17
For what you want to do, you don't need to go to the TCP level.
You use the TCP connection via a QTcpSocket

See this document:
http://www.ietf.org/rfc/rfc3920.txt

Kill3rReaper
3rd June 2010, 16:28
Exactly that I doing. I'm using QTcpSocket.
For example: someone sending to me message (4000 chars = 4000 bytes). He sending it in one message, but I receive 3 packets (size of one packet depends for MTU; for example MTU = 1500 - most popular). I have got:
1: 1500 bytes
2: 1500 bytes
3: 1000 bytes
The size of each one is very often changeable, and in different places (routers for example) MTU can be different. I never know how large is packet and when message is complete.
I hope, you're understanding what I mean.

tbscope
3rd June 2010, 16:34
(4000 chars = 4000 bytes).
Not always true! Check encodings!


He sending it in one message, but I receive 3 packets (size of one packet depends for MTU; for example MTU = 1500 - most popular). I have got:
1: 1500 bytes
2: 1500 bytes
3: 1000 bytes
The size of each one is very often changeable, and in very places (routers for example) MTU can be different. I never know how large is packet and when message is complete.
I hope, you're understanding what I mean.
I do, and no, you're not going to find TCP network infrastructure that sends you a gigabyte in one single packet. That would be silly.
So, what to do...

You need to receive a single file or message but it comes in several packets.
A good protocol tells you when the data ends. In HTML for example, the size of the body is mentioned in the headers. In NNTP, a multiline message ends with \r\n.\r\n.
So, find in the protocol you want to use how the server tells the clients that a specific request has ended.

Kill3rReaper
3rd June 2010, 17:02
Not always true! Check encodings!
I know that - it is wrote in simplification ;)

A protocol of XMPP/Jabber looks like that:


<message from="someone@jabber.org" to="someone2@jabber.org">
<body>
Content - message from user to user.
</body>
</message>


It looks OK, but what when user writes some of XML code...


<message from="someone@jabber.org" to="someone2@jabber.org">
<body>
Content - message from user to user. [and a lot of strings, and now message have got 1500 bytes]
</body> <--- user sends text, and he wrote to his friend a XML code like that
</message>
</body>
</message>


Understand what I mean?

tbscope
3rd June 2010, 17:12
In this case, it is very simple.

When reading from the socket, keep collecting the data. Everytime you receive data, check for "</message>" as that is the end of a message.
When you receive "</message>" you can start again waiting for and reading data.

In pseudocode:


slotReadyRead()
{
data += readAll();

if (data.endsWidht("</message>")
{
doSomethingUsefullWith(data);
data = "";
}
}

wysota
3rd June 2010, 17:23
fatjuicymole, I think you're right. I'm trying to write a XMPP client (Jabber), and I don't know when packet is fragmented, and when it is whole. Size of packet dependents of MTU. I can't weather with it...

Fragmentation has nothing to do with the situation you describe. Fragmentation is a situation where a data segment (or packet, I don't remember whether fragmentation is done on transport or network layer) is transfered from a network segment with a larger MTU to a segment with a smaller MTU and the payload needs to be cut into pieces to fit into frames (and can later be reassembled when the frame size increases). What you are fighting against is data window size which is indeed dependant on the implementation of TCP (and its extensions) and data congestion in each of the network segments the data is travelling through. But still this has nothing to do with "messages" sent by the transmitter because TCP doesn't operate in terms of messages or datagrams. It is a constant flow (stream) of bytes. Its speed (flow) is regulated using buffers on both ends of transmission and window size that changes based on congestion. "Messages" are interpreted on the application level and that's all you have to deal with. With XMPP it is very easy as it is an xml based protocol - just use QXmlStreamReader and feed it with data as it comes in.

Kill3rReaper
8th June 2010, 13:49
Hey!
I did it at last :D
I didn't know that, a XMPP message contains HTML entities, so solution by tbscope works.
Thank you all!!