Archived

This topic is now archived and is closed to further replies.

sporty

Need help handling my TCP packets

Recommended Posts

FYI: I''m using Winsock. Okay I''m writing a client that connects to other clients via TCP/IP, and thus sends packets in that protocol. Each packet is setup like.. SIGNATURE TYPE_OF_PACKET BODY_INFORMATION For the sake of this post, lets pretend SIGNATURE should be "/msg-sig/", the packet type will be "1", and the body_information will be a list of files. The signature is always the same, it''s how the client knows that the packet is coming from another client using the same software. Easy enough, right? Well when my winsock retrieves data sent to it, it always checks the first X bytes to see if they equal the signature. If it''s valid, it then goes on to extract the TYPE_OF_PACKET, and the BODY_INFORMATION, and then based on the packet type, it parses the body and does whatever to it. This is all nothing new of course, and works fine and dandy. However, here''s my problem.. I just discovered how often times TCP/IP will not send all of my packet information in one packet..it might send it like..
  
[packet 1]
/msg-sig/
1
c:\hello.bmp
c:\folder\random.txt
c:\whatever\etc.txt

[packet 2]
c:\garbage.html
c:\windows\calc.exe
  
And thus, my program nicely reads in the first packet, parses the information, and the deal is done, but when the second packet arrives carrying the rest of the information, it contains no "header", and my winsock disregards it. How do I get around this? How do I get my winsock to know what to do with any more incoming packets, and to know exactly where to continue the process of parsing it?

Share this post


Link to post
Share on other sites
Try something like

[packet1]
/msg-sig/
TYPE_MSGSTART
MSGID

[packet2]
/msg-sig/
MSGID
DATA

[packet3]
/msg-sig/
MSGID
MOREDATA

...

[packet4]
/msg-sig/
TYPE_MSGEND
MSGID


packet1 signals start of data stream, packet2 & 3 are data, packet4 signals end.

Share this post


Link to post
Share on other sites
You simply want to do what LilMikey said, which is basically creating a buffer for every user (on the server) and when a packet comes in if there is no "packet terminator" at the end of the packet then you append this packet to that users buffer. You continue doing this until you receive a packet terminator from the user and then prepend the data thats in the buffer to the current packet and then parse it normally.

Share this post


Link to post
Share on other sites
Or just add a message length field to your message header and read in until you read that many bytes.

The problem is that TCP doesn''t preserve message boundaries, so one send could equal 1-x receives, or x sends could be received in a single call. It doesn''t necessarily mean 1 packet though.

The best way to handle this (IMHO) is to create some sort of buffer where you copy the data and remove a message from the stream at a time, placing the rest of what you receive on this buffer to be combined with your next receive to hopefully form another complete message, or be put back on the buffer to wait some more.

Share this post


Link to post
Share on other sites