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!