[jdev] TCP Packet Construction

Jon Phillips jon at rejon.org
Fri Nov 5 02:06:09 CST 2004


First of all thanks for your wonderful insight!

Heiner Wolf wrote:
> Hi,
> 
> 
>>I interested to know if there are any good resources, documents, or 
>>source code I could look at to see how to properly receive 
>>packets from a jabber server like jabberd (which I am using) via TCP and 
>>then how to receive and reconstruct the entire packet.
> 
> 
> When you deal with TCP as in the case of Jabber, then you must not think in terms of 'packets'. Both parties will receive a stream of bytes. While they send chunks of data on the connection, there is no guarantee that the receiver gets the data in the original chunks. The only guarantees that TCP gives are 1. that the data will arrive or you get an error 2. the byte order will be retained. Again: the receiver might get the data in totally different 'packets'. 
> 
> 
>>I'm using Visual Studio .net and WINSOCK to do connection and 
>>receiving. Right now I have a working client that receive packets that 
>>are 8129 bytes, but these packets might not contain the full message. 
>>This problem gets worse with more traffic.
> 
> 
> What you must do is: forward every piece of data to the protocol parser. In this case it is an XML parser. The XML parser must be able to parse 'incomplete' XML and tell you about the structure before the main wrapper tag is closed. People call it a SAX parser as opposed to DOM parsers. You need a SAX parser. You feed it with every byte that comes from the connection. There is virtually nothing between the read() system call where you get data from the connection and the parse() where you forward the data to the parser. The parser will consume the bytes. The parser will tell you when a first order tag is finished. That would be something like a <message/> or <presence/> inside a <stream:stream>. If this happens then you can analyze the first order tag. That's then called a stanza in Jabber terms. In XML terms it is just the first order sub tag of the XML wrapper tag.

What parser would you recommend? I'm looking into expat. I was using 
tinyXML, but it doesn't seem to fit the bill you are describing...

> 
> 
>>What is the best way to construct entire jabber server packets into 
>>comlete packets/message. For example, the server might send a user's 
>>message in xml, and because of web traffic the complete message might 
>>not arrive in teh received 8192 bytes...
> 
> 
> I reiterate, because this is important: this is TCP. There are no messages. Your network API will give you multiple bytes at once. This looks like a message. You don't care. You handle all data to the XML parser. The parser will tell you when stanzas are complete. You are probably testing in your lab. Over the wide internet you might even get smaller pieces of data at once (like 500 bytes). Stanzas might be split badly. There might be even the last '>' missing. Don't look at the data directly. Let the XML parser look. It will tell you when a stanza is complete. 
> 
> 
>>Other than this problem, my custom jabber client for this game, 
>>gopetslive.com is going pretty well.
>>
>>Any help or code sample or pointers would be much appreciated.
> 
> 
> hw
> --
> Dr. Klaus H. Wolf
> bluehands GmbH & Co.mmunication KG
> http://www.bluehands.de/people/hw
> +49 (0721) 16108 75
> --
> Jabber enabled Virtual Presence on the Web: http://www.lluna.de/
> Open Source Future History: http://www.galactic-developments.com/
> _______________________________________________
> jdev mailing list
> jdev at jabber.org
> http://mail.jabber.org/mailman/listinfo/jdev
> 
> 

-- 
Jon Phillips

KOREA.PH.010.3140.7483
USA.PH.858.361.2811
jon at rejon.org
http://www.rejon.org

Inkscape (http://inkscape.org)
Open Clip Art Library (www.openclipart.org)
CVS Book (http://cvsbook.ucsd.edu)
Scale Journal (http://scale.ucsd.edu)



More information about the JDev mailing list