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 applications 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 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.

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.

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.

I know this is an old thread, but none of the answers were helpful to me and i wanted to help the next in my shoes.

The code below just needs to be called when the server is stopping. This SetSocketOption LingerOption will force the socket to fully close after 1 second if it hasn’t already closed.

void CloseServer()
{
    if (server != null)
    {
        server.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, new LingerOption(true, 1));
        server.Stop();
        server = null;
    }
}

void OnApplicationQuit()
{
    CloseServer();
}