- Home /
Network.Connect failed.
Hello everyonw, yesterday I started designing the network features my game would use, and I encountered a problem when trying to make a basic connection between 2 clients.
Whenever I create the server, automatically I get disconnected from it. The console shows the following:
Server initialized and ready on port: 9999.
Local server connection disconnected
That's for the server part. I said 'well, let's check if a client can connect to be sure' and when a client tries to connect I get:
The connection request to 127.0.0.1:9999 failed. Are you sure the server can be connected to?
Could not connect to server: ConnectionFailed
So, I believe this last error is because the server shutted down the second it started as the first warnings say. Anyone has any idea what could be causing this? I have direct connection, my firewall turned off, and the script to check if a connection can be made as the docs say, it returned perfect.
This is the code I'm using to create the server and later connect as a Client. (Note that I already tried to make 2 builds, one server and one client, to check if that was the problem and its the same):
using UnityEngine;
using System.Collections;
public class _Server : MonoBehaviour {
private int playerCount;
// Server
void Start(){
PlayerPrefs.SetInt("ServerPort", 9999);
LaunchServer();
}
public void LaunchServer() {
bool useNat = !Network.HavePublicAddress();
Network.InitializeServer(32, PlayerPrefs.GetInt("ServerPort"), useNat);
}
void OnServerInitialized() {
Debug.Log("Server initialized and ready on port: " + PlayerPrefs.GetInt("ServerPort"));
ConnectToServerFromLocal();
}
// Client
void ConnectToServerFromLocal() {
Network.Connect("127.0.0.1", PlayerPrefs.GetInt("ServerPort"));
}
void OnFailedToConnect(NetworkConnectionError error) {
Debug.Log("Could not connect to server: " + error);
}
void OnDisconnectedFromServer(NetworkDisconnection info) {
if (Network.isServer)
Debug.Log("Local server connection disconnected");
else
if (info == NetworkDisconnection.LostConnection)
Debug.Log("Lost connection to the server");
else
Debug.Log("Successfully diconnected from the server");
}
}
Thanks very much for taking the time to read my problem, it is much appreciated.
Are you trying to connect and host on the same client/version of the game. You can't host and connect with the same program. Try building the game and connect with the build, while host with the editor...
Answer by giulio-pierucci · Mar 19, 2013 at 01:40 PM
I think you cannot call "ConnectToServerFromLocal" from "OnServerInitialized". the first is a "client side method" and the second is a "Server Side Event".
Maybe that "ConnectToServerFromLocal" close the server listen connections that you open from "LaunchServer".
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Network.Connect not connecting to host data or ip address 1 Answer
Main Parameters of MP game for Clients to Join 0 Answers
Networking??? 2 Answers