Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

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

Discussion in 'Multiplayer' started by xernes3, Apr 29, 2016.

  1. xernes3

    xernes3

    Joined:
    Feb 10, 2016
    Posts:
    2
    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?
     
    svendkiloo and Trox like this.
  2. svendkiloo

    svendkiloo

    Joined:
    Dec 2, 2014
    Posts:
    11
    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 :)
     
    Oxygeniium and Carbonn like this.
  3. abhishek_hingorani

    abhishek_hingorani

    Joined:
    Jan 3, 2016
    Posts:
    1
    Just add
    Code (CSharp):
    1. NetworkServer.Reset();
    before you start host.

    cheers!
     
    Azurne, galiggergo, ltnksr and 4 others like this.
  4. svendkiloo

    svendkiloo

    Joined:
    Dec 2, 2014
    Posts:
    11
    Yes, this solves my issue! Thank you very much :)
     
  5. jayvinx

    jayvinx

    Joined:
    Feb 14, 2013
    Posts:
    1
    Wow! Thank you for this
    My problem is gone now.:) :)
     
  6. VaporAnomaly

    VaporAnomaly

    Joined:
    Jun 9, 2015
    Posts:
    22
    This worked :)
    Thank God for small miracles :D
     
  7. ikilane

    ikilane

    Joined:
    Oct 2, 2016
    Posts:
    3
    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 ?
     
  8. Farwall

    Farwall

    Joined:
    Sep 19, 2014
    Posts:
    18
    Getting same error on Android, Why? Please help.
     
  9. Mira101

    Mira101

    Joined:
    Feb 19, 2018
    Posts:
    1
    where should i add it ??
     
  10. unity_29

    unity_29

    Joined:
    Sep 1, 2017
    Posts:
    1
    @Mira101 Please follow below given order of calling networking methods. Hope this help you.
    NetworkServer.Reset();
    NetworkManager.singleton.StartHost();
     
  11. DarkT0mato

    DarkT0mato

    Joined:
    Feb 23, 2018
    Posts:
    30
    Dude I Fu**ing love you, saved my day!
     
    wlwl2 likes this.
  12. MelonMan96

    MelonMan96

    Joined:
    May 4, 2014
    Posts:
    10
    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!