Search Unity

Client game not receiving UDP Packages with "client.Receive()"

Discussion in 'Multiplayer' started by RotcivOcnarbs, Jan 7, 2020.

  1. RotcivOcnarbs

    RotcivOcnarbs

    Joined:
    Feb 10, 2019
    Posts:
    4
    Ok, so i'm making a multiplayer digital boardgame, and at first i made all work on the same machine (server and client). Now i'm testing having a server in a machine, and a client in another (both at the same local network), but when the client machine isn't the same as the server, it simply does not receive messages from the server (but it does send them perfectly).

    It all works by the server listening and sending back JSON objects that consists of a parameter "path", that defines the end point of what are you requesting from the server, and an array of parameters for aditional data.

    For the client to request a list of all created rooms for example, it would send a UDP package with data like this:

    Code (JavaScript):
    1. {
    2. "path": "/rooms",
    3. "parameters": []
    4. }
    And this is the Node JS server source code that listens and sends back the rooms data (rooms is an array of objects consisting of the data of the room)

    Code (JavaScript):
    1.  
    2. var callbacks = {
    3.     "/rooms" : function(request, rinfo){
    4. // this executes when a message is received with a JSON parameter "path" == "/rooms"
    5. // the same structure it sends back to the client
    6.         sendBackData(rinfo.address, rinfo.port, "/rooms", rooms.map(function(e) { return JSON.stringify(e);}));
    7.     },
    8. };
    9.  
    10. function sendBackData(address, port, path, parameters){
    11.     var obj = {
    12.         "path":  path,
    13.         "parameters": parameters
    14.     };
    15.     var message = Buffer.alloc(JSON.stringify(obj).length, JSON.stringify(obj), "utf8");
    16.  
    17.     var client = dgram.createSocket('udp4');
    18.     client.send(message, 0, message.length, port, address, function(err, bytes) {
    19.       if (err) throw err;
    20.       console.log("Message " + path + " returned to " + address + ":" + port);
    21.       client.close();
    22.     });
    23. }
    24.  
    25. server.bind(1337, "192.168.0.7");
    The thing i'm trying to test is the "/rooms" request, which i simply send a message to the server with parameter "path" = "/room", and it should send back a JSON text representing all the created rooms in the lobby.

    The server ip is the machine "192.168.0.7", and the client machine i'm testing is with IP "192.168.0.10"

    The server prints each time it receives a message or sends it, and this is what it prints when i make the UDP request in the client:



    So i can confirm that the server is indeed receiving the package from the other machine via network

    This is the threaded method that listens to UDP packages in the Unity client:

    Code (CSharp):
    1.     public void ReceiveData(object o)
    2.     {
    3.         Debug.Log("Begin receiving data...");
    4.         var tc = (ThreadController)o;
    5.         IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);//new IPEndPoint(IPAddress.Parse(GlobalVars.SERVER_IP), GlobalVars.SERVER_PORT);
    6.         while (tc.ShouldExecute) {
    7.             try {
    8.                 Debug.Log("Waiting for data...");
    9.                 byte[] data = client.Receive(ref endPoint);
    10.                 Debug.Log(data.Length + " bytes received");
    11.  
    12.                 string text = Encoding.UTF8.GetString(data);
    13.  
    14.                 GlobalVars.ServerMessage response = JsonUtility.FromJson<GlobalVars.ServerMessage>(text);
    15.                 ReceiveFromServer(response); //this multiplexes all the different requests the client can receive, not important right now, since the code isn't even getting here
    16.  
    17.             }
    18.             catch (Exception err) {
    19.                 print(err.ToString());
    20.             }
    21.         }
    22.         Debug.Log("Finished receiving data");
    23.     }
    So when it receives ANY data, it should at least print how many bytes the message has, but nothing happens in the console...



    I have also downloaded Wireshark to see if the package is actually being sent or not, but it seems that the packages are being sent normally, this is the packages that appear in Wireshark (i ran in both machines and both of them showed the same info)



    The next thing i have checked is the Windows Firewall, but it seems that Unity is not being blocked at all:



    I have thought about blocking ports, but i can't seem to find a suitable solution, since Unity sends packages with random ports, i couldn't possibly know all the ports it will use to unblock all of them. Also i don't know if the UDP packages can be sent with a specificated port in unity settings.

    I have searched all across the internet, but i haven't got no information whatsoever (just posts from early 2013, and unanswered forum posts)

    Does anyone know what could be happening in my client code? I have tried a LOT of things but none seems to work
     
    Fenikkel likes this.
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,066
    You got a ThreadAbortException?! Do you know what this is about?
    Maybe Receive() does not work for the endpoint you define? Try without one.

    Did you make sure that Receive() was called before the datagram arrived? You may want to send more than one from the server, just to make sure it's not that.
     
  3. RotcivOcnarbs

    RotcivOcnarbs

    Joined:
    Feb 10, 2019
    Posts:
    4
    The thread abort exception probably is the abort request the code makes to the receive thread and the end of the game execution so it doesnt loop forever even after the game is closed

    i'm certain that the Receive() is called before, because its called at the Start() method, and the datagram only arrives after i click a button in the game to request the server

    I dont think sending more than one is the problem here, because i have made a little test. I have created another Node App in the client machine that only listens to the servers messages, and in fact i am receiving the message back from the server, so there is nothing wrong with firewall or any network configuration



    So i can be certain that this issue is within Unity, because the Node App can listen to those messages normally

    I just need to know now if the problem is in the code, and i'm not doing something i should, or if the problem is in Unitys configuration (which i dont know what to mess to find out)
     
  4. aabramychev_

    aabramychev_

    Joined:
    May 10, 2019
    Posts:
    16
    Check ports please, looks like one of your peer has randomly selected port. Set up port for both socket (client and server)
    Wireshark should show something like
    192.x.x.Y:13337 -> 192.x.x.Z:5001
    192.x.x.Y:13337 <- 192.x.x.Z:5001
    ...
     
  5. xgjuice

    xgjuice

    Joined:
    Mar 31, 2022
    Posts:
    1
    I have the same problem like this. Did you solve it? Can anyone help me?
     
    Necrimon likes this.
  6. Necrimon

    Necrimon

    Joined:
    Nov 23, 2022
    Posts:
    2
    Same Problem here. UDP gets send perfectly, but receive methode stays waiting.
     
  7. Necrimon

    Necrimon

    Joined:
    Nov 23, 2022
    Posts:
    2
    Solved my problem. (kinda) Unity doesnt accept traffic other than localhost so i wrote a python script which redirects the udp traffic localy to unity