- Home /
Unity not releasing ports correctly?
I am building a server with a client application. Everything seems to work, but seemingly at random, unity decides to NOT close a port on stopping the project in the editor or when running (build for windows).
Most of the times i have this problem once or 2ce (on first boot. If it correctly works the first time starting and stopping the editor project, it will work for the rest of the runs.)
However, this is starting to get on my nerves as switching ports on both applciations is a pain in the ass, and the final build should just work without this problem.
Here is my server application (Note that no client has to connect for this to happen):
public class ServerScript : MonoBehaviour {
// Use this for initialization
void Start () {
instance = this;
NetworkConnectionError error = Network.InitializeServer(500,26556,false);
if(error != NetworkConnectionError.NoError){
Debug.Log("Server couldn't run: " + error);
}
Debug.Log("[Server]Server started: "+GetIP());
}
#region connect/disconnect
void OnPlayerConnected(NetworkPlayer user){
Debug.Log("[Server]Connected: "+ user.externalIP);
}
void OnPlayerDisconnected(NetworkPlayer user){
//remove this players RPC calls, he might have none but just make sure.
Debug.Log("[Server]Disconnected: "+ user.externalIP);
Network.RemoveRPCs(user);
Network.DestroyPlayerObjects(user);
}
void OnDestroy(){
Network.Disconnect();
}
#endregion
string GetIP(){
string strMachineName = Dns.GetHostName();
IPHostEntry ipHost = Dns.GetHostByName(strMachineName);
IPAddress IP = null;
foreach(IPAddress ip in ipHost.AddressList){
if(ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork){
IP = ip;
}
}
return IP.ToString();
}
public void returnDataToUser(string JsonObject, NetworkPlayer user, LoLCall.CallType calltype){
//make RPC call back to user to deliver data
Debug.Log("[Server]Returning to "+user.externalIP+": " + JsonObject);
foreach (NetworkPlayer player in Network.connections) { //make sure this user didn't bugger out in the mean time!
if(player.Equals(user))networkView.RPC("DataResponse", user, Utils.GetBytes(JsonObject), calltype);
}
}
#region RPC
//the user calls this when he wants data, the cache will return data with an RPC back.
[RPC]
void DataRequest(string JsonCall, NetworkMessageInfo info){
Debug.Log("[Server]User made a data request: " + JsonCall);
//cache.makeRequest(JsonCall, info.sender);
}
[RPC]
void DataResponse(byte[] JsonResult){//, string callType){
//THIS IS A STUB TO MAKE RPC WORK!
}
#endregion
}
And the errors are:
Failed to initialize network interface. Is the listen port already in use?
Server couldn't run: CreateSocketOrThreadFailure
Does anyone have any idea as to why this problem keeps popping up? In general there is not a lot of documentation regarding unity's Network.* part, i wish there was more troubleshooting on this subject.
Thanks in advance, Smiley
I never had such a problem and i always used the editor as server. Since you start your server in "Start", are you sure you don't have this (stupid) ExecuteInEdit$$anonymous$$ode attribute on your class?
However it's usually good advice to use a port range. For example valve games use 27000 - 27015. That allows to run multiple servers on the same machine / IP. So when a port is in use, just use the next from your port pool.
I'm not sure how you manage your client connection but it seems you don't use the $$anonymous$$asterServer.
It's up to you how you handle this. As i said: i never had this problem. Are you sure the game quits "normally"?
I have never heard of ExecuteInEdit$$anonymous$$ode, fast read through says i will not need it often.
I will never require more then one server (it would kind of beat the purpose of what i am trying to achieve) and the same goes for not needing a master server. I just connect clients directly to this server the client can make a request, i will then check a cache or access a database, and give it a result to play with. Nothing fancy going on there.
The application ''quits'' by me pressing the Play button in top of unity. No exceptions or errors have been thrown in the entire runtime either.
Is there an easy example or way to make both server and client aware of the port pool? (i can change the server port, but the client still has to know where to connect to)
Thanks for the reply, Smiley
Well, just define a "short" range (max 10-20 ports) and try them one by one. On the client, you should require some kind of server handshake on connect. So the client can react to a "timeout" and choose the next port.
Answer by Vince_H · Dec 28, 2013 at 05:04 PM
I have found that this error occurs whenever i fail to make connection with my SQL database directly. No idea how to tell unity to fix this though.
Answer by sandbaydev · May 17, 2014 at 08:40 AM
Not a proper solution, but this helped temporarily for me: quit the Unity editor and restart Unity. After that, clicking play & server port should work OK.
Answer by bugmagnet · Jul 06, 2014 at 11:25 PM
I found a solution for my case where I was having the same problem.
It turns out it was my Webroot Antivirus software interfering with my server port. I would be able to start a server, and close it. But once I tried to run it again it would fail with:
"Failed to initialize network interface. Is the listen port already in use?"
Make sure you add your application to your antivirus to tell it not to worry or scan about it. In WebRoot its Identity Protection Settings > Application Protection tab > + Add Application add your Unity application like that.
After adding my .exe to Webroot, my server starts perfectly. Hope this helps.
Your answer
Follow this Question
Related Questions
Position synchronization problem in RPC Network System 0 Answers
RPC call not sending correct data 0 Answers
Empty RPC functions. 1 Answer