3

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:

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?

IARI
  • 1,217
  • 1
  • 18
  • 35
  • 1
    I'm onto something: it appears to be some Firewall Problem, because it seems to work, when i turn the windows firewall off. I'm just puzzled that it works without a problem with the UdpSenderReceiver tool ... – IARI Apr 26 '20 at 07:11

1 Answers1

0

Apparently Windows Defender by default blocks the Unity Editor (but not UdpSenderReceiver).

I had to resolve it by Admitting the Unity Editor on the public domain

image

The Rules for

  • Unity 2019.3.3f1 Editor
  • Unity 2019.3.4f1 Editor

had been present and were not modified.
I modified the Rule for Unity 2019.2.4f1 Editor in order to allow access from the public domain (I simply allowed all domains).

IARI
  • 1,217
  • 1
  • 18
  • 35