Listen Ports in use error.

I'm trying to work on a game that's going to use some simple networking to have LAN functionality. It's been going well so far, but unfortunately, it seems that after a while I get an error.

I initialize the server with the following command..

Network.InitializeServer(32, connectPort, true);

and it will work usually. However, after using the same connectPort for a while, I get an error message saying...

"Failed to initialize network interface. Is the listen port already in use?"

I have no other instances of the game running. Has anyone seen something like this. This might be a very simple problem, but I am really new to networking so I'm kind of stuck


Which port are you trying to use?

It could well be that another app is already listening on it, or it's one of the ports that you generally shouldn't be using (the <1024 range)

This will happen if you are running the server in a standalone player and you forget to close it before trying to run another server. If you keep running into the issue just make sure when you close on side of the game to close all the other open versions of the game as well. Then if you still run into the issue a quick restart should fix it. I have been making a networked game a for 2 months now and have never seen this issue except when I didn't close the game prior to trying to start another game.

And is there any way to catch this error ?
Network.InitializeServer(1, 10000, useNat);
Debug.LogError(“Port in use!”);
This does not work for me for error catching.

Here’s my StartServer function. I’m looking into a way to check if the port is open before attempting a connection, to avoid hitting the port exception. I can get away with using a random listen port because it is being sent out with a UDP broadcast which is being used to “announce” to the network that a server is running.

static int listenLow = 25000, listenhigh = 26000;
static int startTries = 5;
/// <summary>
/// Starts the server.
/// </summary>
/// <returns>
/// True for success, false for error.
/// </returns>
public static bool StartServer() {
	if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork) {
		NetworkConnectionError error = Network.InitializeServer(numConnections, listenPort, useNat);
		if (error == NetworkConnectionError.NoError) {
			me.networkView.RPC("SetHost", RPCMode.AllBuffered, Network.player);
			Debug.Log("Server Success");
				return true;
		} if (startTries-- > 0) {
			listenPort = UnityEngine.Random.Range(listenLow, listenhigh);
			return StartServer();
		} else {
			UDPConnection.error += error.ToString();
			return false;
	} else
		Debug.LogWarning("no nets!");
	return false;

I’m running into the “port in use” error on both the UDP broadcast (though less frequently) as well as the TCP/IP server. I cannot randomly swap the port on the UDP end because all instances of the game have to “agree” on a port to check.

Also, this is happening in the Unity editor, without having even made a build.

This happens when you don’t close the connection of the network. In your network manager, add this code

void OnDestroy(){
		Network.Disconnect ();

This ensures that when you stop debugging (or more specifically, when your network management GameObject is destroyed), The connection is closed properly.