Socket Server buffer size

G

Guest

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
 
V

Vadym Stetsyak

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.

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.
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?

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.
 
W

William Stacey [MVP]

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]

| 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top