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

Network.Connect(IPAddress, Port) not working

Discussion in 'Multiplayer' started by Tisimuvek, Mar 3, 2015.

  1. Tisimuvek

    Tisimuvek

    Joined:
    Feb 2, 2015
    Posts:
    2
    Everything works fine if I want to create a WAN or a LAN server, but I can only connect to servers with public IP.
    If I want to connect to a LAN server, using the GUI.TextField it doesn't matter what IP I write there it returns the first x numbers before the first dot.
    So if i write in 127.0.0.1 for example, it gives me an error like this:




    But the strange thing is, if I Debug.Log the ipAddres, it gives me back the proper addres.
    So any idea what could be the problem here?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Net;
    4. using UnityEngine.UI;
    5. using System.Collections.Generic;
    6.  
    7. public class NetworkManager : MonoBehaviour
    8. {
    9.     private const string typeName = "RPGGameDev01";
    10.     private string gameName = "Test";
    11.     private bool isRefreshingHostList = false;
    12.     private HostData[] hostList;
    13.     public GameObject playerPrefab;
    14.     public GameObject Spawner;
    15.     private string ipAddres = "eg:127.0.0.1";
    16.     private string Port = "25565";
    17.     private int MainselGridInt;
    18.     private int SubselGridInt;
    19.     private string[] selStrings;
    20.  
    21.     private bool MainMenuButtons = true;
    22.     private bool StartServerButton = false;
    23.     private bool ConnectToServerButton = false;
    24.     private int playerCount = 0;
    25.  
    26.     void Start(){
    27.         Application.runInBackground = true;
    28.     }
    29.  
    30.     void OnGUI(){
    31.         if (!Network.isClient && !Network.isServer){
    32.             MainselGridInt = 4;
    33.             SubselGridInt = 4;
    34.             if(MainMenuButtons){
    35.                 selStrings = new string[] {"Start Server", "Connect to Server"};
    36.                 MainselGridInt = GUILayout.SelectionGrid(MainselGridInt, selStrings, 2, GUILayout.Width(300), GUILayout.Height(150));
    37.                 if (MainselGridInt == 0){
    38.                     MainMenuButtons = false;
    39.                     StartServerButton = true;
    40.                 }
    41.                 if (MainselGridInt == 1){
    42.                     MainMenuButtons = false;
    43.                     ConnectToServerButton = true;
    44.                 }
    45.             }
    46.  
    47.             if(StartServerButton){
    48.                 selStrings = new string[] {"LAN", "WAN", "Cancel"};
    49.                 SubselGridInt = GUILayout.SelectionGrid(SubselGridInt, selStrings, 3, GUILayout.Width(300), GUILayout.Height(150));
    50.                 if(SubselGridInt == 0){
    51.                     StartServerLAN();
    52.                 }
    53.                 if(SubselGridInt == 1){
    54.                     StartServerWAN();
    55.                 }
    56.                 if(SubselGridInt == 2){
    57.                     StartServerButton = false;
    58.                     MainMenuButtons = true;
    59.                 }
    60.             }
    61.             if (ConnectToServerButton){
    62.                 ipAddres = GUI.TextField(new Rect(10, 200, 200, 20), ipAddres, 25);
    63.                 selStrings = new string[] {"Connect", "Refresh Hosts", "Cancel"};
    64.                 SubselGridInt = GUILayout.SelectionGrid(SubselGridInt, selStrings, 2, GUILayout.Width(300), GUILayout.Height(150));
    65.                 if(SubselGridInt == 0){
    66.                     Debug.Log(ipAddres + ":" + Port);
    67.                     ConnectToServer(ipAddres);
    68.                 }
    69.                 if(SubselGridInt == 1){
    70.                     RefreshHostList();
    71.                 }
    72.                 if(SubselGridInt == 2){
    73.                     ConnectToServerButton = false;
    74.                     MainMenuButtons = true;
    75.                 }
    76.                 if (hostList != null){
    77.                     for (int i = 0; i < hostList.Length; i++)
    78.                     {
    79.                         if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList[i].gameName))
    80.                             JoinServer(hostList[i]);
    81.                     }
    82.                 }
    83.             }
    84.         }
    85.     }
    86.  
    87.     private void StartServerLAN(){
    88.         Network.InitializeServer(5, 25565, !Network.HavePublicAddress());
    89.     }
    90.  
    91.     private void StartServerWAN(){
    92.         Network.InitializeServer(5, 25565, Network.HavePublicAddress());
    93.         MasterServer.RegisterHost(typeName, gameName);
    94.     }
    95.  
    96.     private void OnServerInitialized(){
    97.         SpawnPlayer();
    98.         Network.Instantiate(Spawner, Vector3.up * 5, Quaternion.identity, 0);
    99.     }
    100.  
    101.     void Update(){
    102.         if (isRefreshingHostList && MasterServer.PollHostList().Length > 0){
    103.             isRefreshingHostList = false;
    104.             hostList = MasterServer.PollHostList();
    105.         }
    106.     }
    107.  
    108.     private void RefreshHostList(){
    109.         if (!isRefreshingHostList){
    110.             isRefreshingHostList = true;
    111.             MasterServer.RequestHostList(typeName);
    112.             gameName = MasterServer.PollHostList().ToString();
    113.         }
    114.     }
    115.  
    116.     private void ConnectToServer(string ipAddress){
    117.         Network.Connect(ipAddres, Port);
    118.     }
    119.  
    120.     private void JoinServer(HostData hostData){
    121.         Network.Connect(hostData);
    122.     }
    123.  
    124.     private void OnConnectedToServer(){
    125.         SpawnPlayer();
    126.     }
    127.  
    128.     public void SpawnPlayer(){
    129.         Network.Instantiate(playerPrefab, Vector3.up * 5, Quaternion.identity, 0);
    130.     }
    131.  
    132.     private void OnPlayerDisconnected(NetworkPlayer player) {
    133.         Debug.Log("Clean up after player " + player);
    134.         Network.RemoveRPCs(player);
    135.         Network.DestroyPlayerObjects(player);
    136.     }
    137.  
    138.     void OnPlayerConnected(NetworkPlayer player) {
    139.         Debug.Log("Player " + playerCount++ + " connected from " + player.ipAddress + ":" + player.port);
    140.     }
    141. }
    142.  
    and the GUI looks like this:
     

    Attached Files:

  2. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    You have a typo at line 117 > ipAddres -> when your param is ipAddress

    additionally what line does the NAT error appear on? It is important to note that when NAT errors appear it typically has to do with NAT punchthrough which is where the facilitator comes in....

    More info can be found on here..

    http://docs.unity3d.com/Manual/net-UnityNetworkElements.html
    The Master Server is a meeting place for servers and clients where servers are advertised and compatible clients can connect to running games. This prevents the need for fiddling with IP addresses for all parties involved. It can even help users host games without them needing to mess with their routers where, under normal circumstances, that would be required. It can help clients bypass the server’s firewall and get to private IP addresses which are normally not accessible through the public internet. This is done with help from a facilitator which facilitates connection establishment.

    Taken from the link above.
     
  3. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
  4. Tisimuvek

    Tisimuvek

    Joined:
    Feb 2, 2015
    Posts:
    2
    I know what the Master Server is and how it works, but I want to make the game to run on a LAN without contacting the Master Server, because in school where I spend half of my days, the firewall wont allow any ports to pass the border of the local network except for 80/443/21 and the remaining common ports.

    What I wanted to do, was a Minecraft style multiplayer connnect, where a user can type in the hosts ip addres and port, so he/she can join in.

    I thought that "Network.InitializeServer(5, 25565, !Network.HavePublicAddress());" and "Network.InitializeServer(5, 25565, useNat)"; is the same, or am I wrong?
     
  5. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Not sure then man, all I can tell you is that its giving you a facilitator based error.