Unity: Can Connect Only on localhost, no External IP

So, I’m trying to make an online game, and I’m working on the online (Duh), but I can only connect with localhost, if I use External IP Or another computer does it fails.

I DO NOT want to use Master Server, because i’m planning to make the online server free for all, so they can Set-Up their own Server

and Yes, I got portforward already, I tried with several ports, 2550, 25600, all with no luck.

I already tries UseNAT, and the connection test script says everyone can connect to me.

to end this, here’s the full script:

//Strings
private var ServerPortString : String;
private var NOfPlayersServerString : String;
var ClientPortString : String;
var Ip : String;

//ints

var ServerPort : int;
var NOfPlayersServer : int;
private var playerCount: int = 0;
var ClientPort : int;

//true/false
var IsCreatingServer = true;
var IsConnectingToClient = false;
var IsOnMainMenu = false;


function Awake () {
    DontDestroyOnLoad (transform.gameObject);
}

function StartServer() {
Network.InitializeServer(NOfPlayersServer, ServerPort, true);
MasterServer.RegisterHost("Worldz", "Test Server", "I don't know what the hell is this.");
}

function OnGUI() {
	
	if(IsCreatingServer){
	ServerPortString = GUI.TextField (Rect (Screen.width/2-150,Screen.height/1.5-20, 300, 20), ServerPortString, 25);
	NOfPlayersServerString = GUI.TextField (Rect (Screen.width/2-150,Screen.height/1.3-20, 300, 20), NOfPlayersServerString, 25);
	ServerPort = int.Parse(ServerPortString);
	NOfPlayersServer = int.Parse(NOfPlayersServerString);
	if(GUI.Button (Rect (Screen.width/2-100,Screen.height/1.4-25,200,50),"Create Server")){ 
	StartServer();
		} 
	}
	
	if(IsConnectingToClient){
	Ip = GUI.TextField (Rect (Screen.width/2-150,Screen.height/1.5-20, 300, 20), Ip, 25);
	ClientPortString = GUI.TextField (Rect (Screen.width/2-150,Screen.height/1.2-20, 300, 20), ClientPortString, 25);
	ClientPort = int.Parse(ClientPortString);
	if(GUI.Button (Rect (Screen.width/2-100,Screen.height/2-25,200,50),"Connect to the Server")){ 
	ConnectToServer();
			}
	if(GUI.Button (Rect (Screen.width/2-100,Screen.height/1.3-25,200,50),"Back")){ 
	IsConnectingToClient = false;
	IsOnMainMenu = true;
			}  
		}
	
	if(IsOnMainMenu){
	if(GUI.Button (Rect (Screen.width/2-100,Screen.height/2-25,200,50),"Connect to a Server")){ 
	IsOnMainMenu = false;
	IsConnectingToClient = true;
		}
	if(GUI.Button (Rect (Screen.width/2-100,Screen.height/1.5-25,200,50),"Create a Server")){ 
	IsOnMainMenu = false;
	IsCreatingServer = true;
		}
	}
}

function ConnectToServer() {
Network.Connect(Ip,ClientPort);
}

function Update() {
if(ServerPortString == null){
ServerPortString = ("0");
}
if(ClientPortString == null){
ServerPortString = ("0");
}
if(NOfPlayersServerString == null){
NOfPlayersServerString = ("0");
	}
}

function OnServerInitialized() {
    Debug.Log("Server initialized and ready");
}

function OnPlayerConnected(player: NetworkPlayer) {
    Debug.Log("Player " + playerCount++ + 
              " connected from " + player.ipAddress + 
              ":" + player.port);
    // Populate a data structure with player information ...
}

function OnPlayerDisconnected(player: NetworkPlayer) {
    Debug.Log("Clean up after player " +  player);
    Network.RemoveRPCs(player);
    Network.DestroyPlayerObjects(player);
}

open the port in your router/firewall.

Just to tell, I opened ALL the possible porta and tries 3 modems, ALL with no luck at all, connects only with localhost.

1 Like

If you cant connect from the outside, it is most likely a port somewhere (router, firewall, ISP?) that is blocked.

what ports does Unity use? As I already said, I portforwarded port 80, 2550, 26500 and 30000, tried every one of these, with no luck.

why would it be any of those ports? which port are you connecting on?

I tried to connect to everyone of these one at one, and even if they are portforwarded unity refuse to connect.

Any help? I tried everything, even setting up no-ip, but it refuses to connect using external IP, AND YES, MY PORTS ARE UNLOCKED for god’s sake.

var ServerPort : int; → your port is right there, thats the port that should be open and forwarded if needed.

Already done, i opened and forwarded and everything else the port I try to use, and it failed, so I tried to do the same with another port, and it failed again, so basicly unity refuses to connect using an external IP.

EDIT: For god’s sake, people refuse to understand, so I made a pic.

(AGAIN, the port is opened, forwarded and everything else.

EDIT2: (here’s the link to the image if it’s too small http://s7.postimage.org/mgjgtctrf/Cattura_di_schermata_1.png

PS( I made this script all by myself, I sure know what are the vars in there, just to say.)

If it works locally but not through your external IP, it is still most likely what I said: The port is blocked somewhere, maybe by your ISP, maybe by your router, maybe by your firewall on your computer, etc. Maybe you only opened in one place, maybe you dont have control over the place where it is blocked (at your ISP).

I can assure, the port is not blocked, I already tried all of them using other tools… and those tools work just fine, but Unity doesn’t, it refuses in every way to work, and I already called my ISP to be sure they don’t block something, and they don’t block anything, so I am starting to think unity hates me.

Turn on full network debugging in the editor and try and find what is happening, it should show something. And sorry for not understanding, just trying to troubleshoot.
Post the debug if possible.

already turned on. the debug is all in the picture.

(here’s the server debug in image: http://s1.postimage.org/7uwawo0r3/Cattura_di_schermata_2.png )

(the client debug is in the pic up there.)

Ok, thats the server end. What about the connecting client side ?

http://s7.postimage.org/mgjgtctrf/Cattura_di_schermata_1.png

I guess your problem is the nat traversal. Do you have way to host the game on a public ip without router of firewall ?
So the remote connects directly to the game port.

public ip? what’s that?

When you connect to the internet, your provider will assign you an ip addres, hopfully this isn’t an RFC1918 address.
If you get an ip from a public range, that is routed on the net, you can connect to it directly when you connect the pc or laptop directly on the cable.
Eliminating the router/firewall/nat traversal will probaly work and maybe your router/firewall doesn’t support nat traversal.

I got another router, and it supports NAT, I can try it.

EDIT: here’s the connectiontest from the other router. http://s14.postimage.org/i654iqej5/Cattura_di_schermata_3.png

EDIT2: Still, can’t connect using external IP.