I’m writing my own UDP broadcast code using System.Net and System.Net.Sockets. The following code works fine in the OS X builds ( as well as in the editor on OS X), it’s also fine on Android, but the windows (both 32 and 64 bit) builds are very sluggish in performance, as if the UDP broadcasts are queueing up. Then the application can’t be terminated normally and must be killed. The longer the app is running the worse it gets although task manager does not show the app to be consuming excessive (or growing) amounts of memory, so I 'm not sure where it gets bogged down.
I call StartListeningToUDPBroadcast first to initiate listening for the broadcast,
// StartListeningToUDPBroadcast
public void StartListeningForUDPBroadcast () {
Debug.Log("StartListeningForUDPBroadcast called");
UdpClient udpClient = new UdpClient(22345);
udpClient.BeginReceive(new AsyncCallback(ReceiveDataCallback), udpClient);
}
// ReceiveDataCallback
private void ReceiveDataCallback(IAsyncResult ar) {
string receiveBytes = "";
UdpClient udp = (UdpClient)ar.AsyncState;
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, portnum);
if (udp != null) {
byte[] receive = udp.EndReceive(ar, ref ipEndPoint);
receiveBytes = Encoding.ASCII.GetString(receive);
}
Debug.Log("Received this: " + receivedBytes;
//Restart the listener
udp.BeginReceive(new AsyncCallback(ReceiveDataCallback), udp);
}
Are there any gross problems/omissions or is this perhaps a bug?
If I comment out the StartListeningToUDPBroadcast call in the windows build, it doesn’t go sluggish (but of course doesn’t receive any broadcasts either).
Cheers
- Balt