Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

UDP Packets not received?

Discussion in 'Multiplayer' started by Aeonvirtual, Oct 26, 2018.

  1. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    33
    I made a script that sends and receives udp broadcasts between two PCs. Its the same script on both PCs. PC A can see packets from PC B, but it doesn't work the other way around. I checked with wireshark on PC B and can see the packets from A arriving, they just don't register in unity. Does anyone know what this could be?

    This is the listening part of my code. PC B does get into the listenCallback but the ar.endpoint = 0.0.0.0 instead of the ip address of A and the execution stops (without an error thrown) at the receivedBytes line.

    Code (CSharp):
    1.  private static void listen() {
    2.             if (udpClient != null) udpClient.Close();
    3.             Debug.Log("Listen!");
    4.  
    5.             IPEndPoint ep1 = new IPEndPoint(IPAddress.Any, port);
    6.             UdpClient uc1 = new UdpClient(ep1);
    7.            
    8.             UdpState us1 = new UdpState();
    9.             us1.e = ep1;
    10.             us1.u = uc1;
    11.             uc1.BeginReceive(new AsyncCallback(listenCallback), us1);
    12.  
    13.             udpClient = uc1;
    14.         }
    15.  
    16.         private static void listenCallback(IAsyncResult ar) {
    17.             UdpClient uc1 = ((UdpState)(ar.AsyncState)).u;
    18.             IPEndPoint ep1 = ((UdpState)(ar.AsyncState)).e;
    19.             byte[] receivedBytes = uc1.EndReceive(ar, ref ep1);
    20.             Debug.Log("Received something");
    21.             uc1.Close();
    22.  
    23.             string ip = ep1.Address.ToString();
    24.             NetworkResident r = residents.Find(i => i.ip == ip);
    25.             if (r != null)
    26.                 r.lastSeen = DateTime.Now.Ticks;
    27.             else {
    28.                 r = new NetworkResident(ep1.Address, receivedBytes);
    29.                 residents.Add(r);
    30.                 onNewResident();
    31.             }
    32.         }
     
  2. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    33
    anyone??
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    When a networking issue occurs with the same build on one computer but not the other, chances are it is software firewall settings on the problem computer.
     
  4. Aeonvirtual

    Aeonvirtual

    Joined:
    Oct 14, 2015
    Posts:
    33
    I agree... but I disabled the firewall and still nothing...