Unity和UDP MulticastGroup

时间:2018-05-24 15:37:58

标签: c# unity3d networking udp

我正在尝试收听使用MultiCastGroup通过UDP发送的主机。在Unity中,客户端启动但没有收到任何消息,但是如果我使用完全相同的代码并将其放入控制台应用程序中,它就可以工作,我会看到主机的消息。

这就是我所拥有的:

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设置我缺少?

1 个答案:

答案 0 :(得分:0)

没关系,Windows更新已经将防火墙重新打开,这阻止了Unity:]