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. what to do when message size is bigger than the byte[] size in Socket.BeginReceive?

what to do when message size is bigger than the byte[] size in Socket.BeginReceive?

Scheduled Pinned Locked Moved C#
question
8 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.
  • P Offline
    P Offline
    Pr teek B h
    wrote on last edited by
    #1

    lets say you set an initial byte[] size to 30 and the message that you receive has a size of 50. How would you handle that with Socket.BeginReceive With Socket.Receive this would work:

    byte[] buffer = new byte[30];
    int size = Socket.Receive(buffer);
    String msg = Encoding.ASCII.GetString(buffer, 0, size);

    while(size > 0)
    {
    buffer = new byte[30];
    size = Socket.Receive(buffer);
    msg = msg + Encoding.ASCII.GetString(buffer, 0, size);
    }

    How would you do the same thing with Socket.BeginReceive? Thank you, Prateek

    E P 2 Replies Last reply
    0
    • P Pr teek B h

      lets say you set an initial byte[] size to 30 and the message that you receive has a size of 50. How would you handle that with Socket.BeginReceive With Socket.Receive this would work:

      byte[] buffer = new byte[30];
      int size = Socket.Receive(buffer);
      String msg = Encoding.ASCII.GetString(buffer, 0, size);

      while(size > 0)
      {
      buffer = new byte[30];
      size = Socket.Receive(buffer);
      msg = msg + Encoding.ASCII.GetString(buffer, 0, size);
      }

      How would you do the same thing with Socket.BeginReceive? Thank you, Prateek

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

      use the overloaded receive method that takes in an offset, and a length to read. Also, don't redeclare the buffer as it is unnecessary.

      int read = 0;
      byte[] buffer = new byte[255];
      while( ( read = socket.Receive(buffer, 0, buffer.Length)) != 0){

      ... Encoding.ASCII.GetString(buffer, 0, read);

      }

      Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
      Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
      Most of this sig is for Google, not ego.

      P 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        use the overloaded receive method that takes in an offset, and a length to read. Also, don't redeclare the buffer as it is unnecessary.

        int read = 0;
        byte[] buffer = new byte[255];
        while( ( read = socket.Receive(buffer, 0, buffer.Length)) != 0){

        ... Encoding.ASCII.GetString(buffer, 0, read);

        }

        Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
        Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
        Most of this sig is for Google, not ego.

        P Offline
        P Offline
        Pr teek B h
        wrote on last edited by
        #3

        Thank you for your suggestion... i know how to do it with Socket.Receive. I wanted to do this with Socket.BeginReceive. The problem is in order to get the first part of the msg, you'll have to use Socket.EndReceive and then how will you get the rest of the msg?

        E 1 Reply Last reply
        0
        • P Pr teek B h

          Thank you for your suggestion... i know how to do it with Socket.Receive. I wanted to do this with Socket.BeginReceive. The problem is in order to get the first part of the msg, you'll have to use Socket.EndReceive and then how will you get the rest of the msg?

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

          Begin Receive contains a similar overload as receive. The logic is the exact same, except in the callback method you must call BeginRecieve again after reading all of the bytes.

          Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
          Most of this sig is for Google, not ego.

          P 1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            Begin Receive contains a similar overload as receive. The logic is the exact same, except in the callback method you must call BeginRecieve again after reading all of the bytes.

            Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
            Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
            Most of this sig is for Google, not ego.

            P Offline
            P Offline
            Pr teek B h
            wrote on last edited by
            #5

            so just for clearification the code would be something like this: int size = socket.EndReceive(iasyncresult); while(size > 0) socket.BeginReceive(...);

            E 1 Reply Last reply
            0
            • P Pr teek B h

              so just for clearification the code would be something like this: int size = socket.EndReceive(iasyncresult); while(size > 0) socket.BeginReceive(...);

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

              no. read the MSDN.

              Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
              Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
              Most of this sig is for Google, not ego.

              P 1 Reply Last reply
              0
              • E Ennis Ray Lynch Jr

                no. read the MSDN.

                Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
                Most of this sig is for Google, not ego.

                P Offline
                P Offline
                Pr teek B h
                wrote on last edited by
                #7

                ok. Thanks for your help :)

                1 Reply Last reply
                0
                • P Pr teek B h

                  lets say you set an initial byte[] size to 30 and the message that you receive has a size of 50. How would you handle that with Socket.BeginReceive With Socket.Receive this would work:

                  byte[] buffer = new byte[30];
                  int size = Socket.Receive(buffer);
                  String msg = Encoding.ASCII.GetString(buffer, 0, size);

                  while(size > 0)
                  {
                  buffer = new byte[30];
                  size = Socket.Receive(buffer);
                  msg = msg + Encoding.ASCII.GetString(buffer, 0, size);
                  }

                  How would you do the same thing with Socket.BeginReceive? Thank you, Prateek

                  P Offline
                  P Offline
                  Pr teek B h
                  wrote on last edited by
                  #8

                  If anybody is looking for the answer, do check this link [^] out. Also, be careful with the ReceiveTimeouts. ReceiveTimeout is for synchrounous receive only. So, if you don't want your loop to get stuck, make sure you include some way of identifying the end of message. Good Luck, Prateek

                  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