How to calculate packet size?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3

Thread: How to calculate packet size?

  1. #1
    Join Date
    May 2012

    How to calculate packet size?


    I am receiving something back from a server using the following command:


    The RecvChar in this case is returning me 25 value and receivingbuff has 25 characters in it. This means 25 bytes of characters are received.

    But the packet will also have some header and other such stuff. How can I calculate the total size of packet including the 25 characters payload and the header etc.?

    Thank you.

  2. #2
    Join Date
    Nov 2000
    Voronezh, Russia

    Re: How to calculate packet size?

    Function recv is called on application level. There are no packets, "headers and other stuff" on application level. That's only payload byte stream there. You're not guaranteed to receive the sent data in same chunks as they were sent by client app (see this post). You cannot calculate anything about the data in receiving stream. What you can is reading bytes from stream to buffer. Function will block your program and wait for the next data to come.
    Last edited by Igor Vartanov; June 2nd, 2012 at 01:06 AM.
    Best regards,

  3. #3
    Join Date
    Nov 2002

    Re: How to calculate packet size?

    Assuming TCP (and not UDP), Igor is correct. All you get from TCP is a stream of bytes. It's up to you as the programmer to give meaning to those bytes.

    One way to give meaning to the bytes, so as to distinguish one "packet" from another, is to implement some sort of application-level protocol. And one simple protocol is for the sender to pre-pend each "packet" with a integer (four bytes) that tells the recipient how many bytes are in each "packet".

    For searching purposes, look for references that give advice on serializing and de-serializing data.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)