Thread: packet data

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    packet data

    Hello..

    How to send a packet that will have some kind of header with data length information, so server would know how big/long the data is?

    For example first 4 bytes would tell the data length..
    How to make that kind of string and how to read this kind of packet?

    Does anyone have a better idea for finding out packet length/size?

    Thanks for help

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    send size
    send data
    Code:
    read size
    if(sizeisvalid)
         read that many bytes;

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    What if it fails to get header (packet size), what should I do then?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Fail with an error message I guess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Salem
    Fail with an error message I guess.
    I wondered if it would be better to send data size together with other data or separately?

    Do you guys know of any example/source that uses that approach so I could take a look..

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You mean like HTTP, which somewhere in the header provides a content length.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by l2u
    I wondered if it would be better to send data size together with other data or separately?

    Do you guys know of any example/source that uses that approach so I could take a look..
    You mean to send size as an separate packet? better not. What you will do if the packet containing size will be lost? or will arrive later then the packet with the data?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> You mean to send size as an separate packet? better not. What you will do if the packet containing size will be lost? or will arrive later then the packet with the data?

    Well, we really can't assume anything as

    - Data in one 'send' is not guaranteed to just be sent as a 'packet'
    - He didn't specify what protocol he would be using

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Tonto
    >> You mean to send size as an separate packet? better not. What you will do if the packet containing size will be lost? or will arrive later then the packet with the data?

    Well, we really can't assume anything as

    - Data in one 'send' is not guaranteed to just be sent as a 'packet'
    - He didn't specify what protocol he would be using
    TCP/IP.

  10. #10
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    A summary of the contents of the internet header follows:


    0 1 2 3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |Version| IHL |Type of Service| Total Length |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Identification |Flags| Fragment Offset |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Time to Live | Protocol | Header Checksum |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Source Address |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Destination Address |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    | Options | Padding |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    Example Internet Datagram Header
    read rfc791

  11. #11
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Chances are the application isn't actually dealing with things at the packet level. IOCTL - FIONREAD returns the amount of data that can be read from a socket.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    TCP guarantees that no packets are lost or received out-of-order.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by CornedBee
    TCP guarantees that no packets are lost or received out-of-order.
    You sure?

  14. #14
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yes, but that doesn't mean that invalid data can't be sent, which you need to take measures to check for.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    630
    How should I check for invalid data? Lets say I send some data to the server.. The first four bytes are size which are not sent separately from 'actual data', but there is some invalid data. How should I know when a new packet arrives?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WinPCap - Packet data containing strings?
    By Glorfindel in forum C Programming
    Replies: 11
    Last Post: 02-11-2009, 03:34 PM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. Replies: 1
    Last Post: 07-31-2002, 11:35 AM