Socket Server buffer size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Macca

    #1

    Socket Server buffer size

    Hi,

    My application uses an asynchronous socket server. The question I have is
    what i should set my socket server buffer size to.

    I will know the size of each data packet sent across the network and was
    considering setting the buffer size to this.

    In the examples I have seen on the net the buffer size is usually set to
    1024 bytes and the socket is continually read until all data is received.

    I'd like to avoid repeatedly reading socket to get full data packet since I
    will know how big the packet will be in advance.

    My data will be bigger than this e.g 10KB but will this buffer size impact
    on performance and therefore be better to use a smaller buffer and read the
    socket repeatedly?

    I'd appreciate any advice.

    Thanks In Advance
    Macca
  • Vadym Stetsyak

    #2
    Re: Socket Server buffer size

    > In the examples I have seen on the net the buffer size is usually set to[color=blue]
    > 1024 bytes and the socket is continually read until all data is received.
    >
    > I'd like to avoid repeatedly reading socket to get full data packet since
    > I
    > will know how big the packet will be in advance.[/color]

    There is no guarantee that you will receive all the data via one Receive
    call.
    Especially if you have large data chunk, you may not receive whole data.
    [color=blue]
    > My data will be bigger than this e.g 10KB but will this buffer size impact
    > on performance and therefore be better to use a smaller buffer and read
    > the
    > socket repeatedly?[/color]

    You can allocate 10 Kb of memory to store received data, and small buffer
    that
    will be used in the Receive method. After each receive you'll be appending
    data.


    --
    Regards, Vadym Stetsyak
    www: http://vadmyst.blogspot.com


    Comment

    • William Stacey [MVP]

      #3
      Re: Socket Server buffer size

      Your receives can get 0, or 1 or more bytes. 0 means socket closed by client
      side. So you have a two step process here:
      1) Receive 4 bytes for len (or what ever your len type is). Still have to
      loop until 4 bytes.
      2) Recive Len bytes in your buffer until you get Len bytes in your loop.

      I would leverage the same helper method for both. Something like "public
      byte[] FillBuffer(int size){//Get size bytes from socket}"

      --
      William Stacey [MVP]

      "Macca" <Macca@discussi ons.microsoft.c om> wrote in message
      news:C5D7AC3B-0D08-4BD5-9DC6-3743F3C103C7@mi crosoft.com...
      | Hi,
      |
      | My application uses an asynchronous socket server. The question I have is
      | what i should set my socket server buffer size to.
      |
      | I will know the size of each data packet sent across the network and was
      | considering setting the buffer size to this.
      |
      | In the examples I have seen on the net the buffer size is usually set to
      | 1024 bytes and the socket is continually read until all data is received.
      |
      | I'd like to avoid repeatedly reading socket to get full data packet since
      I
      | will know how big the packet will be in advance.
      |
      | My data will be bigger than this e.g 10KB but will this buffer size impact
      | on performance and therefore be better to use a smaller buffer and read
      the
      | socket repeatedly?
      |
      | I'd appreciate any advice.
      |
      | Thanks In Advance
      | Macca


      Comment

      Working...