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#
  4. TCP .Begin receive

TCP .Begin receive

Scheduled Pinned Locked Moved C#
csharpvisual-studiohelptutorialannouncement
5 Posts 4 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.
  • C Offline
    C Offline
    captainmogo
    wrote on last edited by
    #1

    I am trying to read a variable number of bytes from another .Net application but the messages are not always received correctly, sometimes I read the first half of the message and then the second half. Since the message sizes I am sending are constantly changing, I know I cannot hardcode the receive buffer. Right now that is how I have it and it works most of the time when my update rate is 1x/second but not at 10x/second. My data is a string with ids and data values (id:value|id:value|...) that I convert to bytes before sending (.GetBytes(dataString)). I keep reading on forums that the standard way of remedying this situation is to append the number of bytes to the front of the message but I cannot find any examples of how to do that. Some of the channels have a more data than others (40KB vs 500 bytes) but the sending routine is the same so I need to make sure it will work across all channels. Any help would be appreciated - I am really on a tight schedule thanks in advance

    L A P 3 Replies Last reply
    0
    • C captainmogo

      I am trying to read a variable number of bytes from another .Net application but the messages are not always received correctly, sometimes I read the first half of the message and then the second half. Since the message sizes I am sending are constantly changing, I know I cannot hardcode the receive buffer. Right now that is how I have it and it works most of the time when my update rate is 1x/second but not at 10x/second. My data is a string with ids and data values (id:value|id:value|...) that I convert to bytes before sending (.GetBytes(dataString)). I keep reading on forums that the standard way of remedying this situation is to append the number of bytes to the front of the message but I cannot find any examples of how to do that. Some of the channels have a more data than others (40KB vs 500 bytes) but the sending routine is the same so I need to make sure it will work across all channels. Any help would be appreciated - I am really on a tight schedule thanks in advance

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      captainmogo wrote:

      I keep reading on forums that the standard way of remedying this situation is to append the number of bytes to the front of the message

      Standard? I don't know if I would agree with that. In any event you are talking about a protocol[^].

      1 Reply Last reply
      0
      • C captainmogo

        I am trying to read a variable number of bytes from another .Net application but the messages are not always received correctly, sometimes I read the first half of the message and then the second half. Since the message sizes I am sending are constantly changing, I know I cannot hardcode the receive buffer. Right now that is how I have it and it works most of the time when my update rate is 1x/second but not at 10x/second. My data is a string with ids and data values (id:value|id:value|...) that I convert to bytes before sending (.GetBytes(dataString)). I keep reading on forums that the standard way of remedying this situation is to append the number of bytes to the front of the message but I cannot find any examples of how to do that. Some of the channels have a more data than others (40KB vs 500 bytes) but the sending routine is the same so I need to make sure it will work across all channels. Any help would be appreciated - I am really on a tight schedule thanks in advance

        A Offline
        A Offline
        Anthony Mushrow
        wrote on last edited by
        #3

        When i was sending / receiving stuff, i would send one message which contains the length of the peice of data, so the receiving end knows what to expect, and had the buffer at say 50KB. The sending end would send in 50Kb chunks until the end of the file, and the receiving end would receive until it had the full 50KB chunk (not all the data comes through in one big chunk) or until it reached the end of the file. It basically worked like this: Sender: 123456 <- the size in bytes Receiver: "OK"; Sender: *sends 50KB* Receiver: *keeps trying to receive until 50KB has been downloaded* Receiver: "OK"; Sender: *sends next 50KB* Receiver: *keeps trying to receive until 50KB has been downloaded* Receiver: "OK"; Sender: *sends last part of data (less than 50KB)* Receiver: *keeps trying to receive until the current amount of bytes == total amount of bytes* Receiver: "OK"; All done. I also had it send an MD5 hash, just to make sure nothing went wrong somewhere.

        My current favourite word is: Waffle Cheese is still good though.

        1 Reply Last reply
        0
        • C captainmogo

          I am trying to read a variable number of bytes from another .Net application but the messages are not always received correctly, sometimes I read the first half of the message and then the second half. Since the message sizes I am sending are constantly changing, I know I cannot hardcode the receive buffer. Right now that is how I have it and it works most of the time when my update rate is 1x/second but not at 10x/second. My data is a string with ids and data values (id:value|id:value|...) that I convert to bytes before sending (.GetBytes(dataString)). I keep reading on forums that the standard way of remedying this situation is to append the number of bytes to the front of the message but I cannot find any examples of how to do that. Some of the channels have a more data than others (40KB vs 500 bytes) but the sending routine is the same so I need to make sure it will work across all channels. Any help would be appreciated - I am really on a tight schedule thanks in advance

          P Offline
          P Offline
          pbraun
          wrote on last edited by
          #4

          If the buffer size is not hard coded, the process will have to retrieve one byte at a time from the socket and constantly resize the incoming buffer to accommodate the byte until the message has been received. That can be very time expensive. What can be done is on the receive side, set up a hard coded buffer size. Each time a message is received, pass that buffer up to another method that will parse the buffer copying it into a second buffer. Then the original buffer is cleared. When the next message is received, the same process happens again. Let the parsing method determine when the message is complete and further processing of the completed message is necessary. This requires some analysis of the message and how it is structured so that the receiving process can put the message back together correctly. There are many applications out there that do the second method effectively. In fact there are some examples on this web site of both methods.

          Phil

          C 1 Reply Last reply
          0
          • P pbraun

            If the buffer size is not hard coded, the process will have to retrieve one byte at a time from the socket and constantly resize the incoming buffer to accommodate the byte until the message has been received. That can be very time expensive. What can be done is on the receive side, set up a hard coded buffer size. Each time a message is received, pass that buffer up to another method that will parse the buffer copying it into a second buffer. Then the original buffer is cleared. When the next message is received, the same process happens again. Let the parsing method determine when the message is complete and further processing of the completed message is necessary. This requires some analysis of the message and how it is structured so that the receiving process can put the message back together correctly. There are many applications out there that do the second method effectively. In fact there are some examples on this web site of both methods.

            Phil

            C Offline
            C Offline
            captainmogo
            wrote on last edited by
            #5

            I have been mia for the past few weeks but I am finally getting back to it. I understand the concept of your post but I am not really sure how to start implementing that. Would there be some sort of EOF char to determine the end of the message and then only pull out of the buffer up to that point. Would it be possible to maybe get me started on that. Thanks a lot

            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