C# 统一和UDP多播组
我正在尝试侦听使用多播组通过UDP发送的主机。在Unity中,客户端启动,但从未收到任何消息,但如果我将完全相同的代码放入控制台应用程序中,它就会工作,我会看到主机的消息 以下是我得到的:C# 统一和UDP多播组,c#,unity3d,networking,udp,C#,Unity3d,Networking,Udp,我正在尝试侦听使用多播组通过UDP发送的主机。在Unity中,客户端启动,但从未收到任何消息,但如果我将完全相同的代码放入控制台应用程序中,它就会工作,我会看到主机的消息 以下是我得到的: public class UDPListener : MonoBehaviour { IPEndPoint ip; private int port = 20000; private IPAddress group_address = IPAddress.Parse("233.255.255
public class UDPListener : MonoBehaviour
{
IPEndPoint ip;
private int port = 20000;
private IPAddress group_address = IPAddress.Parse("233.255.255.255");
private UdpClient client;
string data;
private void Start()
{
StartClient();
}
void StartClient()
{
Debug.Log("Starting Client");
ip = new IPEndPoint(IPAddress.Any, port);
client = new UdpClient(ip);
client.JoinMulticastGroup(group_address);
client.BeginReceive(new AsyncCallback(ReceiveServerInfo), null);
}
void ReceiveServerInfo(IAsyncResult result)
{
byte[] receivedBytes = client.EndReceive(result, ref ip);
data = Encoding.ASCII.GetString(receivedBytes);
if (String.IsNullOrEmpty(data))
{
Debug.Log("No data received");
}
else
{
Debug.Log(data);
}
client.BeginReceive(new AsyncCallback(ReceiveServerInfo), null);
}
}
我使用client.Receive也得到了同样的结果(在控制台应用程序中工作,但在Unity中不工作),所以我想知道是否有我缺少的Unity设置?不过,Windows update已经打开了阻止Unity的防火墙:首先,您使用的是一个不属于您的多播组。您应该在组织本地范围内使用组(
239.0.0.0/8
)。此外,除非在两台主机之间的路径中的每个路由器上都配置了多播路由,否则不能使用多播。这就是为什么多播不能在公共Internet上工作的原因(您需要使用支持多播的隧道,而不是所有支持多播的隧道来跨Internet进行多播)。多播路由与单播路由非常不同。很抱歉,我应该提到这只适用于本地网络,但谢谢。