Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

(Netcode) client cannot connect to server

Discussion in 'Multiplayer' started by Danahi, Oct 24, 2022.

  1. Danahi

    Danahi

    Joined:
    Oct 1, 2022
    Posts:
    9
    Hey all,

    I want to be able to set the ip address of a host by means of my own matchmaking server.
    However, the client times out when it attempts to connect.

    I verified the router/modem/port configuration by running something else over the port on the same ip address and that checks out, so it is the connection code. When I verify if the port is open with the host, it doesn't seem to work so my assumption is that the host is the issue. Can anyone see an issue with the following methods?
    Is it the async? I am using system.net.http to perform an async post request in both cases:

    Code (CSharp):
    1.  
    2. private async void Host()
    3. {
    4.     // code that sends relevant data to my own matchmaking server (that is working properly)
    5.     var nm = NetworkManager.Singleton;
    6.     nm.GetComponent<UnityTransport>().SetConnectionData("127.0.0.1", (ushort)7777);
    7.     nm.StartHost();
    8. }
    9.  
    10. private async void Client()
    11. {
    12.     // code that receives the host ipAddress (Verified by Debug.Log)
    13.     if (result.ContainsKey("ip")) // result contains the ip address.
    14.     {
    15.         Debug.Log(result["ip"]); // Verifies that the correct ip address is used (works)
    16.         var nm = NetworkManager.Singleton;
    17.         nm.GetComponent<UnityTransport>().SetConnectionData(result["ip"],  (ushort)7777);
    18.         nm.StartClient();
    19.     }
    20. }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    127.0.0.1 is localhost address,
    host should use local area IP (usually 192.168.x.x)
    or external ip (can get that from google: whats my ip)
    but then router needs to have port forwarding enabled.
     
    Danahi likes this.
  3. Danahi

    Danahi

    Joined:
    Oct 1, 2022
    Posts:
    9
    Thanks, I assumed that the host ip address was a formality and should not intervene with broadcasting the server if the port is correct. But if I interpret your answer, it means that I close the server from accessing it outside the machine and inside the machine on any other ip address than 127.0.0.1?

    The code works now :)
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,529
    127.0.0.1 is called the "localhost", this always points to the local machine. It is essentially like "me" in the english language. Everyone can say it but everyone only refers to his/her own identity by saying it. ;)

    Btw, if you want others to join over the Internet, those clients need to provide the public IP of the network the machine is on. Usually the IP assigned to the router. On that router, port forwarding has to be set up to reach the target server/host on the local network. If that machine also runs a software firewall (ie built-in or antivirus software) then you also need to set up port forwarding there.

    In my learning Netcode project I have a NetcodeHelper class which has a utility to get the public IP.
     
  5. Danahi

    Danahi

    Joined:
    Oct 1, 2022
    Posts:
    9
    Thanks :). I must come across as a total noob XD. I guess I should have been a bit more specific with my second question.

    My worries are not that the external parties (other client(s) and matchmaking server) would have issues with the host ip address, but rather if the netcode component has an issue with initiating a host/server with ip: "127.0.0.1".

    I don't let the host send their ip address to the matchmaking server, rather the matchmaking server checks the ip that is attached to the request. Hence, the client also receives the correct (external) ip.

    However, I fixed the issue. It seems if I run the host in the game that runs in unity itself, then I cannot connect to the host via the executable version of the game as client. Alternatively, if I swap host and client (so executable and unity respectively), it does work.

    Maybe a firewall issue?
    Anyways, it works :D
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,529
    It shouldn't matter who runs in the editor and who runs a build. If it does matter, then those two versions aren't identical for some reason.
     
  7. sh0v0r

    sh0v0r

    Joined:
    Nov 29, 2010
    Posts:
    325