- Home /
Network connection not working!
Hi everyone. I have created this script which is attached to an empty object in my scene. It uses the master server to connect. When I press the connect button it prints "yay" but doesn't connect. I am trying to connect to an executable (Mac) and the unity editor... Code:
var serverPort : String="25000";
var networkGameName : String;
private var hostNumber : int;
private var hostData: HostData[];
function Start (){
MasterServer.RequestHostList("GameType");
}
function Update (){
if(MasterServer.PollHostList().length>0){
hostData=MasterServer.PollHostList();
print(hostData[0].gameName);
}
}
function OnGUI (){
if(Network.peerType == NetworkPeerType.Disconnected){
networkGameName=GUI.TextField(Rect(5, 30, 150, 20), networkGameName, 20);
if(GUI.Button(Rect(5, 5, 150, 20),"NewServer")){
Network.InitializeServer(32, parseInt(serverPort), false);
MasterServer.RegisterHost("GameType", networkGameName, "Comment");
}
if(GUI.Button(Rect(200, 40, 30, 20),"<") && hostNumber>0){
hostNumber--;
}
if(GUI.Button(Rect(235, 40, 30, 20),">") && hostNumber<10){
hostNumber++;
}
if(hostData){
for(var element : HostData in hostData) {
var tmpIp : String = "";
tmpIp = element.ip[hostNumber] + " ";
Debug.Log(element.gameName + " ip: " + tmpIp + ":" + element.port);
Network.natFacilitatorIP = tmpIp;
Network.natFacilitatorPort = element.port;
}
if(GUI.Button(Rect(200,5,200,30), hostData[hostNumber].gameName)){
Network.Connect(tmpIp, hostData.port);
print("yay");
}
}else{
GUI.Button(Rect(200,5,200,30), "Can't Find Any Games!");
}
}
if(Network.peerType != NetworkPeerType.Disconnected){
if(GUI.Button(Rect(5, 5, 150, 20),"Disconnect")){
Network.Disconnect();
}
}
}
-------------------Can you see anything wrong?--------------------
We need more information, What specifically is not happening, Is there any error messages, or any Network errors, Do you have port 2500 unblocked on both ends, in firewall and on the router? Are you trying it over local host or over IP, if IP Does it work over localhost?
I am having trouble connecting to servers and other people are having trouble connecting to my server. Does this mean we have to open the port on the router on both ends? This is not practical because the majority of users do not have capacity to do this, even I do not have access to the router in my house.
Answer by gilmarhz · Nov 07, 2012 at 11:42 AM
MasterServer is to facilitate the connection. So, use it to connect.
This is the function:
static function Connect (hostData : HostData, password : String = "") : NetworkConnectionError
Just use the HostData: Network.Connect(HostData) like this:
if(hostData){
// for(var element : HostData in hostData) { // var tmpIp : String = "";
// tmpIp = element.ip[hostNumber] + " ";
// Debug.Log(element.gameName + " ip: " + tmpIp + ":" + element.port);
// Network.natFacilitatorIP = tmpIp; // Network.natFacilitatorPort = element.port; // }
if(GUI.Button(Rect(200,5,200,30), hostData[hostNumber].gameName)){
Network.Connect(hostData[hostNumber]);
print("yay");
}
}else{
GUI.Button(Rect(200,5,200,30), "Can't Find Any Games!");
}
And the code at Update() and Start, try to put it in a button or in a function called by a button, may be a button named Refresh:
function RefreshHosts(){
MasterServer.RequestHostList("GameType");
yield WaitForSeconds(1);
if(MasterServer.PollHostList().length>0){
hostData=MasterServer.PollHostList();
print(hostData[0].gameName);
}
}
Answer by gilmarhz · Nov 06, 2012 at 01:01 PM
Go to Edit/Project Settings/Network and set Debug Level to Full. Monitor on console what is happening when you connect() and you will get your answer. Remember to set Run In Background in Edit/Project Settings/Player because when you connect the client, the server is paused. If it does not connect, shows the console log here .
Your answer
Follow this Question
Related Questions
Network.Connect not connecting to host data or ip address 1 Answer
Unhandled message 65 from local ip. 0 Answers
Connection to a registered host not working 1 Answer
Clients are not disconnected when Master Server goes shut down 1 Answer
Can I Control a Network implemented by MasterServer ? 0 Answers