how to setup and start a server

How do I start a server on another computer? I found this samplecode:

function OnGUI() {
if (GUILayout.Button (“Start Server”))
{
// Use NAT punchthrough if no public IP present
Network.useNat = !Network.HavePublicAddress();
Network.InitializeServer(32, 25002);
MasterServer.RegisterHost(“MyUniqueGameType”, “JohnDoes game”, “l33t game for all”);
}
}

But that’s in Unity and I don’t guess the server has to be started from within Unity on the serverside?

I found a walkthrough online but I’m not sure how to do it.

Hi Kenneth,

Well, I guess you should try that guess :wink:

Game servers in Unity are in fact just games built with Unity and acting as a server. So you would use that code in a game built with Unity that you start on a server, as a server…

Sunny regards,
Jashan

Yes, you do currently use Unity to run a server. This can be a user who runs a server that other users (clients) connect to. This can also be a dedicated server sitting in a server farm. But unless you write your own networking using .Net and sockets, you have to use an instance of Unity to act as server.

Ah, so the link:
http://unity3d.com/master-server/masterserver_0.1.0.zip
is only information to how the masterserver is build. Nobody except the Unityguys has any use of it (because the developers have to make a “server” in a Unity-scene/gui)?

Alright. Now i made an html-page with a UnityObject in it that acts as a server.
In the clientUnity I setup the correct IP as the URL. But if I try to run both of the Unityobjects nothing seems to happen. Well, on the clientside it states that

“RPC call failed because the function ‘printOut’ does not exist in the any script attached to ‘Main Camera’
UnityEngine.NetworkView:RPC(String, RPCMode, Object)”

Do I have to attach the serverfunction to the MainCamera of the clientUnity? That doesn’t sound right to me.

When I call the serversidefunction I do it this way:

networkView.RPC (“printOut”, RPCMode.All, form2);

Is the call correct?

The masterserver is used to register available game servers which your clients can query when they want to join a game. You can either run your own (several of us are) or use the one UT provides at their facility.

Yes, but is it done? How do I setup and run my own masterserver?

Hi Kenneth,

I would recommend starting out with Unity’s Masterserver unless you feel you really need your own masterserver. Once you feel comfortable with the other issues involved in networking, you can still work on the “my own masterserver” project :wink:

Unity does make a lot of the networking a lot easier than if you had to do all the nasty sockets stuff yourself. But still: Networking is not at all trivial…

Have you had a look at the networking example projects? Those should get you started quickly…

Concerning the “workflow”: I usually test with having the client in the editor and starting the server as a standalone on the same machine, or the other way round (server in editor, client as standalone). I also often start up Unity a few times, but that is not recommended (and will give you quite a few crashes of the editor). I usually only create Web players when I’m going for a final round of testing (but that’s just the way I do it because it feels more comfortable to me).

If you’re comfortable with programming and distributed systems in general, you should be able to get the connection up and running pretty quickly and then might want to start with the “Hello World” of networking: A simple little chat (you can use the example from the example projects). Once that works, you can move on :wink:

Sunny regards,
Jashan

I would like to begin with Unitys masterserver. Just one problem: I do not have any clue on how to find such thing in Unity.

Maybe it is stated in those examples you refered to. I will have a look on those in the morning.

Look in the docs under Networking. There is a section that explains how the masterserver works. Also in the scripting docs there is docs on the MasterServer class and the functions that can be called to register your game and query it for available games. I believe that it is set by default to contact the Unity Tech. master server, so if you don’t change the master server, facilitator, and connection tester IP addresses and ports everything should be going through UTs system.

well, I tried to follow the instructions from the example. But I VERYquickly got overwhelmed by the content and how advanced it aparently was.
So I decided to quit the downloaded example.
What I would really like was to get explained in detail (but very shortly and precise) how to do it. And then maybe a short example, too.

If you ignore the NAT related code, it is quite simple (and even that isn’t much once it makes sense). You need to call Network.InitializeServer and then on each client call Network.Connect and point them to the client. This is where the master server comes in, it lest the host put its info up for clients. So after you call InitializeServer call MasterServer.RegisterHost on your instance that is going to be the server. Then before calling Connect you must get the host info from the MasterServer by calling MasterServer.RequestHostList and then MasterServer.PollHostList. This will give you an array of games that match your game type. From there you can choose one of the array elements and use that as the argument for Connect. While you are testing there should only be 1 game available unless you are running multiple servers.

After that its pretty much up to you to call whatever functions using RPCs. Any object that you want to communicate over the network has to have a NetworkView attached. Look at the Network again and try and trace through what is happening, it really is not difficult and that is the most basic example you will find… Though I say that and I’m still trying to work out an RPC bug. But, I did start by modifying the Networking example and going back to the scripting docs. :smile:

I’ve imprted some of the scripts and modified a little. But somehow I get an error:
connection request to masterserver 83.221.146.11:23456

I’ve searched for the IP in the code but can’t find it anywhere. How can I correct this problem?

Bad! :wink:

Go back, do it again. Read through it. Otherwise you’ll end up with half of an idea on how it works after having posted 20-40 questions on the matter.

yes, it was just a short thought I had of giving it up. I went back again pretty quickly. I did some research on open ports and corrected that part. But for some reason nothing happens when I run the aplications. Is there some way of testing whether I have an open connecting between 2 computers? Is there a default firewall in a Mac that needs to be set up?

well, during run I get this error:
“The connectionrequest to masterserver at 83.221.146.11:23456 failed. Are you sure it can be connected to?”

I searched the code for that specific print of error but without any luck. Can anybody give me a hint?

I tried to make the example as simple as possible. Now the code is this:

DontDestroyOnLoad(this);

var remoteIP = “170.23.19.62”;
var remotePort = 50001;
var listenPort = 50000;
var useNAT = false;

function Awake()
{
if (FindObjectOfType(ConnectGuiMasterServer))
this.enabled = false;
}

function OnGUI ()
{
GUILayout.Space(10);

GUILayout.BeginHorizontal();
GUILayout.Space(10);
GUILayout.BeginVertical();
if (GUILayout.Button (“Test RPC”))
{
OnConnectedToServer();
}
if (Network.peerType == NetworkPeerType.Disconnected)
{
if (GUILayout.Button (“Connect”))
{
Network.useNat = !Network.HavePublicAddress();
Network.Connect(remoteIP, remotePort);
print(“Now call to OnConnectedToServer”);
}
if (GUILayout.Button (“Start Server”))
{
Network.useNat = !Network.HavePublicAddress();//useNAT;
Network.InitializeServer(32, listenPort);
// Notify our objects that the level and the network is ready
for (var go in FindObjectsOfType(GameObject))
go.SendMessage(“OnNetworkLoadedLevel”, SendMessageOptions.DontRequireReceiver);
}
GUILayout.EndVertical();
remoteIP = GUILayout.TextField(remoteIP, GUILayout.MinWidth(100));
remotePort = parseInt(GUILayout.TextField(remotePort.ToString()));
}
else
{
if (GUILayout.Button (“Disconnect”))
{
Network.Disconnect(200);
}
}

GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
}

function OnConnectedToServer() {
//RPC-call
networkView.RPC (“printOut”, RPCMode.All, “Hello world: RPC works!”);
// Notify our objects that the level and the network is ready
for (var go in FindObjectsOfType(GameObject))
go.SendMessage(“OnNetworkLoadedLevel”, SendMessageOptions.DontRequireReceiver);
}

function OnDisconnectedFromServer () {
if (this.enabled != false)
Application.LoadLevel(Application.loadedLevel);
//else
// FindObjectOfType(NetworkLevelLoad).OnDisconnectedFromServer();
}

@RPC
function printOut(str: String)
{
print("From RPC: "+str.ToString());
}

@script RequireComponent(NetworkView)

I build 1 version that I treat like a server and presses “Start server”.
I run another version in Unity where I press “connect” and “Test RPC”.
But nothing happens when I do this. If I run 1 version inside Unity and press both the buttons in that version then the output is as I would expect. Then the RPC works…aparently.

What am I doing wrong?

Strange that you cannot connect to the master server - would appear that you for some reason are having some outgoing ports blocked. By default OS X will not block outgoing or incoming connections so it must be something in your external network setup.