I want to receive a UDP message which was broadcasted to 255.255.255.255 with a UdpClient
within Unity.
But whatever combination of settings I try, it only receives a message, if it was sent from localhost.
I have tried fitted example code from these resources, non worked:
- https://gist.github.com/michaelosthege/857acac92b8ee689a6bb30d5bf23d9f6
- C# UDP Broadcast and receive example
- UdpClient receive on broadcast address
- How to do Network discovery using UDP broadcast
I'm running the Code below a task.
private void Listen()
{
udpClient = new UdpClient(9000);
//udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, 9000));
//udpClient.EnableBroadcast = true;
//udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//udpClient.ExclusiveAddressUse = false;
//broadcastAddress = new IPEndPoint(IPAddress.Any, 9000);
//udpClient.Client.Bind(broadcastAddress);
//udpClient.Connect(broadcastAddress);
//var from = new IPEndPoint(IPAddress.Any, 9000);
var from = new IPEndPoint(0, 0);
while (true)
{
var receive = udpClient.Receive(ref from);
var msg = Encoding.UTF8.GetString(receive);
Debug.Log($"Received message \"{msg}\"");
Debug.Log($"from {from} ({from.Address})");
}
}
I have used several of the commented lines in combination.
- When I send something from within the same Application to 255.255.255.255 with another
UdpClient
on port 9000, it works as expected. - When I send something from any other machine on the network to 255.255.255.255
- any machine in the network receives it (checking with PacketSender on osx devices)
- On the windows machine I'm developing this application on, the message is received by UdpSenderReceiver
- But the udpClient from within Unity does not receive anything - and the Firewall does not ask or tell me anything.
What could be the issue here?