Search Unity

Can't connect to server on local host... err=6

Discussion in 'Multiplayer' started by Severos, Feb 25, 2016.

  1. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    I have 2 projects, server and client.
    The server works without problems, it loads and in debug it says it's running and message handlers are registered. also I've checked the work in background to true (same for client). But the problem is with the client, When it tried to connect it seems to fail so, the on connect event is never fired, and some errors show in debug console:
    And the server never gets the connection at all, it doesn't seem to write anything in debug window (developer level).
    I've tried building the server just to make sure it's not the editor stopping but same problem.

    Here's the full scene, if needed I can provide the files.
    1-Server:
    Starts in a scene with a single empty object having this script:
    Code (CSharp):
    1. public class Servermanager : MonoBehaviour {
    2.     void Awake () {
    3.         DontDestroyOnLoad (this);
    4.         MyServer.Instance.StartServer ();
    5.     }
    6. }
    The MyServer is the class I use to manage server, most of the code is taken from Unity's NetworkManager:
    Code (CSharp):
    1. public class MyServer {
    2.  
    3.     static readonly MyServer _instance;
    4.     public static MyServer Instance{ get { return _instance; } }
    5.  
    6.     public string ServerIP="localhost";
    7.     public int ServerPort=9999;
    8.     public float MaxDelay=20f;
    9.    
    10.     //Message readers.
    11.     static AddPlayerMessage AddPlayerMessage = new AddPlayerMessage();
    12.     static RemovePlayerMessage RemovePlayerMessage = new RemovePlayerMessage();
    13.     static ErrorMessage ErrorMessage = new ErrorMessage();
    14.    
    15.     public string OnlineScene="MainMap";
    16.     public string OfflineScene="OfflineScene";
    17.  
    18.     static MyServer(){
    19.         _instance = new MyServer ();
    20.         LogFilter.currentLogLevel = (int)LogFilter.FilterLevel.Developer;
    21.         Application.runInBackground = true;
    22.         Network.InitializeSecurity ();
    23.         NetworkCRC.scriptCRCCheck = false; //tried using true too.
    24.         ConnectionConfig config = new ConnectionConfig ();
    25.         config.AddChannel (QosType.ReliableSequenced);
    26.         config.AddChannel (QosType.Unreliable);
    27.         NetworkServer.Configure (config, 2000);
    28.     }
    29.  
    30.     public void StartServer(){
    31.         RegisterServerMessages ();
    32.         if (NetworkServer.Listen (ServerIP, ServerPort)) {
    33.             SceneManager.LoadSceneAsync (OnlineScene);
    34.             NetworkServer.SpawnObjects ();
    35.         }
    36.     }
    37.     public void StopServer(){
    38.         if (!NetworkServer.active)
    39.             return;
    40.         NetworkServer.Shutdown();
    41.         SceneManager.LoadScene (OfflineScene);
    42.     }
    43.     void RegisterServerMessages(){
    44.         NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnect);
    45.         NetworkServer.RegisterHandler(MsgType.Disconnect, OnServerDisconnect);
    46.         NetworkServer.RegisterHandler(MsgType.Ready, OnServerReadyMessage);
    47.         NetworkServer.RegisterHandler(MsgType.AddPlayer, OnServerAddPlayerMessage);
    48.         NetworkServer.RegisterHandler(MsgType.RemovePlayer, OnServerRemovePlayerMessage);
    49.         NetworkServer.RegisterHandler(MsgType.Error, OnServerError);
    50.     }
    51.  
    52.     void OnServerConnect(NetworkMessage netMsg)
    53.     {
    54.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerConnectInternal"); }
    55.         netMsg.conn.SetMaxDelay(MaxDelay);
    56.     }
    57.  
    58.  
    59.     void OnServerDisconnect(NetworkMessage netMsg)
    60.     {
    61.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerDisconnectInternal"); }
    62.     }
    63.  
    64.  
    65.     void OnServerAddPlayerMessage(NetworkMessage netMsg)
    66.     {
    67.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerAddPlayerMessageInternal"); }
    68.  
    69.         netMsg.ReadMessage(AddPlayerMessage);
    70.  
    71.         if (AddPlayerMessage.msgSize != 0)
    72.         {
    73.             var reader = new NetworkReader(AddPlayerMessage.msgData);
    74.             //OnServerAddPlayer(netMsg.conn, AddPlayerMessage.playerControllerId, reader);
    75.         }
    76.         else
    77.         {
    78.             //send error message.
    79.         }
    80.     }
    81.  
    82.     void OnServerRemovePlayerMessage(NetworkMessage netMsg)
    83.     {  
    84.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerRemovePlayerMessageInternal"); }
    85.  
    86.         netMsg.ReadMessage(RemovePlayerMessage);
    87.     }
    88.  
    89.  
    90.     void OnServerError(NetworkMessage netMsg)
    91.     {
    92.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerErrorInternal"); }
    93.  
    94.         netMsg.ReadMessage(ErrorMessage);
    95.     }
    96.  
    97.     //NOT USED.
    98.     void OnServerReadyMessage(NetworkMessage netMsg)
    99.     {
    100.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerReadyMessageInternal"); }
    101.     }
    102. }
    pretty simple code, just to make the server start, it does start and debug does log that server is started with all the messages registered, and the online scene loads.
    That's the whole server project.

    2-Client:
    First scene is called PatchScene that will be used to update game components if required. it have single button to connect to server once all checks are done, when clicked the Launch method is called.
    The simple patch mocking script is here:
    Code (CSharp):
    1. public class Patcher : MonoBehaviour {
    2.     bool done = false;
    3.  
    4.     public void Awake(){
    5.         //Find client manager, if exist destroy object.
    6.         foreach (var manager in FindObjectsOfType<ClientManager>()) {
    7.             if (manager != this) {
    8.                 Debug.Log ("Destroying manager: " + manager);
    9.                 Destroy (manager);
    10.             }
    11.         }
    12.         Debug.Log ("Starting to patch");
    13.         StartCoroutine ("Patch");
    14.     }
    15.  
    16.     public void Launch(){
    17.         if (done)
    18.             SceneManager.LoadScene ("LoginScene");
    19.         else
    20.             Debug.Log ("Still patching");
    21.     }
    22.  
    23.     IEnumerator Patch(){
    24.         yield return new WaitForSeconds (5f);
    25.         Debug.Log ("Done Patching");
    26.         done = true;
    27.     }
    28. }
    Finally the LoginScene have the following: camera, light, event system, and client manager object.
    client manager have this code attached:
    Code (CSharp):
    1. public class ClientManager : MonoBehaviour {
    2.  
    3.     public void Login(){
    4.         string username = GameObject.Find ("UsernameField").GetComponent<InputField> ().text;
    5.         string password = GameObject.Find ("PasswordField").GetComponent<InputField> ().text;
    6.         GameObject.Find ("LoginButton").GetComponentInChildren<Text> ().text = username + ":" + password;
    7.         Debug.Log ("Logged in Successfully");
    8.         StartCoroutine ("LoginWait");
    9.     }
    10.  
    11.     IEnumerator LoginWait(){
    12.         Debug.Log ("Loading Characters");
    13.         yield return new WaitForSeconds (5f);
    14.         Debug.Log ("Loaded Characters... moving to select scene.");
    15.         //SceneManager.LoadScene ("SelectCharacterScene");
    16.     }
    17. }
    And finally the MyClient class, similar to MyServer that manages the network connection of clients:
    Code (CSharp):
    1. public class MyClient {
    2.  
    3.     static MyClient _instance;
    4.     public static MyClient Instance{ get { return _instance; } }
    5.  
    6.     static NetworkClient client;
    7.     static float maxDelay=20f;
    8.     public static string serverIP = "localhost";
    9.     public static int serverPort =9999;
    10.     public static string username;
    11.     public static string password;
    12.  
    13.     static MyClient(){
    14.         _instance = new MyClient ();
    15.         LogFilter.currentLogLevel = (int)LogFilter.FilterLevel.Developer;
    16.         Application.runInBackground = true;
    17.         NetworkCRC.scriptCRCCheck = false;//tried with true
    18.         client = new NetworkClient ();
    19.         ConnectionConfig config = new ConnectionConfig ();
    20.         config.AddChannel (QosType.ReliableSequenced);
    21.         config.AddChannel (QosType.Unreliable);
    22.         client.Configure (config, 2000);
    23.         RegisterClientMessages ();
    24.     }
    25.  
    26.     static void RegisterClientMessages()
    27.     {
    28.         client.RegisterHandler(MsgType.Connect, OnClientConnectInternal);
    29.         client.RegisterHandler(MsgType.Disconnect, OnClientDisconnectInternal);
    30.         client.RegisterHandler(MsgType.NotReady, OnClientNotReadyMessageInternal);
    31.         client.RegisterHandler(MsgType.Error, OnClientErrorInternal);
    32.         client.RegisterHandler(MsgType.Scene, OnClientSceneInternal);
    33.     }
    34.  
    35.     public bool IsConnected(){
    36.         return client.isConnected;
    37.     }
    38.  
    39.     public bool Connect(){
    40.         client.Connect (serverIP, serverPort);
    41.         return client.isConnected;
    42.     }
    43.  
    44.     static void OnClientConnectInternal(NetworkMessage netMsg)
    45.     {
    46.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientConnectInternal"); }
    47.  
    48.         netMsg.conn.SetMaxDelay(maxDelay);
    49.         LoadLoginGUI ();
    50.     }
    51.     static void LoadLoginGUI(){
    52.         Canvas canvas = Resources.Load<Canvas> ("GUI/LoginCanvas");//Pre-designed canvas for login.
    53.         GameObject.Instantiate<Canvas>(canvas);
    54.         GameObject.Find ("LoginButton").GetComponent<Button> ().onClick.AddListener (
    55.             GameObject.Find("ClientManager").GetComponent<ClientManager>().Login
    56.         );
    57.     }
    58.     static void OnClientDisconnectInternal(NetworkMessage netMsg)
    59.     {
    60.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientDisconnectInternal"); }
    61.        
    62.         SceneManager.LoadScene("PatchScene");
    63.     }
    64.  
    65.     static void OnClientNotReadyMessageInternal(NetworkMessage netMsg)
    66.     {
    67.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientNotReadyMessageInternal"); }
    68.     }
    69.  
    70.     static void OnClientErrorInternal(NetworkMessage netMsg)
    71.     {
    72.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientErrorInternal"); }
    73.     }
    74.  
    75.     static void OnClientSceneInternal(NetworkMessage netMsg)
    76.     {
    77.         if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientSceneInternal"); }
    78.     }
    79. }
    80.  
    Anyone can tell me what's the problem? and how would I solve it????
     
  2. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Update:
    The server is listening correctly (netstat -an does show listening for UDP on 0.0.0.0 port 9999 *:*), however I'm unable to detect any traffic going using wireshark (tried using localhost, 127.0.0.1, and my internal local IP 196.168.1.15), in the last case there was extra 2 messages in the debug console:
    Any ideas???
     
  3. notseanr

    notseanr

    Joined:
    Jan 15, 2016
    Posts:
    22
    NetworkClient.Connect is async. Your Connect() method will always return false because isConnected isnt set yet.
     
  4. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Yeah I've noticed that by now, it's not the problem here, problem is that it doesn't connect and gives a timeout error.
     
  5. notseanr

    notseanr

    Joined:
    Jan 15, 2016
    Posts:
    22
    disable windows firewall?
     
  6. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Doubt it's the problem since I've already allowed access to public and private networks for unity, and I've tried building and giving the build network access and still nothing. But gonna try when I get back home.
     
  7. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Still doesn't work...
    Any other ideas on what's going wrong?
     
  8. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    *push ups*
    Come on guys, no one got any idea on what could be going wrong?? or at least any other idea on how to separated the server and client projects???
     
  9. Delirus

    Delirus

    Joined:
    Mar 6, 2016
    Posts:
    1
    I am having a similar problem. It worked fine a few days ago, now it doesn't...for a windows build or in editor play. If I run a server in editor and connect to localhost with a webgl build, THAT works fine.
     
  10. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Thanks for the feed back, but really doesn't help fixing the problem, the game isn't designed to be run in webgl at all :/
    Anyone else got an idea on fixing it???
     
  11. rahulkj

    rahulkj

    Joined:
    Feb 10, 2015
    Posts:
    11
    I have the same issue. I suspect it could be a problem with the NAT(Network Address Translation) type. A common issue in multiplayer games
     
  12. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    I don't think you should be experiencing any NAT problems when connecting to localhost but if that is the case you should be able to fix it by manually forwarding the port that the server is hosting on in the router settings.
     
  13. Paradoks

    Paradoks

    Joined:
    Oct 13, 2009
    Posts:
    436
  14. alejobrainz

    alejobrainz

    Joined:
    Sep 10, 2005
    Posts:
    288
    I started having the same problem today :( It worked fine for the last week... Server connects, client fails to connect!
     
  15. alejobrainz

    alejobrainz

    Joined:
    Sep 10, 2005
    Posts:
    288
    Here is a logs I get in the client:

    RegisterHandlerSafe id:14 handler:OnCRC
    Log: cannot connect to relay server after 10 attempt to address {54.237.204.79:9999}
    Client event: host=0 event=DisconnectEvent error=6
    Client disconnected
    NetworkManager:OnClientDisconnectInternal
    NetworkManager StopClient
    Shutting down client -1
    Disconnected from network 4503599628031793

    As I said, It worked last time I touched the project in the same LAN this morning. I´ve tried connecting from my 4G via tethering with same results. Again, The server connects fine and the client fails (even in the same machine)... Any pointers are highly appreciated.
     
  16. James-Ground-Shatter

    James-Ground-Shatter

    Joined:
    Aug 19, 2014
    Posts:
    41
  17. FeedorFeed

    FeedorFeed

    Joined:
    Jan 9, 2014
    Posts:
    35
    I've been spending the last 2 hours trying to figure out where I went wrong......
     
  18. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
  19. _FLX

    _FLX

    Joined:
    Nov 10, 2015
    Posts:
    85
    Hey, are you on windows (~7)?
    Because you can't (natively) track local traffic with wireshark on windows.
    This is not related to your connection problem but might explain why you can't sniff nothing.

    Double check your firewall settings. restart your ethernet card and unity. check that unity has access to all kind of network. (seems like you already done that).


    Then, if you still got problems, maybe check your NetworkServer MaxConnections property is not less than 2 or more if your using relays&S***

    Also, your client is calling "localhost", so doublecheck your /etc/hosts file.
    This line must be uncommented :localhost 127.0.0.1
     
  20. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Yep, I'm using windows7, I've already installed that npcap to track packets for sniffing.
    I've checked the max connectinos.
    there's no /etc/hosts in windows, but "localhost" always worked right with other programs, also tried using 127.0.0.1 and even tried using my internal network IP 192.168.x.x and all do same behavior.
     
  21. _FLX

    _FLX

    Joined:
    Nov 10, 2015
    Posts:
    85
    Ok, are you able to see local traffic other than unity ? (I tried npcap once but never been able to make it work)

    There is a hosts file in windows, located in something like system32/drivers/etc, but if you never touched it I don't think this is the cause of the problem.

    - Always use 127.0.0.1 instead of localhost. Just in case.
    - Is your unity up to date ?

    I have the same kind of code and everything works fine. Maybe it's just a bad param in the network manager / avanced config ...?
    I'll send you some screen of my setup later.
     
  22. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    Local traffic works flawlessly, tried all possible IPs (localhost, 127.0.0.1, 192.168.x.x, and even tried port forward and connect through the internet).
    Looks like you misunderstood something, I'm not using network manager, I'm going with NetworkServer directly so I can manage my stuff in my way.
     
  23. _FLX

    _FLX

    Joined:
    Nov 10, 2015
    Posts:
    85
    Ok, i'm using both myself (NetworkServer for the master server and NetworkManager for the game itself).


    Here is my NetworkServer init :


    Code (CSharp):
    1. public void InitializeServer() {
    2.         if(NetworkServer.active) return;
    3.  
    4.         DontDestroyOnLoad(gameObject);
    5.  
    6.  
    7.         ConnectionConfig config = new ConnectionConfig();
    8.         //config.AddChannel(QosType.ReliableSequenced);
    9.         config.AddChannel(QosType.ReliableFragmented);
    10.         config.AddChannel(QosType.UnreliableSequenced);
    11.         NetworkServer.Configure(config, 100);
    12.  
    13.  
    14.         NetworkServer.Listen(MasterServerPort);
    15.  
    16.         Debug.Log("MasterServer Initialized");
    17.         GameObject.Find("StartMasterServerBtn").GetComponent<Button>().interactable = false;
    18.         GameObject.Find("StartMasterServerBtn").GetComponentInChildren<Text>().text = "MasterServer is ready";
    19.  
    20.         // Events
    21.         NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnect);
    22.         NetworkServer.RegisterHandler(MsgType.Disconnect, OnServerDisconnect);
    23.         NetworkServer.RegisterHandler(MsgType.Error, OnServerError);
    24.  
    25.         // Messages from Clients
    26.         NetworkServer.RegisterHandler(MasterMessages.RegisterClientId, OnClientRegister);
    27.         NetworkServer.RegisterHandler(MasterMessages.FindGameId, OnClientFindGame);
    28.  
    29.         // Messages from Hosts
    30.         NetworkServer.RegisterHandler(MasterMessages.RegisterHostId, OnHostRegister);
    31.  
    32.         initPorts();
    33.         master_ready = true;
    34.     }

    And here my NetworkClient :
    Code (CSharp):
    1.  
    2.     void Awake() {
    3.         Application.runInBackground = true;
    4.         DontDestroyOnLoad(gameObject);
    5.         InitMasterConnection();
    6.         parseCommandLineArgs();
    7.     }
    8.  
    9.     void InitMasterConnection() {
    10.         if(client != null) return;
    11.  
    12.         client = new NetworkClient();
    13.  
    14.         // La config du NetworkClient doit etre la même que celle du NetworkServer
    15.         ConnectionConfig config = new ConnectionConfig();
    16.         //config.AddChannel(QosType.ReliableSequenced);
    17.         config.AddChannel(QosType.ReliableFragmented);
    18.         config.AddChannel(QosType.UnreliableSequenced);
    19.         client.Configure(config, 100);
    20.  
    21.         client.RegisterHandler(MsgType.Connect, OnConnected);
    22.         client.RegisterHandler(MsgType.Disconnect, OnDisconnected);
    23.         client.RegisterHandler(MsgType.Error, OnError);
    24.  
    25.         client.Connect(MasterServerIP, MasterServerPort);
    26.     }
    As you can see this is really minimalist but everything works, also for remote players.

    I'm using the port 45555 (also opened on my router). Maybe try another port?

    Also, i'm not sure about this because there is nothing in the doc, but classes like Network or NetworkCRC look like old S*** from legacy system. ​
     
    Last edited: May 28, 2016
  24. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    I think its started happening since the 5.3.5 on unity, because I used to be able to connect to my own public ip address now i cannot, its not a firewall or nat issue its something to do with unity, what is odd i used to get this problem only on my vps server but was in reverse i could connect all the time, but no one else could. and on my local machince i and others could connect, i fixed the issue on the vps by binding the network adapter ip.. but i cant do that on my local machine because my network adapter is behind a nat so dosent bind to my public ip address.. i hope they fix this soon.. btw if you distrabute it to a remote server like a vps it works fine, it just dosent work when connecting to your self.
     
  25. _FLX

    _FLX

    Joined:
    Nov 10, 2015
    Posts:
    85
    Could be that, i'm still on 5.3.4.
     
  26. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    I just tested it wasent that, i found it to be my port forwarding on my router, you HAVE to do port forwarding even if you have your router setup to allow connections, for some reason without port forwarding other people remotely can connect fine, but you cant locally, but with port forwarding i was able too.. now i dont know if this is your issue since your trying to connect to localhost, so i doubt that would fix your problem.. so maybe you should try port forwarding then connect locally to your internet public ip in the mean time?
     
  27. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    @_FLX
    Your code works amazingly... thanks dude :D
    Somewhy I have the feeling that my code's problem is somewhere in the network messages.
     
  28. _FLX

    _FLX

    Joined:
    Nov 10, 2015
    Posts:
    85
    I see nothing wrong in your messages.
    Maybe SetMaxDelay ?
     
  29. veereshkumbar

    veereshkumbar

    Joined:
    Aug 24, 2017
    Posts:
    4
    Bro Have u solved it having the same problem again
     
  30. iLyxa3D

    iLyxa3D

    Joined:
    Sep 25, 2013
    Posts:
    31
    Turning off Windows firewall (and Esset) helps..