x


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");
}

}

more ▼

asked Apr 03 '11 at 05:42 AM

Jason Jolley gravatar image

Jason Jolley
105 7 7 14

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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 :)

more ▼

answered Apr 03 '11 at 06:02 AM

Jason Jolley gravatar image

Jason Jolley
105 7 7 14

(comments are locked)
10|3000 characters needed characters left

That helped me, thanks.

more ▼

answered May 10 '12 at 07:39 PM

kynous gravatar image

kynous
10

Is this an answer?? then why is the comments used for?

Apr 15 at 04:45 PM moghes
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4148
x681
x412
x124

asked: Apr 03 '11 at 05:42 AM

Seen: 2586 times

Last Updated: Apr 15 at 04:46 PM