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

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.
Projects Content… Long one.

1-Server:
Starts in a scene with a single empty object having this script:

public class Servermanager : MonoBehaviour {
    void Awake () {
        DontDestroyOnLoad (this);
        MyServer.Instance.StartServer ();
    }
}

The MyServer is the class I use to manage server, most of the code is taken from Unity’s NetworkManager:

public class MyServer {

    static readonly MyServer _instance;
    public static MyServer Instance{ get { return _instance; } }

    public string ServerIP="localhost";
    public int ServerPort=9999;
    public float MaxDelay=20f;
    
    //Message readers.
    static AddPlayerMessage AddPlayerMessage = new AddPlayerMessage();
    static RemovePlayerMessage RemovePlayerMessage = new RemovePlayerMessage();
    static ErrorMessage ErrorMessage = new ErrorMessage();
    
    public string OnlineScene="MainMap";
    public string OfflineScene="OfflineScene";

    static MyServer(){
        _instance = new MyServer ();
        LogFilter.currentLogLevel = (int)LogFilter.FilterLevel.Developer;
        Application.runInBackground = true;
        Network.InitializeSecurity ();
        NetworkCRC.scriptCRCCheck = false; //tried using true too.
        ConnectionConfig config = new ConnectionConfig ();
        config.AddChannel (QosType.ReliableSequenced);
        config.AddChannel (QosType.Unreliable);
        NetworkServer.Configure (config, 2000);
    }

    public void StartServer(){
        RegisterServerMessages ();
        if (NetworkServer.Listen (ServerIP, ServerPort)) {
            SceneManager.LoadSceneAsync (OnlineScene);
            NetworkServer.SpawnObjects ();
        }
    }
    public void StopServer(){
        if (!NetworkServer.active)
            return;
        NetworkServer.Shutdown();
        SceneManager.LoadScene (OfflineScene);
    }
    void RegisterServerMessages(){
        NetworkServer.RegisterHandler(MsgType.Connect, OnServerConnect);
        NetworkServer.RegisterHandler(MsgType.Disconnect, OnServerDisconnect);
        NetworkServer.RegisterHandler(MsgType.Ready, OnServerReadyMessage);
        NetworkServer.RegisterHandler(MsgType.AddPlayer, OnServerAddPlayerMessage);
        NetworkServer.RegisterHandler(MsgType.RemovePlayer, OnServerRemovePlayerMessage);
        NetworkServer.RegisterHandler(MsgType.Error, OnServerError);
    }

    void OnServerConnect(NetworkMessage netMsg)
    {
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerConnectInternal"); }
        netMsg.conn.SetMaxDelay(MaxDelay);
    }


    void OnServerDisconnect(NetworkMessage netMsg)
    {
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerDisconnectInternal"); }
    }


    void OnServerAddPlayerMessage(NetworkMessage netMsg)
    {
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerAddPlayerMessageInternal"); }

        netMsg.ReadMessage(AddPlayerMessage);

        if (AddPlayerMessage.msgSize != 0)
        {
            var reader = new NetworkReader(AddPlayerMessage.msgData);
            //OnServerAddPlayer(netMsg.conn, AddPlayerMessage.playerControllerId, reader);
        }
        else
        {
            //send error message.
        }
    }

    void OnServerRemovePlayerMessage(NetworkMessage netMsg)
    {   
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerRemovePlayerMessageInternal"); }

        netMsg.ReadMessage(RemovePlayerMessage);
    }


    void OnServerError(NetworkMessage netMsg)
    {
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerErrorInternal"); }

        netMsg.ReadMessage(ErrorMessage);
    }

    //NOT USED.
    void OnServerReadyMessage(NetworkMessage netMsg)
    {
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnServerReadyMessageInternal"); }
    }
}

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:

public class Patcher : MonoBehaviour {
    bool done = false;

    public void Awake(){
        //Find client manager, if exist destroy object.
        foreach (var manager in FindObjectsOfType<ClientManager>()) {
            if (manager != this) {
                Debug.Log ("Destroying manager: " + manager);
                Destroy (manager);
            }
        }
        Debug.Log ("Starting to patch");
        StartCoroutine ("Patch");
    }

    public void Launch(){
        if (done)
            SceneManager.LoadScene ("LoginScene");
        else
            Debug.Log ("Still patching");
    }

    IEnumerator Patch(){
        yield return new WaitForSeconds (5f);
        Debug.Log ("Done Patching");
        done = true;
    }
}

Finally the LoginScene have the following: camera, light, event system, and client manager object.
client manager have this code attached:

public class ClientManager : MonoBehaviour {

    public void Login(){
        string username = GameObject.Find ("UsernameField").GetComponent<InputField> ().text;
        string password = GameObject.Find ("PasswordField").GetComponent<InputField> ().text;
        GameObject.Find ("LoginButton").GetComponentInChildren<Text> ().text = username + ":" + password;
        Debug.Log ("Logged in Successfully");
        StartCoroutine ("LoginWait");
    }

    IEnumerator LoginWait(){
        Debug.Log ("Loading Characters");
        yield return new WaitForSeconds (5f);
        Debug.Log ("Loaded Characters... moving to select scene.");
        //SceneManager.LoadScene ("SelectCharacterScene");
    }
}

And finally the MyClient class, similar to MyServer that manages the network connection of clients:

public class MyClient {

    static MyClient _instance;
    public static MyClient Instance{ get { return _instance; } }

    static NetworkClient client;
    static float maxDelay=20f;
    public static string serverIP = "localhost";
    public static int serverPort =9999;
    public static string username;
    public static string password;

    static MyClient(){
        _instance = new MyClient ();
        LogFilter.currentLogLevel = (int)LogFilter.FilterLevel.Developer;
        Application.runInBackground = true;
        NetworkCRC.scriptCRCCheck = false;//tried with true
        client = new NetworkClient ();
        ConnectionConfig config = new ConnectionConfig ();
        config.AddChannel (QosType.ReliableSequenced);
        config.AddChannel (QosType.Unreliable);
        client.Configure (config, 2000);
        RegisterClientMessages ();
    }

    static void RegisterClientMessages()
    {
        client.RegisterHandler(MsgType.Connect, OnClientConnectInternal);
        client.RegisterHandler(MsgType.Disconnect, OnClientDisconnectInternal);
        client.RegisterHandler(MsgType.NotReady, OnClientNotReadyMessageInternal);
        client.RegisterHandler(MsgType.Error, OnClientErrorInternal);
        client.RegisterHandler(MsgType.Scene, OnClientSceneInternal);
    }

    public bool IsConnected(){
        return client.isConnected;
    }

    public bool Connect(){
        client.Connect (serverIP, serverPort);
        return client.isConnected;
    }

    static void OnClientConnectInternal(NetworkMessage netMsg)
    {
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientConnectInternal"); }

        netMsg.conn.SetMaxDelay(maxDelay);
        LoadLoginGUI ();
    }
    static void LoadLoginGUI(){
        Canvas canvas = Resources.Load<Canvas> ("GUI/LoginCanvas");//Pre-designed canvas for login.
        GameObject.Instantiate<Canvas>(canvas);
        GameObject.Find ("LoginButton").GetComponent<Button> ().onClick.AddListener (
            GameObject.Find("ClientManager").GetComponent<ClientManager>().Login
        );
    }
    static void OnClientDisconnectInternal(NetworkMessage netMsg)
    {
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientDisconnectInternal"); }
        
        SceneManager.LoadScene("PatchScene");
    }

    static void OnClientNotReadyMessageInternal(NetworkMessage netMsg)
    {
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientNotReadyMessageInternal"); }
    }

    static void OnClientErrorInternal(NetworkMessage netMsg)
    {
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientErrorInternal"); }
    }

    static void OnClientSceneInternal(NetworkMessage netMsg)
    {
        if (LogFilter.logDebug) { Debug.Log("NetworkManager:OnClientSceneInternal"); }
    }
}

Anyone can tell me what’s the problem? and how would I solve it???

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???

NetworkClient.Connect is async. Your Connect() method will always return false because isConnected isnt set yet.

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.

disable windows firewall?

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.

Still doesn’t work…
Any other ideas on what’s going wrong?

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???

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.

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 :confused:
Anyone else got an idea on fixing it???

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

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.

I got the same kind of problem.
You can follow this topic too:

There is a weakness somewhere very hard to debug, unity team is not really reactive on this one so:
welcome in the black hole.

I started having the same problem today :frowning: It worked fine for the last week… Server connects, client fails to connect!

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.

There seems to be a problem with the relay server. As other people are having the same problem (myself included). Not sure if it’s only test side or whether it could be affecting live games:

I’ve been spending the last 2 hours trying to figure out where I went wrong…

Well, my problem isn’t with the match server, never even tried reaching anything outside my local machine.

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&shit

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

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.