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. Asynchronous socket question

Asynchronous socket question

Scheduled Pinned Locked Moved C#
questioncsharp
4 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.
  • M Offline
    M Offline
    Madmaximus
    wrote on last edited by
    #1

    Hi all, I have a continuous stream of packet data going through my asynchronous socket connection. My question is how can I tell in the EndReceive callback method where one package of data ends and another begins? I coded the socket class in C# 1.1. Thanks in an advance. :) Ken

    T R 2 Replies Last reply
    0
    • M Madmaximus

      Hi all, I have a continuous stream of packet data going through my asynchronous socket connection. My question is how can I tell in the EndReceive callback method where one package of data ends and another begins? I coded the socket class in C# 1.1. Thanks in an advance. :) Ken

      T Offline
      T Offline
      Tim Kohler
      wrote on last edited by
      #2

      You have to have a delimiter in the data. And once you read in the delimiter in the callback, you'll know you got to the end of one message/stream and you're now reading for the next message/stream. So a very simple protocol is needed to delimit your messages. You have to make sure you read all the message as it may/may not (certainly not guaranteed) to be send in a single socket.send.

      E 1 Reply Last reply
      0
      • T Tim Kohler

        You have to have a delimiter in the data. And once you read in the delimiter in the callback, you'll know you got to the end of one message/stream and you're now reading for the next message/stream. So a very simple protocol is needed to delimit your messages. You have to make sure you read all the message as it may/may not (certainly not guaranteed) to be send in a single socket.send.

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        I like to prefix my messages with a length rather than delimit them.

        A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

        1 Reply Last reply
        0
        • M Madmaximus

          Hi all, I have a continuous stream of packet data going through my asynchronous socket connection. My question is how can I tell in the EndReceive callback method where one package of data ends and another begins? I coded the socket class in C# 1.1. Thanks in an advance. :) Ken

          R Offline
          R Offline
          RizwanSharp
          wrote on last edited by
          #4

          The more appealing and real world solution for all types of data transfer is to prefix each messaeg with its length. Procedure is as follow. Sending Part: 1) Calculate the Number of bytes produced by a message. 2) Store length as an integer value. 3) Convert the Length (integer) into bytes using BitConverter.GetBytes(messageLength) method. 4) Send these bytes on stream. 5) Send the original message. Receiving Part: 1) Read 4 bytes from the stream Asynchronously. 2) Convert these bytes into integer value using BitConverter.ToInt32(buffer,0) 3) Now creat a Memory Stream and loop on it untill that message size is met. Its the best technique in my knowledge and supports in Network delay situation for other type of data (Not String) If you have further queries, feel free to conact me ;) rizwansharp@hotmail.com Best Regards, Rizwan

          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