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. Other Discussions
  3. IT & Infrastructure
  4. Receive complete message (network programming)

Receive complete message (network programming)

Scheduled Pinned Locked Moved IT & Infrastructure
questionsysadminalgorithmshelptutorial
3 Posts 2 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.
  • R Offline
    R Offline
    Rickard Andersson20
    wrote on last edited by
    #1

    I'm designing a protocol for my chatserver that I'm building. But I've run into a problem about receiving messages. How can I determine that the message was completely received? Let's say this is my message (an example only):

    MSG\r\n
    Content-Type: text/plain\r\n
    Extras: font=Arial; style=bold;\r\n\r\n
    This is where the body goes, after a double '\r\n' mark\r\n

    Now to my problem. Let's say I receive only 16 bytes at the time (in reality you don't do this, I know). This means I'll have to call recv() more than one time in this example. So, how can I determine when I've received the complete message? Should I put some ending mark like a tripple \r\n or something? Should I send the complete size of the message after MSG? Do you have any good algorithm to share? Another thing also when it's in mind: What if I receive the end of one message and the beginning of another at the same recv() call? Like this:

    fter a double '\r\n' mark\r\n
    MSG\r\n
    Content-Type: bla bla

    But what I understand from my book, in order to accokmplish that I have send the end and the beginning in the same buffer with send()? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318

    R M 2 Replies Last reply
    0
    • R Rickard Andersson20

      I'm designing a protocol for my chatserver that I'm building. But I've run into a problem about receiving messages. How can I determine that the message was completely received? Let's say this is my message (an example only):

      MSG\r\n
      Content-Type: text/plain\r\n
      Extras: font=Arial; style=bold;\r\n\r\n
      This is where the body goes, after a double '\r\n' mark\r\n

      Now to my problem. Let's say I receive only 16 bytes at the time (in reality you don't do this, I know). This means I'll have to call recv() more than one time in this example. So, how can I determine when I've received the complete message? Should I put some ending mark like a tripple \r\n or something? Should I send the complete size of the message after MSG? Do you have any good algorithm to share? Another thing also when it's in mind: What if I receive the end of one message and the beginning of another at the same recv() call? Like this:

      fter a double '\r\n' mark\r\n
      MSG\r\n
      Content-Type: bla bla

      But what I understand from my book, in order to accokmplish that I have send the end and the beginning in the same buffer with send()? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318

      R Offline
      R Offline
      Rickard Andersson20
      wrote on last edited by
      #2

      I think I have to send the WHOLE message and then also receive the WHOLE message. ElseI have to send the size as a field or something, I don't know... Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318

      1 Reply Last reply
      0
      • R Rickard Andersson20

        I'm designing a protocol for my chatserver that I'm building. But I've run into a problem about receiving messages. How can I determine that the message was completely received? Let's say this is my message (an example only):

        MSG\r\n
        Content-Type: text/plain\r\n
        Extras: font=Arial; style=bold;\r\n\r\n
        This is where the body goes, after a double '\r\n' mark\r\n

        Now to my problem. Let's say I receive only 16 bytes at the time (in reality you don't do this, I know). This means I'll have to call recv() more than one time in this example. So, how can I determine when I've received the complete message? Should I put some ending mark like a tripple \r\n or something? Should I send the complete size of the message after MSG? Do you have any good algorithm to share? Another thing also when it's in mind: What if I receive the end of one message and the beginning of another at the same recv() call? Like this:

        fter a double '\r\n' mark\r\n
        MSG\r\n
        Content-Type: bla bla

        But what I understand from my book, in order to accokmplish that I have send the end and the beginning in the same buffer with send()? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        Either send the length of message before message text (like HTTP does) or use a special mark at the end of the message (like SMTP does). Both approaches are proven to work well enough.

        In your second problem, you can't assume that blocks you send() match the blocks you receive() if you use TCP. The network is free to split or combine data as it sees fit.

        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