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) {
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
c # udp
natli
source share