【发布时间】:2020-04-26 06:00:20
【问题描述】:
我想接收一个 UDP 消息,该消息在 Unity 中使用UdpClient 广播到 255.255.255.255。
但是无论我尝试什么设置组合,它只会收到一条消息,如果它是从本地主机发送的。
我已经尝试从这些资源中安装示例代码,但没有工作:
- 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
我正在任务下方运行代码。
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})");
}
}
我结合使用了几行注释。
- 当我从同一个应用程序中向 255.255.255.255 发送一些内容时,另一个
UdpClient在端口 9000 上,它可以正常工作。 - 当我从网络上的任何其他计算机向 255.255.255.255 发送内容时
- 网络中的任何机器都会收到它(使用 osx 设备上的 PacketSender 检查)
- 在我正在开发此应用程序的 Windows 机器上,消息由 UdpSenderReceiver 接收
- 但是 Unity 内部的 udpClient 没有收到任何信息 - 防火墙没有询问或告诉我任何信息。
这可能是什么问题?
【问题讨论】:
-
我遇到了一些问题:这似乎是一些防火墙问题,因为当我关闭 Windows 防火墙时,它似乎可以工作。我只是对 UdpSenderReceiver 工具没有问题感到困惑......
标签: c# unity3d udpclient receiver