Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Netcode ConnectClient Error

Discussion in 'Unity Transport' started by murolofilippo, Nov 27, 2023.

  1. murolofilippo

    murolofilippo

    Joined:
    Jan 23, 2023
    Posts:
    9
    Hi all, I am trying to connect two devices together under the same wifi network, but I am experiencing this problem when I start the client:
    Invalid network endpoint: 192.168.34.198:7777.
    Target server network address (192.168.34.198) is Invalid!
    The ip address is the local address of the machine that is acting as the Host, obtained with

    Code (CSharp):
    1. private string GetLocalIPAddress()
    2.     {
    3.         var host = Dns.GetHostEntry(Dns.GetHostName());
    4.         foreach (var ip in host.AddressList)
    5.         {
    6.             if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    7.             {
    8.                 return ip.ToString();
    9.             }
    10.         }
    11.         throw new System.Exception("No network adapters with an IPv4 address in the system!");
    12.     }
    The devices (android and windows) are both connected to an iOS wifi hotspot. The problem is that whatever address I enter when I go to be the client, it says the ip address is invalid.
    Any solutions? (I also tried entering the same ip address as the machine I run the client from, still same problem).
    Here are the codes for connecting from Host and from Client.

    Host
    Code (CSharp):
    1. public void StartHost()
    2.     {
    3.         UnityTransport transport = NetworkManager.Singleton.GetComponent<UnityTransport>();
    4.         transport.ConnectionData.Address = GetLocalIPAddress();
    5.         NetworkManager.Singleton.StartHost();
    6.     }
    Client

    Code (CSharp):
    1. public void ConnectClient()
    2.     {
    3.         if (serverIp.text != "")
    4.         {
    5.             Debug.Log("Attempting to connect to server IP: " + serverIp.text);
    6.  
    7.             UnityTransport transport = NetworkManager.Singleton.GetComponent<UnityTransport>();
    8.  
    9.             if (!string.IsNullOrEmpty(serverIp.text))
    10.             {
    11.                 Debug.Log($"Attempting to connect to server IP: {serverIp.text} on port: {7777}");
    12.  
    13.              
    14.                 transport.ConnectionData.Address = serverIp.text.ToString();
    15.                 Debug.Log("Set connection data successfully.");
    16.  
    17.              
    18.                 StartClient();
    19.             }
    20.             else
    21.             {
    22.                 Debug.LogError("No server IP provided, cannot connect to server.");
    23.             }
    24.         }
    25.  
    26.     }
    27.  
    28.     public void StartClient()
    29.     {
    30.         NetworkManager.Singleton.StartClient();
    31.     }
    EDIT: Also 127.0.0.1 is an invalid endpoint.
     
    Last edited: Nov 27, 2023
  2. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    Could you try hardcoding an IP address directly in the code? For example:
    Code (CSharp):
    1. transport.ConnectionData.Address = "127.0.0.1";
    That otherwise perfectly fine IP addresses are getting rejected like that at parsing time would tend to indicate that there's something wrong in the string itself (e.g. it contains some invalid control/space character).
     
  3. murolofilippo

    murolofilippo

    Joined:
    Jan 23, 2023
    Posts:
    9
    Yes, if i write it directly in the code it works... why?
     
  4. murolofilippo

    murolofilippo

    Joined:
    Jan 23, 2023
    Posts:
    9
    Now it start the client but is not finding any server, also if the server is launched on the windows machine (client is from android)
     
  5. simon-lemay-unity

    simon-lemay-unity

    Unity Technologies

    Joined:
    Jul 19, 2021
    Posts:
    441
    As I wrote above, it is likely that there's something wrong with the string itself. For example there could be extra spaces around the IP address. Try calling
    Trim
    on the string before feeding it to the transport. If you can hook up a debugger to the application, I'd also check the contents of the string and ensure it only contains the characters that constitute the IP address and nothing else. If using a debugging is not possible, logging the characters one by one would also work.

    Your server needs to be listening on 0.0.0.0 to receive external connections. There's a checkbox under the Unity Transport component to allow remote connections. Make sure it is checked. Alternatively, you can also set the
    ListenAddress
    property of the connection data to "0.0.0.0".
     
  6. murolofilippo

    murolofilippo

    Joined:
    Jan 23, 2023
    Posts:
    9
    I don't know why but an invisible character is added to the end of the string, so it doesn't work with Trim() either. I found the solution by cleaning the string with this function, for anyone who has the same problem as me, using this the ip string is accepted

    string cleanedIP = System.Text.RegularExpressions.Regex.Replace(serverIp.text, "[^0-9.]", "");