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. Dismiss Notice

Network.Connect returns immediately then throws Exception?

Discussion in 'Scripting' started by KelsoMRK, Feb 21, 2013.

  1. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    So according to the docs - Network.Connect is supposed to return a NetworkConnectionError that denotes whether or not the connection happened. I figured I would check this returned value to display some info to the user if they weren't able to connect for whatever reason. What seems to be happening is that Network.Connect returns immediately with a clean NetworkConnectionError (NoError) and after some time elapses (a timeout setting on the socket I'm guessing) Unity throws an Exception saying it couldn't connect to such-and-such IP address.

    Is this the way it's supposed to work? I should mention that I'm connecting via LAN directly and not through a Master Server. Does that have something to do with it? The bummer is, I'm hooking Unity's error handling in order to treat every Exception like an "assert" in the standalone build so I don't want this Exception bubbling up to the user and causing my game to exit - and I really would rather not suppress displaying the error message based on the string contained in it (something I've unfortunately already had to do to some degree).

    Thoughts?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Just giving this a bump.

    Is the preferred method to use the MonoBehaviour "events" for connection success/failure? It seems odd for these methods to return values that apparently aren't accurate.
     
  3. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Indeed I could. I was looking to use the returned enum because the class that manages connections does not inherit from MonoBehaviour.

    Curious why the documented functionality doesn't appear to function as documented. :)

    EDIT: Here's basically what I wanted to do.

    Code (csharp):
    1.  
    2. public NetworkConnectionError Connect()
    3. {
    4.     return Network.Connect(ip, port);
    5. }
    6.  
    7. public void Foo()
    8. {
    9.     NetworkConnectionError e = Connect();
    10.     if (e == NetworkConnectionError.NoError)
    11.         // go to lobby screen
    12.     else
    13.         // display some message to player that they can't connect
    14. }
    15.  
     
    Last edited: Feb 21, 2013
  5. magnusDB

    magnusDB

    Joined:
    Dec 12, 2013
    Posts:
    2
    I have noticed the same issue. Furthermore, it seens that, when you call a MasterServer function and it triggers an OnFailedToConnectToMasterServer event (like, let's say my own MasterServer is not running yet), Unity stops trying to comunicate to the MasterServer even if you call the MasterServer functions like RequestHostList() or RegisterHost(), and start the Master Server service.
     
  6. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    241
    I have tha same problem. RequestHostList and RegisterHost don't work if the connection failed once. You have to restart the Game. Someone has a solution for this?

    I already used Network.Disconnect(), but still the same problem.