Cannot open socket on ip {*} and port {7777} plus StartServer Error - Help!;

Hello,

I’m a beginner at Unity3d and networking. I’m creating a UNET multiplayer game by having the host be a client with up to 3 other clients connecting. This is not cloud matchmaking.

I’m having a problem with restarting a game once I’ve cancelled it in the lobby manager using stopHost().

My game is simple:

Scene 0 = Main Menu (not networked)
Scene 1 = Lobby (Custom singleton UNET Lobby NetworkManager for up to four players)
Scene 2 = Game (Networked)
Scene 3 = HighScores (defaults to this after Game is over/calls StopServer and StopHost).

If I enter the lobby from the menu and host a game, the network lobby starts up with an available cancel button. The button calls StopHost() and it does stop the game in the lobby before it starts. This works fine.

It should be noted that the Lobby NetworkManager persists across all screens as Don’t Destroy on Load, Auto Spawn Player and Run in Background are all checked.

When I try and restart the game a second time by clicking the “Play and Host”/StartHost () button I get the following two errors:

  1. Cannot open socket on ip {*} and port {7777}; check please your network, most probably port has been already occupied.

  2. StartServer listen failed.

I also get these errors if I play a full game, go to the HighScores screen (which also does StopHost and StopServer) and then back to the Menu to restart another networked game. Only by closing the program and restarting it can one create another new networked game.

Clearly when I start and then cancel the first game attempt, the lobby networkManager isn’t freeing up/disconnecting from the port and maybe other network protocols when I call stophost(). Why is this and should I be using some other commands?

There are other threads on this type of error, but nothing in those threads seems to work here.

Any advice on how I should be cancelling my networked games, freeing my ports and ControllerIDs so they can be restarted successfully?

2 Likes

I was having similar problems, and reading this post helped. I was only calling Network.Disconnect(), assuming this would completely close the network connection. I now call:

NetworkManager.singleton.StopClient();
NetworkManager.singleton.StopHost();
NetworkManager.singleton.StopMatchMaker();
Network.Disconnect();

before reloading the scene, which seems to have solved my problems, and I can disconnect/reconnect as much as I like.

Hope that might also be useful for you :slight_smile:

2 Likes

Just add NetworkServer.Reset(); before you start host.

cheers!

7 Likes

Yes, this solves my issue! Thank you very much :slight_smile:

Wow! Thank you for this
My problem is gone now.:slight_smile: :slight_smile:

This worked :slight_smile:
Thank God for small miracles :smile:

please i don’t use custom scripts for making looby and so on but i try to test how the online work were i can add these because i dont have any script i just use network manger ?

Getting same error on Android, Why? Please help.

where should i add it ??

@Mira101 Please follow below given order of calling networking methods. Hope this help you.
NetworkServer.Reset();
NetworkManager.singleton.StartHost();

Dude I Fu**ing love you, saved my day!

1 Like

I know this is a very old thread but is there an equivalent of “NerworkServer.Reset();” for Unitys new Netcode for GameObjects? Because im trying to learn this new system and get the exact error stated above!