Where is the data sent by UDP stored?

I had never used UDP before, so I let it go. To find out what would happen, I received the “server” data every half second, and the client received the data every 3 seconds. Therefore, despite the fact that the server sends data much faster than the client can receive, the client still receives it all neatly one after another.

Can someone explain why / how this happens? Where exactly is the data buffered?

Submit

class CSimpleSend { CSomeObjServer obj = new CSomeObjServer(); public CSimpleSend() { obj.changedVar = varUpdated; obj.threadedChangeSomeVar(); } private void varUpdated(int var) { string send = var.ToString(); byte[] packetData = System.Text.UTF8Encoding.UTF8.GetBytes(send); string ip = "127.0.0.1"; int port = 11000; IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ip), port); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); client.SendTo(packetData, ep); Console.WriteLine("Sent Message: " + send); Thread.Sleep(100); } } 

All CSomeObjServer is an increment of an integer per unit every half second

Get

 class CSimpleReceive { CSomeObjClient obj = new CSomeObjClient(); public Action<string> showMessage; Int32 port = 11000; UdpClient udpClient; public CSimpleReceive() { udpClient = new UdpClient(port); showMessage = Console.WriteLine; Thread t = new Thread(() => ReceiveMessage()); t.Start(); } private void ReceiveMessage() { while (true) { //Thread.Sleep(1000); IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, port); byte[] content = udpClient.Receive(ref remoteIPEndPoint); if (content.Length > 0) { string message = Encoding.UTF8.GetString(content); if (showMessage != null) showMessage("Recv:" + message); int var_out = -1; bool succ = Int32.TryParse(message, out var_out); if (succ) { obj.alterSomeVar(var_out); Console.WriteLine("Altered var to :" + var_out); } } Thread.Sleep(3000); } } } 

CSomeObjClient saves the variable and has one function (alterSomeVar) to update it

Ouput:

 Sent Message: 1 Recv:1 Altered var to :1 Sent Message: 2 Sent Message: 3 Sent Message: 4 Sent Message: 5 Recv:2 Altered var to :2 Sent Message: 6 Sent Message: 7 Sent Message: 8 Sent Message: 9 Sent Message: 10 Recv:3 Altered var to :3 
+8
c # udp
source share
2 answers

The operating system kernel supports separate send and receive buffers for each UDP and TCP socket. If you google SO_SNDBUF and SO_RCVBUF , you will find a lot of information about them.

When you send data, it is copied from your application space to the send buffer. From there, it is copied to the network interface card, and then to the wire. Reception side - reverse: NIC for receiving a buffer, where it waits until you read it. In addition, copies and buffering may also occur depending on the OS.

It is important to note that the sizes of these buffers can change dramatically. Some systems may by default only have 4 kilobytes, while others may have 2 megabytes. You can find the current size with getsockopt() with SO_SNDBUF or SO_RCVBUF and set it in the same way with setsockopt() . But many systems limit the size of the buffer, sometimes to arbitrarily small amounts. This is usually a kernel value, such as net.core.wmem_max or net.core.rmem_max , but the exact reference is system dependent.

Also note that setsockopt() may fail even if you request an amount less than the intended limit. To actually get the size you want, you need to call setsockopt() again, using decreasing amounts, until it finally succeeds.

The next page is the Technical Note of my company, which touches on this topic a bit and contains links for some common systems: http://www.dataexpedition.com/support/notes/tn0024.html

+3
source share

It seems to me that the UdpClient-Class provides a buffer for the received data. Try using the socket directly. You can also set ReceiveBufferSize sockets to be zero, although I believe that it is used only for TCP connections.

0
source share

All Articles