- Home /
Connecting to server through another computer (old network system Raknet)
http://www.paladinstudios.com/2013/07/10/how-to-create-an-online-multiplayer-game-with-unity/
This tutorial has shown me the basics of setting up a multiplayer game. I can run two instances of my game on my own computer that are able to connect to one another however when trying to connect to a server hosted by myself on a different computer they are unable to join. Can someone point me in the right direction in fixing this issue?
This is my script that controls the networking..
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
private const string typeName = "2468";
private const string gameName = "Server1";
private void StartServer()
{
Network.InitializeServer(4, 25000, !Network.HavePublicAddress());
MasterServer.RegisterHost(typeName, gameName);
}
void OnGUI()
{
if (!Network.isClient && !Network.isServer)
{
if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
StartServer();
}
if (!Network.isClient && !Network.isServer)
{
if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
StartServer();
if (GUI.Button(new Rect(100, 250, 250, 100), "Refresh Hosts"))
RefreshHostList();
if (hostList != null)
{
for (int i = 0; i < hostList.Length; i++)
{
if (GUI.Button(new Rect(400, 100 + (110 * i), 300, 100), hostList[i].gameName))
JoinServer(hostList[i]);
}
}
}
}
private HostData[] hostList;
private void RefreshHostList()
{
MasterServer.RequestHostList(typeName);
}
void OnMasterServerEvent(MasterServerEvent msEvent)
{
if (msEvent == MasterServerEvent.HostListReceived)
hostList = MasterServer.PollHostList();
}
private void JoinServer(HostData hostData)
{
Network.Connect(hostData);
}
public GameObject playerPrefab;
void OnServerInitialized()
{
SpawnPlayer();
}
void OnConnectedToServer()
{
SpawnPlayer();
}
private void SpawnPlayer()
{
GameObject myPlayerGO = (GameObject)Network.Instantiate(playerPrefab, new Vector3(218f, 48f, 166f), Quaternion.identity, 0);
((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
((MonoBehaviour)myPlayerGO.GetComponent("CharacterMotor")).enabled = true;
myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
}
}
please add your code so we can help, without it we really don't know whats the issue.
Answer by engdahl · Oct 02, 2017 at 08:56 AM
Juste do a a match if you have unity network
This is
not an answer
a necro post for no reason (question date 2015).
a question about the old networking system (Raknet) which can still be used but is not supported on all platforms. Also Unity's development-$$anonymous$$asterServer has been switched off AFAI$$anonymous$$.
Answer by Bunny83 · May 02, 2015 at 12:14 PM
Well, you use the MasterServer functionality to let the players join the server. If the server is behind a NAT router it depends on if the server's router "supports" NAT punchthrough. If not you can't join the server.
The only solution for such a player that wants to host a server is, to setup a port forwarding rule in his router according to your used port. So the player that hosts the server need to forward the port "2468" to his local pc so others from outside can join on this port.
I've once made a simply Network Example (webplayer) if you want to try if connecting works. (I started a server at the moment)
There's also another possibility. Some routers (like mine :/) allow NAT punchthrough, but don't allow a local loopback connection. That means you can talk to the whole internet and the internet can talk to you but you can not talk to yourself. So if you have two PCs in the same local network, they can't join each other when using the public router IP. Using local addresses of course works.
Whether NAT punchthrough is successful or not depends on both routers. Some combinations don't work. See the documentation over here.
What you can try is replacing your InitializeServer like with this one:
Network.InitializeServer(4, 25000, true);
to force using NAT.
Also be sure you understand what a MasterServer is and how this all works. See my internet crash course over here.
Okay I think I understand, how do I find out if my router allows NAT punchthrough? And can there be a fix to this issue through Unity? Im trying to allow anyone using my game to create their own server for their friends and them to play on without doing anything more, is this possible or does it entirely depend on their router?
@Blink: It depend entirely on their router (or on all routers in charge). The problem is that the "NAT technology" is actually a quick hack of existing structutes and they "misuse" the port number in TCP and UDP packets to identify a local user. There is no specification on which those routers where developed at the time. Actually those routers with the worst security are those which work best for NAT punchthrough ^^.
NAT punchthrough is actually a technology that tries to trick or cheat on the router to let a packet from the outside in.
As you can read in the documentation of RakNet (which i already linked in my answer) they write:
In the old days your users would have to go to their router configuration screen and setup a mapping. However, in modern applications users are not usually required to do this, thanks to NatPunchthrough.
Note the word "usually" in that statement ^^. If the hardware of your user doesn't allow NAT punchthrough (or he has a firewall or any other blocking service) there's nothing you can do about that except inform the user that it doesn't work and suggest he should open the appropriate port.
If you're behind a NAT router even "modern games" like $$anonymous$$ecraft requires you to open the right port in your router or nobody on the internet can join your server.
If you want provide a 100% connectability for your users, you have to host the servers on a public server yourself which isn't located behind a NAT router.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Network.Connect failed. 1 Answer
MasterServer.RegisterHost Issue 0 Answers
How can I transfer hosts once the current host leaves? 0 Answers
Multiplayer Server Networking- Player Server Hosting 2 Answers