Actual Size of UDP Packets

O

O.B.

I have two sockets:

Socket tcpSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);

Socket udpSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);

The BeginReceiveFrom operation is invoked with a buffer of 1500 bytes
(the MTU size). Example:
byte[] buffer = new byte[1500];
udpSocket.BeginReceive(buffer, 0, 1500, SocketFlags.None,
new AsyncCallback(ReceiveDataCallback), null);

When configuring a C# Socket class as a TCP socket, I have noticed
that each time the BeginReceiveFrom operation is invoked, it will
stuff as much data as possible into the specified "buffer." However,
when it is configured as a UDP socket, it only puts one packet into
the "buffer" each time the operation is invoked ... regardless of if
the "available" buffer if full.

So this brings about many questions.

First, when I send 1000 packets that are 144 bytes long through the
UDP socket, is 144,000 bytes sent? Or are 144 * 1500 (MTU Size) bytes
sent?

If only 144,000 bytes are sent, why does the BeginReceiveFrom
operation only populate one packet into the buffer per call to the
BeginReceiveFrom operation?
 
O

O.B.

[...]
So this brings about many questions.

Are you sure?  You only posted two.  :)
First, when I send 1000 packets that are 144 bytes long through the
UDP socket, is 144,000 bytes sent?  Or are 144 * 1500 (MTU Size) bytes
sent?

The former is closer to the truth.  In reality, there's overhead per  
datagram, so more than 144,000 bytes are sent.  But, it's nowhere near  
216,000 bytes.  "MTU" is "maximum transmission unit".  It's the most data  
in a transmission unit, but that doesn't mean a transmission unit is  
required to always be that large.
If only 144,000 bytes are sent, why does the BeginReceiveFrom
operation only populate one packet into the buffer per call to the
BeginReceiveFrom operation?

Because "UDP" means "User Datagram Protocol" and by convention, you only  
receive one datagram at a time.  That's how the receiver knows how long 
the datagram is.

TCP is a stream-oriented protocol, with no data boundaries implicit in the  
protocol.  Thus, bytes are coalesced when possible for more efficient  
transmission.  It's not even correct think of a "packet" transmission unit  
when discussing TCP.  At the application level, a byte is the transmission  
unit for TCP.  When receiving, you can receive any number of bytes, from 1  
up to the total number that has been sent so far but not yet read from the  
socket (or of course the buffer passed in, whichever is smaller).

Pete

Thanks Pete, this amount of detail was exactly what I needed.

-Obie
 

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

Similar Threads


Top