Search Unity

Need help getting my UDP to work

Discussion in 'Multiplayer' started by JoeStrout, Jan 19, 2019.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I've hacked out a simple UDP test project, with one app that broadcasts packets, and another app that receives any packets on the same port. This afternoon it was working great. Now it's not working at all, unless both apps are run on the same machine, and I'm stumped.

    The sender code is simple:
    Code (CSharp):
    1.     int PORT = 9876;
    2.     UdpClient udpClient;
    3.     udpClient = new UdpClient();
    4.     ...
    5.     udpClient.Send(bytes, bytes.Length, "255.255.255.255", PORT);
    6.  
    That last line is done about 12 times/second; the earlier lines are done in Awake.

    The receiving code is also fairly simple; it uses a thread to poll the UDP socket:
    Code (CSharp):
    1.         udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, PORT));
    2.         Thread t = new Thread(new ThreadStart(ThreadProc));
    3.         t.Start();
    4.  
    5.     ...
    6.     void ThreadProc() {
    7.         var from = new IPEndPoint(0, 0);
    8.         while (true) {
    9.             var recvBuffer = udpClient.Receive(ref from);
    10.             incomingData = System.Text.Encoding.UTF8.GetString(recvBuffer);
    11.         }
    12.     }
    13.  
    and then I check incomingData in my Update method.

    This all works great when both apps are run on my Mac. But when I run the sender on my Mac and the client on my Oculus Go, no packets are ever received. Earlier this afternoon it did work, though. I don't know what's changed. My wife was watching a movie earlier but she's done; there should now be no more activity on my LAN than there was this afternoon. But it stubbornly refuses to work.

    I also went out on a limb and tried a direct connection to the Go, using the IP address shown by adb shell ifconfig (a 10.0 DHCP address). But then as soon as I send (just removing the third parameter from udpClient.Send), I get a "SocketException: No route to host" error.

    Anybody have any idea what I'm doing wrong here? My goal, if it is possible, is to allow the sender to broadcast packets to any listeners on the same wifi router, without having to specifically set up a connection.
     
  2. CallMeSpam

    CallMeSpam

    Joined:
    Feb 10, 2018
    Posts:
    19
    I think you need to replace IPAddress.Any with the IPAddress of your server.