- Home /
How do I connect a client to a server?
I have this simple c# script that is attached to my main camera object.(Included below) I run two different instances, one as a server and the other as a client.
I start the server first. The server Network.peerType changes properly. Then I start the client. I type in the proper ipaddress and port and hit connect. There are no network errors, but the Network.peerType never changes on the client and the OnConnectedToServer function never gets called. However everytime I hit the Connect button, the OnPlayerConnect on the server gets called. So there's some type of client server communication. It just seems like the client is missing something.
I'm stuck and I really appreciate the help!
Thanks :)
//Code Start
using UnityEngine; using System.Collections;
public class ConnectionGUI : MonoBehaviour {
private string remoteIp = "127.0.0.1"; private int remotePort = 25000; private int listenPort = 25000; private bool useNAT = false; private string yourIP = ""; private string yourPort = "";
// Use this for initialization void Start () {
}
// Update is called once per frame void Update () {
}
void OnGUI(){ if(Network.peerType == NetworkPeerType.Disconnected){ //Not connected
if(GUI.Button(new Rect(10,10,100,30),"Connect"))
{
//Connecting to the server
Debug.Log(remoteIp);
Debug.Log(remotePort);
NetworkConnectionError ne = Network.Connect(remoteIp,remotePort);
Debug.Log(ne.ToString());
Debug.Log(Network.peerType);
}
if(GUI.Button(new Rect(10,50,100,30),"Start Server")){
//Creating server
Network.InitializeServer(32,listenPort, useNAT);
Object[] gameObjects = FindObjectsOfType(typeof(GameObject));
foreach(GameObject go in gameObjects){
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
}
remoteIp = GUI.TextField(new Rect(120,10,100,20),remoteIp);
remotePort = int.Parse(GUI.TextField(new Rect(230,10,40,20),remotePort.ToString()));
}else{
//Getting your ip address and port
string ipaddress = Network.player.ipAddress;
string port = Network.player.port.ToString();
GUI.Label(new Rect(140,20,250,40),"IP Address:"+ipaddress+":"+port);
if(GUI.Button(new Rect(10,10,100,50),"Disconnect"))
{
//Disconnect from the server
Network.Disconnect(200);
}
}
}
void OnConnectedToServer(){ Debug.Log("Connected to Server"); Object[] gameObjects = FindObjectsOfType(typeof(GameObject));
foreach(GameObject go in gameObjects){
go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);
}
}
void OnPlayerConnected(){ Debug.Log("Player connected"); }
}
Answer by Jason Jolley · Apr 03, 2011 at 06:02 AM
I figured it out. I needed to make sure that I went to Edit -> Project Settings -> Player. Then under Per Platform Settings -> Resolution and Presentation -> Resolution. Then check Run in Background.
Hope this helps somebody :)
Your answer
Follow this Question
Related Questions
Multiplayer server-client setup 1 Answer
Unity networking tutorial? 6 Answers
Multiplayer push object error. 0 Answers
Help with multiplayer connection 0 Answers
Multiplayer syncronisation 1 Answer