Can't connect to localhost server - connection request failed

Hey everyone. Hoping someone might have an answer for what seems like a pretty ridiculous problem. I think I’ve read every answer related to this in here about 10 times, trying to find a solution.

Basically, I can’t connect to a server hosted on localhost, from localhost. i.e. I have a server and client both running on the same machine, and the client just errors "the connection request to 127.0.0.1:whateverport failed. Are you sure the server can be connected to.

I’ve watched about six different client/server tutorials on YT, and I can’t see what I’m doing wrong. The only difference is that most tutes use the same application as client/server, whereas I have two separate applications.

I have tried:

  • -run in background on/off for client and server and server only.
  • -multiple different port numbers
  • -connecting via LAN IP instead of “localhost” or “127…”
  • -connecting via public IP
  • -creating rules in windows firewall
  • -turning off windows firewall
  • -initialisation NAT param set to false, true, and removed (though I get an obsolete warning about that last)
  • -running 32 and 64 bit versions of the server
  • Adding the connect code to be triggered by a keypress instead of in the Start()

I just don’t know what else to do. The one thing I know I haven’t tried is using Unity’s Master Server, but I didn’t want to do this - seems highly unnecessary.

So time for some code.
Server:

using UnityEngine;
using System.Collections;

public class RemNetwork : MonoBehaviour {

	public GUIText updateScreen;


	void Start() {
		updateScreen.text = updateScreen.text + "

Server started. Initialising…

";
LaunchServer ();
}

	void LaunchServer() {
		//Network.incomingPassword = "test";
		Network.InitializeServer(16, 25003);
	}

	void OnServerInitialized() {
		updateScreen.text = updateScreen.text + "Server Initialised 

";
}

	public void OnGUI()
	{
		if (Network.peerType == NetworkPeerType.Server)
		{
			GUI.Label(new Rect(300,100,400,30), "Clients connected: " + Network.connections.Length ); 
		}
	}}

Client: (in here I connect in the Start() function)

void Start () {
		Network.Connect ("localhost", 25003);
		if (Network.isClient) 
		{
			Debug.Log ("Connected");
		} 
		else 
		{
			Debug.Log ("Not connected");
		}

	}

The network.isclient bit doesn’t really work since it gets called before the connection can be established, but regardless, I quickly get the connection request failed message.

The server “works” as far as I can tell. It starts, it initialises, it lists connections as 0.

So currently, at a loss. Knowing my luck it’s just something stupid. In the meantime I guess I’ll give up and try using a master server implementation, but again, I’d really like not to have to do this.

Thanks in advance for any help you can provide!

This may sound obvious, but have you forced both server- and client-side password entries to “”? I see an entry commented out on the svr side, but nothing on the clientside to indicate you’ve tried this…

Connections are not instantly established. You should move the check for connection to other place, like to Update. I would also change “localhost” to “127.0.0.1” just to be sure.

To check the connection you can use the OnConnectedToServer and OnDisconnectedFromServer methods.

private bool connected = false;

void Start () {
    Network.Connect("127.0.0.1", 25003);
}

void OnConnectedToServer() {
    connected = true;
}

void OnDisconnectedFromServer() {
    connected = false;
}

void OnGUI() {
    GUI.Label(new Rect(300,100,400,30), "Connected: " + (connected == true));
}

Hope this helps!

Hi guys, sorry I haven’t been able to get back to testing this out, but have solved the problem. Well, to be specific, I haven’t, but the problem is in my PC, somewhere. I ran the client and server on my laptop and it worked first time. Still does not run on my desktop (frustrating).

So there’s a config issue, or a conflict with something… who knows!
The point is, the code was fine, I just need to find where/what on my PC is causing the problem. Or work permanently from my laptop. (ugh)

So anyway, thanks for your suggestions - I did toy with them first before deciding to try another machine. :slight_smile: