Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. TCP/IP Message Framing and Parsing [modified]

TCP/IP Message Framing and Parsing [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomsysadminhostingjson
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    staticv
    wrote on last edited by
    #1

    I want to send varying length application specific messages between my client and server application. The problem is how should I determine the length of message when receiving it? I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better? May I know why and how do I prefix it? Any code examples would be really appreciated. I'm using Winsocks (version 2.2, if that helps) And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP? Thanks

    Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

    modified on Wednesday, August 26, 2009 6:25 AM

    S 1 Reply Last reply
    0
    • S staticv

      I want to send varying length application specific messages between my client and server application. The problem is how should I determine the length of message when receiving it? I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better? May I know why and how do I prefix it? Any code examples would be really appreciated. I'm using Winsocks (version 2.2, if that helps) And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP? Thanks

      Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

      modified on Wednesday, August 26, 2009 6:25 AM

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Ahmed Manzoor wrote:

      I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better?

      Prefix the length. That gives you a definite message length, which lets you detect errors better. Say you had a termination string and the packet containing the termination string was lost. Your program would keep searching and either a) never see the terminator, so think the message never ended, or b) see the wrong terminator (maybe from the next message) and merge two messages into one.

      Ahmed Manzoor wrote:

      And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP?

      No minimum really - but you need to turn off the Nagle algorithm (see setsockopt and TCP_NODELAY[^]).

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      S 2 Replies Last reply
      0
      • S Stuart Dootson

        Ahmed Manzoor wrote:

        I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better?

        Prefix the length. That gives you a definite message length, which lets you detect errors better. Say you had a termination string and the packet containing the termination string was lost. Your program would keep searching and either a) never see the terminator, so think the message never ended, or b) see the wrong terminator (maybe from the next message) and merge two messages into one.

        Ahmed Manzoor wrote:

        And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP?

        No minimum really - but you need to turn off the Nagle algorithm (see setsockopt and TCP_NODELAY[^]).

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        S Offline
        S Offline
        staticv
        wrote on last edited by
        #3

        I don't understand what you mean by no minimum? Can you please explain?

        Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

        S M 2 Replies Last reply
        0
        • S staticv

          I don't understand what you mean by no minimum? Can you please explain?

          Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          If you turn off Nagling, then you can send a single byte in a TCP/IP packet. You can't get smaller than that (except 0, which is nothing).

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • S staticv

            I don't understand what you mean by no minimum? Can you please explain?

            Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

            M Offline
            M Offline
            Moak
            wrote on last edited by
            #5

            You can send one byte at a time. You do NOT need to turn off Nagle algorithm in order to be able to send single bytes. Beside using a length prefix or a terminator, there is also a third possibility. If you send only one message per TCP connection, then close the connection gracefully[^]. The receiving peer will know when it has received the complete message and can close the socket. However, if you want to send more then one message, the first two possibilities would be better.

            My webchat in Europe :java: (in 4K)

            1 Reply Last reply
            0
            • S Stuart Dootson

              Ahmed Manzoor wrote:

              I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better?

              Prefix the length. That gives you a definite message length, which lets you detect errors better. Say you had a termination string and the packet containing the termination string was lost. Your program would keep searching and either a) never see the terminator, so think the message never ended, or b) see the wrong terminator (maybe from the next message) and merge two messages into one.

              Ahmed Manzoor wrote:

              And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP?

              No minimum really - but you need to turn off the Nagle algorithm (see setsockopt and TCP_NODELAY[^]).

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              S Offline
              S Offline
              staticv
              wrote on last edited by
              #6

              Can you please provide an example? using the recv() and send() functions?

              Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups