Socket Question

  • Thread starter Thread starter William Stacey
  • Start date Start date
W

William Stacey

You probably want to send in MTU size chunks (i.e. the lowest MTU in the
path). This can make IP more efficient at both ends. I would not suggest
building a 10MB byte array. The network stack breaks down into max datagram
buffers anyway, so larger then max datagram does not really help here.
 
I am writing a socket class that implements the System.Net and
System.Net.Sockets namespaces.

My Question is:

If I need to send a large amount of data through a socket connection (for
example 10MB) which option is better?


A) put all 10MB together in 1 Byte Array and pass it to the Send function

B) create small arrays with less data and send each one by one (which could
be the better size if you think that this option is the correct)

C) Another suggest?



Thanks a lot
 
TCP is stream based, which means, it doesn't really distinguish between one
big blob of 10MB or 10 blobs of 1MB each or even 1.05million blobs of 1 byte
each.

The reason why "B" might be a good idea is you will put lesser pressure on
your memory manager.

-vJ
 
there could also be a slowdown if you send too few bytes at once. I used to
have an upload app on c# that if I read one byte at a time from a file and
uploaded it it would run slow, then started tweaking it, sending 1024 bytes,
tehn 2048 (note the numbers dont have to be that, its just some common
numbers to try), I think up to 2048 I would see the speed increase after
that I wouldnt see it increase, it even decreased when I tried something
like 16384 or more.
 
B, definitely.

You can do something like "A", but it means you're allocating a big chunk of
memory, which is expensive (both in memory use and perf, since you will
flush your cache away when you traverse it). There's also the disadvantage
of having the receiver block on a read for a *long* time (could be 3 minutes
to send 10MB over a 10Mbps ethernet link (assuming I did my math
correctly)). You also might want to be able to restart the transfer if it
got interrupted for some reason.

Finally, the data is going to be chunked into smaller pieces when it goes
over the network anyway. My advice is to pick something reasonable
(something like 4K seems like a good starting point), and then benchmark to
see whether that's a good choice.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://blogs.gotdotnet.com/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,

AA said:
I am writing a socket class that implements the System.Net and
System.Net.Sockets namespaces.

My Question is:

If I need to send a large amount of data through a socket connection (for
example 10MB) which option is better?


A) put all 10MB together in 1 Byte Array and pass it to the Send function

B) create small arrays with less data and send each one by one (which could
be the better size if you think that this option is the correct)

Create one small array and reuse it for each send.

The maximum bytes you can send at once, is the size of the underlying send
buffer.

You can get the size of the send buffer by calling GetSocketOption with
SocketOptionName.SendBuffer. This is what TcpClient.SendBufferSize property
does.

I'm not sure but I think the default is something like 8k, but since you
don't need to match it exactly, commonly a chunk size of 1024 bytes is used.
The best performace will be when your chunk size <= send buffer size.

A 10MB byte buffer will never be send at once, so creating one large array
is a memory waste.

HTH
greetings
 
Back
Top