Thread: TCP/IP packet run-together-ing

  1. #1
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490

    TCP/IP packet run-together-ing

    I don't know exactly where to put this... So I'll play it safe and put it here. It's more networking related than C or C++.

    I'm writing a chat program for Flashdaddee. It uses TCP/IP to connect and send packets both ways. Every packet must be at most 500 bytes.


    My problem is that there are some places where packets run together. My send command sends the exact length of bytes needed, and my receive command receives up to 500 bytes, maximum.

    Most of the time, one packet is received as a whole, and the client program processes it like normal. But when I send one packet after another, with 2 seperate send commands, they are both received as one packet. The one sent second can be seen right after the first one.

    Can anyone here explain why this happens? Would it be practical to wait for acknowledgement from the receiving side each time a packet is sent? Is there another, simpler way I haven't thought of?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It's part of the TCP specification. It's a reliable byte protocol, it is not message based. The 'byte bunching' is actually neccesary for decent speed with things like telnet, in which multiple keypresses are sent in one packet to reduce the per packet overhead of TCP.

    You can get around this by having messages have their length as the first part of their data. That is, dedicate the first 2 bytes (You need two bytes, if your message sizes can vary by 1 byte and you need more than 256 bytes). Then when you read messages, read the first two bytes first to find the length of the message, and then read the next length bytes more (if available). You know that, after reading length bytes, the next 2 bytes will be the size of the next message.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Oh, I see. "It's not a bug, it's a feature."

    What if I terminate every message with one unique byte? Given that I don't use that packet format for data files.

    Another question: What amount of bytes is the maximum sent before I have to check the return values of the send and receive functions to make sure all bytes were sent?

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Terminating the messages with a unique byte will work.

    In order for the software to be reliable, you are going to have to keep on checking how result of send. It's possible, that in highly pathological cases, a send of one byte could block.

    I don't know if you've ever seen python before (even if not, you may be able to follow the little mysocket class defined in section 3) , but wrapping the socket into something that you can guarentee will always send as much as you tell it to is nice.

    http://www.amk.ca/python/howto/sockets/

    Basically, upon a send with your socket, you try to send the amount with the socket you are wrapping, then, if that doesn't send everything, you send the remainging portion again, and again, etc.

    If it turns out the network code is too slow, you can the slow portions of it.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    [EMOBA] recommended python to me earilier for doing fast and dirty server applications. The program I'm writing now already has encapsulated sockets, so fixing the problem shouldn't be too difficult.

    One more question: Is it possible to compile python? Flashdaddee needs a GUI client, and I'm thinking of using python's (semi)built in tkinter GUI capabilities. But I want to be able to distribute binaries for those who don't want to download the python interpreter.

  6. #6
    Unreg1
    Guest
    [url=http://pw1.netcom.com/~jsnader/]This book does a good job of explaining TCP/IP messaging. I doubt you want to buy it... but you can download the source code from this web site (the authors homepage). It should give you a good idea on how to do things.

  7. #7
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I am actually in the process of making a GUI application in python with Tkinter. It can be compiled into a binary that doesn't require a python installation on the user's part. So the user can download and double click the exe, and everything works. The package to be distributed is rather large (I think 4 MB uncompressed with Tkinter and a few other libraries).
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    eh... 4 MB? What about compressed? Flashdaddee has a 2 MB limit.

  9. #9
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    It probably hovers right around there, unfortunetely .

    You could possibly get it hosted elsewhere, or just not write it in Python.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Packet processing in Socket programming, please help
    By pumpkin in forum Networking/Device Communication
    Replies: 5
    Last Post: 05-28-2009, 01:33 AM
  2. Replies: 4
    Last Post: 05-05-2009, 05:35 AM
  3. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  4. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 03:04 AM
  5. Program to run in Dos only
    By ihsir in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2002, 06:16 PM