Connection fail

Some time ago I was reading M2H’s networking tutorial. I remember I even managed to script a connection and a friend of mine connected from his PC to mine.

Yeah, the problem is that now it’s not working. I’ve been reading the tutorial for an hour and I can’t get it to work. I couldn’t find information for that in google either.

Here’s my code:

if (Network.peerType == NetworkPeerType.Disconnected){
 connectIP = GUI.TextField(Rect(Screen.width/ 2 - 80, Screen.height/2 - 170, 100, 25), connectIP);
 if(GUI.Button(Rect(Screen.width/ 2 - 80, Screen.height/2 + 138, 160, 50), "CONNECT")){
 Network.Connect(connectIP, connectPort);
 }
 
 if(GUI.Button(Rect(Screen.width/ 2 - 80, Screen.height/2 + 193, 160, 50), "START SERV")){
  var useNat = !Network.HavePublicAddress();
 Network.InitializeServer(4, connectPort, true);
 
 }
 }
 else {
 
  if (Network.peerType == NetworkPeerType.Client){
  GUI.Label(Rect(20, 20, 150, 25), "Connected as a client");
 if(GUI.Button(Rect(Screen.width/ 2 - 80, Screen.height/2 + 138, 160, 50), "DISCONNECT")){
 }
 }
 
 }

It works if I run two instances of the game on my computer and make one the server, but other people woulnd’t be able to connect with their PCs.
I’ve set Run in Background for the project also. What am I missing?

Have you forwarded your server port in your router?

Well I’m using the port, given in the tutorial (not really familiar with this), which is 25000. You can’t see it defined in the code I gave, but I’ve defined it in the begining

It doesn’t matter which port you use, Unity requires the host to forward the port(25000 in your case) in your router. www.portforward.com has some examples on how to forward a port.

Alright, I read this, but I still can’t understand what’s wrong. When initializing the server I give connectPort, which is 25000, so the port for the server is set. Than when connecting I use the same port. I’m sorry, but from all the information I’ve been reading lately about Networking, I got pretty confused.
I’ve also read this:
http://en.wikipedia.org/wiki/Port_forwarding But I still don’t understand how to do this with Unity.

Your code is fine :slight_smile: You need to change something in your router/ modem, not in unity.

Well, that means that everyone with a router will have to change it and I’ve never played a game, which wants from me to set up my router. I have several multiplayer games on my PC and they all work fine. There’s gotta be something to do with my game.

Like I said, only the HOST/server owner has to forward the port, not the player.

Alright, so I sent the game to a friend, who doesn’t have a router and you were right, I managed to connect to him.
So is there a way to bypass that router security or whatever it is? In my game, creating and joining servers will be very vital and for those, who have routers, this will be a pain :frowning:

I believe you can program a port forwarding feature. I don’t know how, but I am reading a network programming book and I should have code for you in a week or two.

port forwarding can not be programmed.

What you can do if a router allows NAT Punchthrough is going that way, but this requires that you setup the networking completely etc (see Unity networking example) and have a fully working master server - connection tester - faciliator setup which is fully reachable through port forwardings etc on the server.
That way most people will be able to host games. There are still some paranoia protected routers that won’t let it pass but those users just have to bear with their paranoia as its their fault.

What though can easily become a problem is when you sit in a LAN and try to connect to yourself with your external IP (internet ip), the router might or might not accept that.
Unitys connection testing etc comes to help here as it will report you 3 IPs in its answer: your external, your LAN ip and the system loopback and if you push in that tripplet to the connection approach unity will use the right one and commonly work (again, see networking example project on the resources section above)

Ah.

What about UPnP though?

only people loving worms and being hacked have UPnP enabled.

also, unity doesn’t support it anyway

I love worms :smile:.

Ok, thanks.

@Dreamora, do you know a way of ‘getting past ports’?. Any special way of programming, or would there have to be a new networking library involved or what?

Edit: Does anyone?

Having a working and stable masterserver - facilitator - connection tester is basically enough. People that don’t get through there will not be able to host without setting up port forwarding manually. Its as easy as that basically, there aren’t many tricks remaining and all of them would require other libraries or implementing them yourself as U3 is already on RakNet 3.7 so it has most if not all available tricks “in”.

There just are and always will be cases where you can’t get through. A good example are also mobiles on the phone network not wifi, they will for example never host games generally

Ok, thanks :smile:.