I’ve been developing a unity script for a few days now (completely new to unity/C#, but I’ve been programming for around 10 years so learning wasn’t too difficult, but I could be making a simple mistake).
The script itself has a flow, where ideally I’d like to continuously receive UDP packets over the lifetime of the application. I’m currently using UdpClient from the C# std lib, but the .Receive method seems to only ever receive a single packet on startup, and just hangs for the rest of the time.
I have verified that there is infact a continuous stream of packets, through another nodejs application which simply prints the packets to the console. These packets are sent at 150 hertz, so I’m not sure if I’m overloading it, but I’ve even tried a Thread.Sleep(1000) and it still happens. It also seems to only be receiving the packet at the very start, so e.g. if I do a Thread.Sleep before the first .Receive call, it won’t receive anything.
In the screenshot below, the output will be the BEFORE RECEIVE log gets printed twice, with the RECEIVED ... log in between those two.
Another thing I’ve noticed is that it sometimes doesn’t receive anything at all, but this happens very inconsistently (possibly due to a race condition).
I’m completely stumped as to why this is happening and why it’s not working. I’ve tried many different ways, e.g. a BeginReceive/EndReceive pair, ReceiveAsync, but nothing has worked and it has always led to the same issue. Does anyone have any suggestions for alternative solutions/ideas as to why this isn’t working?
Have you actually tried to wrap your while loop in a try catch block to see if an exception is thrown? Exceptions in your own background threads usually don’t end up in the console since Unity has nothing to do with them.
So your thread probably just dies silently.
Why exactly do you use Connect? You do know that UDP is not a connection based protocol. When you use Connect you only accept packets from that specific peer. You use the same port for the local and remote port. So are you sure that the sending side does the same? The port in the constructor of the UdpClient is your own local port. When you use Connect, you limit the receiving to only this endpoint. So the sender has to use the same local port.
You probably should remove the Connect call or if you do, you probably should use different ports. You can not bind two sockets to the same endpoint. So if both sender and receiver are both on the same machine, you can not have two sockets both bound to the same port.
Keep in mind that an UDP packet has a local and remote port. The remote port is the destination port where the packet is send to. The local / source port is the port of the sender which may be used by the receiver to “answer” the sender. Though at no point is a “connection” established. UDP in general is a fire-and-forget protocol. The source port is just a “return address” and when both are on the same machine, they must be different.
Thanks for the detailed response! I really appreciate it. I’m only getting back to this project today - sorry on the late reply.
I have just tried using a try catch around the main part of the code (lines 15 - 18 in the original screenshot) but it seems like there’s no exceptions going on.
Also my setup is there is another application on my localhost, where I’m trying to communicate some data across UDP on port 7004 so I assume that distinction doesn’t matter here? But thanks, it’s good to know about.
I have also just removed that connect call and it now works! I had made an assumption it always needed to connect, but looks like that isn’t the case. Thanks so much for the suggestions.