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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Server can't connect from other networks

Discussion in 'Multiplayer' started by briyan, May 7, 2015.

  1. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    People can't connect from other networks to my server, i can even with my mobile phone in house connect to my server. Do someone knows why?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class Server : MonoBehaviour {
    5.     //The ip adress
    6.     public string IP;
    7.     //the port
    8.     public int Port;
    9.     //admin
    10.     public bool admin;
    11.     //first
    12.     public bool First;
    13.     //user
    14.     public bool User;
    15.     //username admin
    16.     private string admin_password;
    17.     //username admin
    18.     private string admin_username;
    19.     //username
    20.     public string username;
    21.     //password
    22.     public string password;
    23.     //list with username's
    24.     public List<string> usernames;
    25.     //list with password's
    26.     public List<string> passwords;
    27.     //list with emails
    28.     public List<string> emails;
    29.     //network view
    30.     public NetworkView networkview;
    31.     // Use this for initialization
    32.     void Start () {
    33.         First = true;
    34.         IP = "NOTFORTHISFORUM";
    35.         Port = 25001;
    36.         admin_username = "NOTFORTHISFORUM";
    37.         admin_password = "NOTFORTHISFORUM";
    38.         username = "";
    39.         password = "";
    40.         usernames.Add("henk");
    41.         passwords.Add ("henk");
    42.     }
    43.    
    44.     // Update is called once per frame
    45.     void Update () {
    46.    
    47.     }
    48.  
    49.     void OnGUI(){
    50.         //when the program is launched for the first time
    51.         if(First == true){
    52.             //username typefield
    53.             username = GUI.TextArea(new Rect(0, 0, Screen.width/4, Screen.height/10), username);
    54.             //password typefield
    55.             password = GUI.TextArea(new Rect(0, Screen.height/4, Screen.width/4, Screen.height/10), password);
    56.             //login button for the admin
    57.             if(GUI.Button(new Rect(0, Screen.height/10 * 8, Screen.width/6, Screen.height/6), "Log in as Admin")){
    58.                 //when the username and password are equil to the username field and passwordfield
    59.                 if(username == admin_username && password == admin_password){
    60.                     //the player is a admin
    61.                     admin = true;
    62.                     //the first launch is false
    63.                     First = false;
    64.                     //it will start a server
    65.                     Network.InitializeServer(50, Port);
    66.                 }
    67.             }  
    68.  
    69.             if(GUI.Button(new Rect(0, Screen.height /10*6, Screen.width/6, Screen.height/6), "Go to User login")){
    70.                 Network.Connect(IP, Port);
    71.                 User = true;
    72.                 First = false;
    73.             }
    74.         }
    75.         //when the player is the admin
    76.         if(admin == true){
    77.             //it wil show in the box the number of connections
    78.             GUI.Box(new Rect(0, 0, Screen.width, Screen.height), " " + Network.connections.Length);
    79.  
    80.         }
    81.         //when the player is a user
    82.         if(User == true){
    83.             //this is the username type field
    84.             username = GUI.TextArea(new Rect(0, 0, Screen.width/4, Screen.height/6), username);
    85.             //password typer field
    86.             password = GUI.TextArea(new Rect(0, Screen.height/4 * 2, Screen.width/6, Screen.height/6), password);
    87.             NetworkPlayer player;
    88.             player = Network.player;
    89.             if(GUI.Button(new Rect(0, Screen.height/10 * 8, Screen.width/6, Screen.height/6), "Log in as Admin")){
    90.                 networkview.RPC("Login", RPCMode.Server, username, password, player);
    91.             }  
    92.         }
    93.  
    94.  
    95.     }
    96.  
    97.     [RPC]
    98.     void Login(string Username, string Password, NetworkPlayer Player)
    99.     {
    100.         if(Network.isServer)
    101.         {
    102.             for(int i = 0; i < usernames.Count; i++){
    103.                 if(Username == usernames[i]){
    104.                     if(Password == passwords[i]){
    105.                         networkview.RPC("LoadLevel", Player);
    106.                     }
    107.                 }
    108.             }
    109.         }
    110.     }
    111.  
    112.     [RPC]
    113.     void LoadLevel(){
    114.         if(Network.isClient)
    115.         {
    116.             if(Application.loadedLevel == 0)
    117.             {
    118.                 Application.LoadLevel(1);
    119.             }
    120.         }
    121.     }
    122. }
    123.  
     
  2. psyydack

    psyydack

    Joined:
    Aug 30, 2013
    Posts:
    93
    Why they can't connect?
    Can you provide more info about this? Have errors in your app?
     
  3. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Chances are you need to port forward your router or use NAT punchthrough.

    When you create a listener you can access it from your network using the same port number because you specify the IP address but because your router will have a different IP that serves multiple devices it translates port numbers to avoid collisions when 2 PC are listening on the same port number. Port forwarding forces the router not to do that and punchthrough just scans your router until it finds the correct port.

    Alternatively, if you provided them with and IP beginning with 192.168... then you've given them your internal IP address not your external IP address. You can find your external IP here.
     
    briyan likes this.
  4. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    well, is a port forwarding something that has been already installed or do i need to do that?

    The connection request to 94.***.125.***:25565 failed. Are you sure the server can be connected to?
     
  5. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Ok so your IP looks fine, and considering you don't know what port forwarding is I suspect it's that!

    Google it. It's a system to stop your router changing the port number you assign so it's easier to access from outside your network. It's not software rather something you need to configure on your router.

    If you're planning on your players hosting their own server then you need to look at Nat punch through.

    Jamie
     
  6. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    Well, iam the only one to start the server, but what if my IP is a dhcp?
     
  7. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    That shouldn't make any difference. Your router will still change the port. Specify the port forwarding rule using your pc's name rather than its IP if you can, otherwise you may need to assign your pic a static IP to do it.
     
  8. briyan

    briyan

    Joined:
    Jan 13, 2015
    Posts:
    52
    And how do i do this
     
  9. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    It depends on your router, I can't help you from here you just need to Google how to do it.