Search Unity

StartServer listen failed.

Discussion in 'Multiplayer' started by any_user, Jun 18, 2015.

  1. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    I'm using the Network Manager. When I start the server (or host), stop it, and start it again, I get the error message:

    Code (CSharp):
    1. StartServer listen failed.
    It works again when I restart the build or restart the game in the editor. Looks like the port of the first server (7777 in my case) is still in use after stopping the server. If I change it for the second start, it works.

    Is there anything else I have to do to stop a server except calling NetworkManager.StopServer() ?

    I'm on OSX with Unity 5.1.1
     
  2. powdered_swords

    powdered_swords

    Joined:
    Mar 11, 2013
    Posts:
    28
    I had the same issue with the NetworkManagerHub after restarting the server as a host. I noticed the error could be avoided if you start a client, stop it, then go back to hosting. Could this be a bug? Perhaps it's due to a method being overwritten.

    Here's the code I was using http://pastebin.com/qRuRJZcb
     
  3. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    It clearly seems to be a bug, it's easy to reproduce it in an empty project without any custom scripts. In a scene with just a NetworkManager + NetworkManagerHUD, Start Server -> Stop Server -> Start Server shows the error.

    Submitted a bug: 705831
     
  4. Pelican_7

    Pelican_7

    Joined:
    Nov 25, 2014
    Posts:
    190
    I am getting the same error.

    This bug seems to have appeared in 5.1.0f3 and is still present in 5.1.1p1.

    I believe this is a regression as this bug was not present in 5.1.0f2 and I originally had this functionality working.

    @any_user Did you file the bug? The issue tracker has no issue for 705831.
     
  5. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    374
    I filed the bug, but no feedback except the automatic mail, don't know why it doesn't show up in the tracker. I can confirm the issue is still there in 5.1.1p1.
     
  6. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    This bug has been already fixed in patch 5.1.1p2
     
  7. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    938
    Confirmed, also having this issue on 5.1.1p1.

    When will this release? Start of next week?
     
  8. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    I think so
     
  9. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    right now You can fix this bug manually call Receiv() method before You will add new host
     
  10. EndUser

    EndUser

    Joined:
    Aug 12, 2013
    Posts:
    51
    You definitely mean either:
    Code (CSharp):
    1. this.GetComponent«NetworkManager»().StopServer();
    2. NetworkServer.Reset();
    or:
    Code (CSharp):
    1. this.GetComponent<NetworkManager>().StopHost();
    2. NetworkServer.Reset();
    And not "receiv"!
     
  11. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    No, I meant NetworkTransport.Recieve() call after StopHost() or (better) before AddHost. But it is temporary solution, with the next patch this problem will be solved...
     
  12. Pelican_7

    Pelican_7

    Joined:
    Nov 25, 2014
    Posts:
    190
  13. docjaq-reach

    docjaq-reach

    Joined:
    May 27, 2015
    Posts:
    7
    I can confirm that this is fixed in 5.1.1p2.
     
  14. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    436
    How do I make use of NetworkTransport.Receive()? I can see the parameters in the manual and that there are three event types but I don't understand how to make use of this method.

    I'm using 5.1.1p2 and I'm finding that this bug is still there if I don't use the NetworkManagerHUD for starting and stopping a host. My custom network manager is very simple and doesn't override any part of the default network manager. I just have some additional functions so that I can use my own UI. When I make a host game, then disconnect, and then try to host again I get the error the original poster mentioned.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5.  
    6. public class NetworkManager_Custom : NetworkManager {
    7.  
    8.     public void StartupHost()
    9.     {
    10.         SetPort();
    11.         StartHost(); //Unity method
    12.     }
    13.  
    14.     public void JoinGame()
    15.     {
    16.         SetIPAddress();
    17.         SetPort();
    18.         StartClient(); //Unity method
    19.     }
    20.  
    21.     void SetIPAddress()
    22.     {
    23.         string ipAddress =  GameObject.Find("InputFieldIPAddress").transform.FindChild("Text").GetComponent<Text>().text;
    24.         networkAddress = ipAddress;
    25.     }
    26.  
    27.     void SetPort()
    28.     {
    29.         networkPort = 4444;
    30.     }
    31.  
    32.     void OnLevelWasLoaded (int level)
    33.     {
    34.         if(level == 0)
    35.         {
    36.             GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.AddListener(StartupHost);
    37.             GameObject.Find("ButtonJoinGame").GetComponent<Button>().onClick.AddListener(JoinGame);
    38.         }
    39.  
    40.         else
    41.         {
    42.             GameObject.Find("ButtonDisconnect").GetComponent<Button>().onClick.AddListener(StopHost); //Unity method
    43.         }
    44.     }
    45. }
    46.  
    47.  
     
  15. EndUser

    EndUser

    Joined:
    Aug 12, 2013
    Posts:
    51
    "when i do as i do, i haven't" ;-)
     
  16. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    Could you attach your project here? I will take a look
     
  17. FFSteve

    FFSteve

    Joined:
    Jun 30, 2015
    Posts:
    7
    Where can I access the NetworkTransport object from NetworkManager?
     
  18. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    NetworkTransport is a static class with static member functions so you can just call it anywhere you see fit.
     
    FFSteve likes this.
  19. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    436
    Thanks, it was my mistake. I submitted a bug report Case 708281 and it turned out that my code for adding a listener to the button wasn't correct and was calling the StartHost method more than once. I changed my code to remove listeners from the buttons first and I also realised that I needed to change how I called the StartHost, StartClient, and StopHost methods by first using NetworkManager.singleton.StartHost() for example.

    Here's my corrected code that works and here's a download for the project folder for anyone interested.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4. using UnityEngine.UI;
    5.  
    6. public class NetworkManager_Custom : NetworkManager {
    7.  
    8.     public void StartupHost()
    9.     {
    10.         SetPort();
    11.         NetworkManager.singleton.StartHost();
    12.     }
    13.  
    14.     public void JoinGame()
    15.     {
    16.         SetIPAddress();
    17.         SetPort();
    18.         NetworkManager.singleton.StartClient();
    19.     }
    20.  
    21.     void SetIPAddress()
    22.     {
    23.         string ipAddress =  GameObject.Find("InputFieldIPAddress").transform.FindChild("Text").GetComponent<Text>().text;
    24.         networkAddress = ipAddress;
    25.     }
    26.  
    27.     void SetPort()
    28.     {
    29.         networkPort = 4444;
    30.     }
    31.  
    32.     void OnLevelWasLoaded (int level)
    33.     {
    34.         if(level == 0)
    35.         {
    36.             GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.RemoveAllListeners();
    37.             GameObject.Find("ButtonStartHost").GetComponent<Button>().onClick.AddListener(StartupHost);
    38.  
    39.             GameObject.Find("ButtonJoinGame").GetComponent<Button>().onClick.RemoveAllListeners();
    40.             GameObject.Find("ButtonJoinGame").GetComponent<Button>().onClick.AddListener(JoinGame);
    41.         }
    42.  
    43.         else
    44.         {
    45.             GameObject.Find("ButtonDisconnect").GetComponent<Button>().onClick.RemoveAllListeners();
    46.             GameObject.Find("ButtonDisconnect").GetComponent<Button>().onClick.AddListener(NetworkManager.singleton.StopHost); //Unity method
    47.         }
    48.     }
    49.  
    50. }
    51.  
     
    Last edited: Jul 13, 2015
  20. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    Thanks :) Could you close this bug as resolved? :)
     
  21. Drommedhar

    Drommedhar

    Joined:
    Sep 24, 2013
    Posts:
    78
    Hi
    I'm currently using 5.1.1f1 and I also get "StartServer listen failed." after closing a host and trying to start a new one.
    It does not matter if I use the NetworkManager HUD or do it myself inside C#. I'm not able to open a host after it got closed.

    The workaround of starting a client and after that starting the host works 1 time. After that back to "StartServer listen failed".
    Looks like the bug is still not fixed ;D

    EDIT:
    Just as an info, that's the code which get's used in the GUI (port is set somewhere else, but this is working)
    Code (CSharp):
    1.     public void HostGame()
    2.     {
    3.         NetworkManager.singleton.StartHost();
    4.     }
    5.  
    6.     public void JoinGame(InputField input)
    7.     {
    8.         NetworkManager.singleton.networkAddress = input.text;
    9.         NetworkManager.singleton.StartClient();
    10.     }
    11.  
    12.     public void LeaveMatch()
    13.     {
    14.         NetworkManager.singleton.StopHost();
    15.         NetworkManager.singleton.StopClient();
    16.     }
     
  22. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    436
    Try the latest patch release. I think you'll have no problem then.
     
  23. BmDeveloperz

    BmDeveloperz

    Joined:
    Jul 1, 2013
    Posts:
    62
    I tried this code on 5.1.1p4 and still getting this error;
    StartServer listen failed.
    UnityEngine.Networking.NetworkManager:StartHost()
    NetworkManagerCustom:StartupHost() (at Assets/Scripts/NetworkManager/NetworkManagerCustom.cs:11)
    UnityEngine.EventSystems.EventSystem:Update()
     
  24. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    436
    Maybe have a look at my video. Perhaps there's something in how I have things setup or something that I've said in it that might be useful to you.
     
  25. BmDeveloperz

    BmDeveloperz

    Joined:
    Jul 1, 2013
    Posts:
    62
    I have already watched the video. Thanks for the response and also for the videos. They're very helpful. i guess i will check the other template projects to see where im doing wrong.
     
  26. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    is there anything else in the console log or editor log?
    is this being called twice and failing the second time?
     
  27. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Also on 5.1.1P4 and getting this error. "StartServer listed failed" is only appearing once in the console. To reproduce, just host, leave the game and try to host another. But that's only if I stop the client without stopping the host :D
     
  28. BmDeveloperz

    BmDeveloperz

    Joined:
    Jul 1, 2013
    Posts:
    62
    This happens to me when hosting the game for the first time. I did some tests. My network manager screen loads after my main (Account verification screen). When i discard the main screen and build from directly the network manager screen, there is no errors except the errors about my account system. Here is the code im using to host a new game which i take from GTGD's video tutorial.

    using UnityEngine;
    using System.Collections;
    using UnityEngine.Networking;
    using UnityEngine.UI;

    public class NetworkManagerCustom : NetworkManager {

    public void StartupHost()
    {
    SetPort ();
    NetworkManager.singleton.StartHost ();
    }

    public void JoinGame()
    {
    SetIPAdress ();
    SetPort ();
    NetworkManager.singleton.StartClient ();
    }

    void SetIPAdress()
    {
    string ipAdress = GameObject.Find ("InputFieldIPAdress").transform.FindChild ("Text").GetComponent<Text> ().text;
    NetworkManager.singleton.networkAddress = ipAdress;
    }

    void SetPort()
    {
    NetworkManager.singleton.networkPort = 7777;
    }

    void OnLevelWasLoaded(int level)
    {
    print ("MY CURRENT LEVEL : " + level);
    if(level == 1) //Network manager screen ("Zero" is the account login scene)
    {
    SetupMenuSceneButtons();
    }
    else if(level == 2) // Ingame Scene
    {
    SetupOtherSceneButtons();
    }
    }

    void SetupMenuSceneButtons()
    {
    GameObject.Find ("ButtonStartHost").GetComponent<Button> ().onClick.RemoveAllListeners ();
    GameObject.Find ("ButtonStartHost").GetComponent<Button> ().onClick.AddListener (StartupHost);

    GameObject.Find ("ButtonJoinGame").GetComponent<Button> ().onClick.RemoveAllListeners ();
    GameObject.Find ("ButtonJoinGame").GetComponent<Button> ().onClick.AddListener (JoinGame);
    }

    void SetupOtherSceneButtons()
    {
    GameObject.Find ("ButtonDisconnect").GetComponent<Button> ().onClick.RemoveAllListeners ();
    GameObject.Find ("ButtonDisconnect").GetComponent<Button> ().onClick.AddListener (NetworkManager.singleton.StopHost);
    }







    }
     
  29. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    436
    This is probably a silly question, but have you checked that there isn't a NetworkManager in your main scene as well when you aren't running the game?
     
  30. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    is there something else on your machine using UDP port 7777?
     
  31. BmDeveloperz

    BmDeveloperz

    Joined:
    Jul 1, 2013
    Posts:
    62
    I'm sure that i have only one network manager which is seperated from other scenes.

    I had a cloud build also. I think no one is using that version of the game currently. I'm the only one who can reach that project.
     
  32. BmDeveloperz

    BmDeveloperz

    Joined:
    Jul 1, 2013
    Posts:
    62
    It's being called only once and i got only one error which says "StartServer listen failed.".
     
  33. remembadaname

    remembadaname

    Joined:
    Jul 6, 2015
    Posts:
    5
    im using 5.1.1p4 and it is broken again xD
     
  34. remembadaname

    remembadaname

    Joined:
    Jul 6, 2015
    Posts:
    5
    im gonna download 5.1.1p2 so i can move to my next part lol
     
  35. remembadaname

    remembadaname

    Joined:
    Jul 6, 2015
    Posts:
    5
    mine works now but not with your same code GTGD. I had to modify it and add an extra script but it works.
    .
     
  36. BmDeveloperz

    BmDeveloperz

    Joined:
    Jul 1, 2013
    Posts:
    62
    Can you share your script, or the changes that you did to solve the problem please ?
     
  37. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
  38. RSH1

    RSH1

    Joined:
    Jul 9, 2012
    Posts:
    256
    Getting this error with 5.1.1f1
     
  39. Koesys

    Koesys

    Joined:
    Jul 20, 2015
    Posts:
    18
    Hello.
    Thanks for your tutorial GTGD. I follow exactely what you did, so I have the same code as you paste here. But I have the same problem as everyone:
    - I click on "start host', it work.
    - I deconnect.
    - I click on "start host" again : I have "'StartServer listen failed". Anytime I click.

    The strange thing is when i write "localhost" in the inputfield, I click on 'joingame', it doesn't work, but then I click on start host and... it work again ! but... only once. If I redo the manipulation, it is definitely not working.
     
  40. Gaston I.

    Gaston I.

    Joined:
    Jul 24, 2015
    Posts:
    1
    Seems fixed after upgrading to Unity 5.1.2f1 - I was having same issue as Koesys when following GTGD's tutorial with Unity 5.1.1f1 on OSX.

    There is a new warning though that seems to reveal what was happening:

    Multiple NetworkManagers detected in the scene. Only one NetworkManager can exist at a time. The duplicate NetworkManager will not be used.
    UnityEngine.Networking.NetworkManager:Awake()


    I think the problem was the Network Manager would be created in the Menu scene, then because 'Dont Destroy on Load' is enabled it would stay around when switching to Main scene, but when you disconnect and go back to 'Menu' scene it was trying to create a new Network Manager, when you already have one.

    Not sure best fix, but guessing writing a script to create the network manager in the Menu scene only if one isn't already running. Anyone please feel free to suggest a better approach.
     
  41. madelinot

    madelinot

    Joined:
    Jul 15, 2015
    Posts:
    5
    Hi, i have the same warning

    Multiple NetworkManagers detected in the scene. Only one NetworkManager can exist at a time. The duplicate NetworkManager will not be used.
    UnityEngine.Networking.NetworkManager:Awake()

    Bioert tell me :
    Try not to use the NetworkManager as a "classic" singlenton, instead create a separate GameObject Game Manager and make it a singlenton.

    What I found is that you need to establish each relative references, variables, UI, each time you Enter the game for the first time, load the online scene and go back to the offline scene. Try using OnlevelLoad to "Re-establish" variables and references.

    me:
    I dont have time to test it actually but maybe thats can help you!


    Edit: i see a new version 5.1.3 im gonna download it , maybe the bug is fixed,
     
    Last edited: Aug 26, 2015
  42. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    this is just a warning.. if it occurs when returning to the staring scene it can be ignored.

    it is to prevent attempts to have multiple active NetworkManager instances at the same time.
     
  43. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    I've stumbled over this warning too. In my setup, the NetworkManager is part of the menu scene (a scene object). The Don't destroy on load flag is enabled so the NetworkManager is still there when I move to the actual InGame scene. After disconnecting and returning to the menu, the warning is displayed.

    My (maybe naive) solution is that I just destroy the current NetworkManager object when disconnecting so that the NetworkManager object in the menu scene replaces the old one and becomes the new single active NetworkManager. I just don't like the idea of potentially having 2 NetworkManagers active at the same time, even if one of them is not gonna be used (what does this actually mean? Is it destroyed?). I also feel uncertain because the warning does not seem to specifiy which one of those duplicate NetworkManagers is not gonna be used. In the first place, I would have expected that the Don't destroy on load flags makes sure that there really is just 1 NetworkManager around, the initial one, without any warnings.
     
    Last edited: Aug 27, 2015
  44. SoftwareLogicAU

    SoftwareLogicAU

    Joined:
    Mar 30, 2014
    Posts:
    34
    I'm stumped here - could really use some help!

    Does anyone know (using GTGD's script - thanks), how to hold off on launching the game until there are 2 players connected? Been trying for 4 days and can't figure it out. Whoever can help me out will be a legend!

    Thanks.
     
  45. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    Override the player join event and increment a player count by 1. I've used this information both to start the game and also to assign spawn points. If you want an awkward way to do it, what I did was on spawning your player, increment a static player count in the player script. This will make an accurate count because other players get instantiated in your game when you join. So if you join second you will get a count of 2. Just add a condition in your player start function for when the count is equal to 2, and send a command to the server to start the game :D

    It's awkward, but it works both in assigning IDs that are already attached to the player and initiating events based on a particular number of players joining
     
  46. SoftwareLogicAU

    SoftwareLogicAU

    Joined:
    Mar 30, 2014
    Posts:
    34
    Thanks Tom - I appreciate your help!

    Can you give an example of how I override the player join event (sorry - probably really simple!)? Is this done in my custom lobby scene or in the player script (in the game scene)?

    My preference would be to have the players wait in my custom lobby scene and when both are connected, the game launches...

    Basically, what I would like to have happen is:
    1. The screen has 2 options (HOST GAME and JOIN GAME) - local multiplayer only at this stage
    2. Player1 clicks HOST GAME on his/her device and is presented with 'Waiting for other player....' (There is also a cancel button if he/she wants to abort.)
    3. Player2 clicks JOIN GAME on his/her device and when the system detects both players are connected it launches the game.

    My game doesn't have characters as such, as it involves both players taking turns at controlling the same object in turn. Basically, Player1 has control of the object and moves it in order to avoid an incoming obstacle. As soon as it has passed, Player2 has control and has to avoid the next obstacle. This goes on until 1 of the players gets hit by an obstacle, which then shows the accumulated score (eg: 3 : 1) and the players can choose to rematch or exit. Does this mean I don't need to spawn a player when I start the game, or do I still need one (invisible perhaps) so it can hold the script which communicates the players commands with the server?

    Any further assistance you can offer would be awesome - thanks!
     
  47. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    http://docs.unity3d.com/ScriptReference/Networking.NetworkManager.html

    Scroll down to public functions. If you override OnServerConnect you can keep track of a player count for joining players and launch some sort of event when it happens. You need to have a class that extends the network manager class and then simply type override and autocomplete will handle the rest. You'll get the function stub + the call to the original function (super call). You can add your player counter somewhere in there.
     
  48. SoftwareLogicAU

    SoftwareLogicAU

    Joined:
    Mar 30, 2014
    Posts:
    34
    Great, thanks Tom - sounds promising.