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

Global Server working only on local network ?

Discussion in 'Multiplayer' started by Quantum_Surt, May 7, 2017.

  1. Quantum_Surt

    Quantum_Surt

    Joined:
    Apr 25, 2015
    Posts:
    39
    Hi, I made a global server and global client so they can connect to each other and then connect to clients together.

    It is working perfectelly with 2 windows on my computer, also working with another computer connected to same wifi but it is not if I try with 2 computers connected to 2 different wifi...

    I really do not understant why...

    Here is part of my code :

    Code (CSharp):
    1.  
    2.     private int port = 6320;
    3.     private int managerPort = 6215;
    4.  
    5.     private string managerComputerIPAdress = "192.168.0.**";
    6.  
    7.     private void Start(){
    8.         Instance = this;
    9.         waitMenu.SetActive(false);
    10.         DontDestroyOnLoad(gameObject);
    11.     }
    12.     public void Playbutton()
    13.     {
    14.         Debug.Log("play button");
    15.      
    16.         mainMenu.SetActive(false);
    17.         waitMenu.SetActive(true);
    18.  
    19.         Debug.Log("SetActive ?");
    20.  
    21.         try
    22.         {
    23.         ManagerClient mc = Instantiate(MClientPrefab).GetComponent<ManagerClient>();
    24.         mc.clientName = nameInput.text;
    25.         if(mc.clientName =="")
    26.             mc.clientName = "UnnamedPlayer";
    27.         mc.clientIPaddress = (Network.player.ipAddress).ToString();
    28.         mc.ConnectToServer(managerComputerIPAdress, managerPort);
    29.         }
    30.         catch(Exception e)
    31.         {
    32.             Debug.Log(e.Message);
    33.         }
    34.  
    35.     }



    Code (CSharp):
    1. public class GlobalServer : MonoBehaviour {
    2.  
    3.     private int port = 6215;
    4.  
    5.     private List<ServerManagerClient> clients;
    6.     private List<ServerManagerClient> disconnectList;
    7.  
    8.     public List<string> clientNames;
    9.     public List<string> clientIP;
    10.  
    11.     private TcpListener server;
    12.     private bool serverStarted;
    13.  
    14.     public void Init()
    15.     {
    16.         DontDestroyOnLoad(gameObject);
    17.  
    18.         clients = new List<ServerManagerClient>();
    19.         disconnectList = new List<ServerManagerClient>();
    20.  
    21.         try
    22.         {
    23.  
    24.             server = new TcpListener(IPAddress.Any, port);
    25.  
    26.  
    27.             server.Start();
    28.             StartListening();
    29.             serverStarted = true;
    30.  
    31.             Debug.Log("#GS : Global Server started");
    32.  
    33.         }
    34.         catch(Exception e)
    35.         {
    36.             Debug.Log("#GS : Socket Error " + e.Message);
    37.         }
    38.     }
    39.  
    40.     private void Update()
    41.     {
    42.         if(!serverStarted || clients.Count <= 0)
    43.             return;
    44.         foreach(ServerManagerClient c in clients)
    45.         {
    46.             /*Debug.Log("#GS : "  + clientNames[clients.IndexOf(c)]);
    47.             Debug.Log("#GS : "  + clientIP[clients.IndexOf(c)]);*/
    48.             if(!IsConnected(c.tcp))
    49.             {
    50.                 Debug.Log("GS : Client disconnect : " + c.clientName);
    51.  
    52.                 disconnectList.Add(c);
    53.                 c.tcp.Close();
    54.  
    55.                 //clients.Remove(c);
    56.                 continue;
    57.             }else{
    58.                 NetworkStream s = c.tcp.GetStream();
    59.                 if(s.DataAvailable)
    60.                 {
    61.                     StreamReader reader = new StreamReader(s,true);
    62.                     string data = reader.ReadLine();
    63.  
    64.                     if(data != null)
    65.                     {
    66.                         Debug.Log("#GS : Receiving data...");
    67.                         OnIncomingData(c,data);
    68.                     }
    69.                 }
    70.             }
    71.  
    72.  
    73.         }
    74.  
    75.         for(int i = 0; i < disconnectList.Count; i++)
    76.         {
    77.             Debug.Log("GS : Client removing : " + disconnectList[i].clientName);
    78.             clients.Remove(disconnectList[i]);
    79.             disconnectList.RemoveAt(i);
    80.  
    81.  
    82.         }
    83.  
    84.         MatchPlayers();
    85.     }
    86.  
    87.     public void StartListening()
    88.     {
    89.         server.BeginAcceptTcpClient(AcceptTcpClient, server);
    90.     }
    91.  
    92.     private void AcceptTcpClient(IAsyncResult ar)
    93.     {
    94.         TcpListener listener = (TcpListener)ar.AsyncState;
    95.  
    96.         string allUsers = "";
    97.         foreach(ServerManagerClient i in clients)
    98.         {
    99.             allUsers += i.clientName + "|";
    100.         }
    101.         ServerManagerClient sc = new ServerManagerClient(listener.EndAcceptTcpClient(ar));
    102.         clients.Add(sc);
    103.  
    104.         StartListening();
    105.  
    106.         Broadcast("GS:NAME+IP?|",clients[clients.Count-1]); //Manager demande NAME et IP
    107.     }
    108.  
    109.     private bool IsConnected(TcpClient c)
    110.     {
    111.         try
    112.         {
    113.             if(c != null && c.Client != null && c.Client.Connected)
    114.             {
    115.                 if(c.Client.Poll(0,SelectMode.SelectRead))
    116.                     return !(c.Client.Receive(new byte[1], SocketFlags.Peek)==0);
    117.  
    118.                 return true;
    119.             }else{
    120.                 return false;
    121.             }
    122.         }
    123.         catch
    124.         {
    125.             return false;
    126.         }
    127.     }

    Code (CSharp):
    1. public class ManagerClient : MonoBehaviour {
    2.  
    3.     public static ManagerClient Instance{set;get;}
    4.  
    5.     public string clientName;
    6.     public string clientIPaddress;
    7.  
    8.     public string opponentName;
    9.     public string opponentIPAddress;
    10.  
    11.     private bool socketReady;
    12.     private TcpClient socket;
    13.     private NetworkStream stream;
    14.     private StreamWriter writer;
    15.     private StreamReader reader;
    16.  
    17.     private void Start()
    18.     {
    19.         Instance = this;
    20.         DontDestroyOnLoad(gameObject);
    21.     }
    22.  
    23.     public bool ConnectToServer(string host, int port)
    24.     {
    25.         if(socketReady)
    26.             return false;
    27.  
    28.         try{
    29.             socket = new TcpClient(host, port);
    30.             stream = socket.GetStream();
    31.             writer = new StreamWriter(stream);
    32.             reader = new StreamReader(stream);
    33.             socketReady = true;
    34.         }catch(Exception e){
    35.             Debug.Log("Socket error" + e.Message);
    36.         }
    37.         return socketReady;
    38.     }
    39.  
    40.     private void Update()
    41.     {
    42.         if(socketReady)
    43.         {
    44.             if(stream.DataAvailable)
    45.             {
    46.                 string data = reader.ReadLine();
    47.                 if(data != null)
    48.                     OnIncomingData(data);
    49.             }
    50.         }
    51.     }








    Maybe managerComputerIPAdress = "192.168.0.**" (** are just here to hide 2 numbers) is not good ?
    (I have take it from Network.player.ipAddress)
    Or have I to do something with my computer ?
     
    Last edited: May 7, 2017
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    192.168.x.x addresses are internal NAT IP's, which cannot normally be accessed from the internet without a technique like VPN or port forwarding. Internet routers will not route any packet with a destination IP address in one of the 3 main private IP address reserved blocks.

    http://www.faqs.org/rfcs/rfc1918.html

    I'd enable port forwarding on your router, forward the correct ports used by your game to whatever machine is acting as the server, and then point your game to connect to the router's public IP address rather than your server's inaccessible private IP address.
     
    Last edited: May 8, 2017
    aabramychev likes this.
  3. Quantum_Surt

    Quantum_Surt

    Joined:
    Apr 25, 2015
    Posts:
    39
    Thank you so much, I fixed it before but that is really kind !

    It is working now, but I then use a server on player 1 computer and clients on both player1 and player2 computers,
    the problem is that it is not working if player1 did not enabled port forwarding on router...
    I saw many things really complicated, don't we have a simple solution with unity ?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Unity's official solution is the relay component of the Unity Multiplayer service. This is a metered bandwidth, pay as much as you use service. There's other 3rd party solutions as well, or you can setup your own dedicated servers.