Search Unity

Async sockets and unity editor

Discussion in 'Multiplayer' started by perlohmann, May 5, 2009.

  1. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
    Can anybody tell me if there are some issues with the unity editor and async socket calls? dunno if it could be some mono issue too (using a Mac and unity 2.1).

    Im currently having issues with a async socket. when i run it in the editor i just crashes no exceptions no anything except unity crash. (i have try's everwhere to attempt to catch exceptions). But the funny thing with this is that it only crashes the second time i run the game first time functions just fine. it also works when i build the game (most likely cuzz you cant "restart" it the same way as in the editor).

    i use socket option reuse address so its not that (that also throws and exception which i know i have catched earlier)

    a very short example of the code im writing:

    Code (csharp):
    1.  
    2.  
    3. int fixedPort = 24999;
    4. Socket socket = null;
    5. EndPoint serverEndpoint;
    6. byte[] buffer = new byte[1400];
    7. ASyncCallback endReceive = new ASyncCallback(EndReceive);
    8.  
    9. Start()
    10. {
    11.   try
    12.   {
    13.      socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    14.      socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
    15.      socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
    16.      socket.Bind(localEndpoint);               
    17.  
    18.      serverEndpoint = new IPEndPoint(IPAddress.Broadcast, fixedPort);
    19.      socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref serverEndpoint, endReceive , socket);
    20.   }
    21.   catch (Exception error)
    22.   {
    23.     //bla bla bla.
    24.   }
    25. }
    26.  
    27. EndReceive(IAsyncResult ar)
    28. {
    29.     Socket theSocket = (Socket)ar.AsyncState;
    30.     int bytesRead = theSocket.EndReceiveFrom(ar, ref serverEndpoint);
    31.     if(bytesRead > 0)
    32.     {
    33.        //proccess received data...
    34.        try
    35.        {
    36.            //continue receiving
    37.            theSocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref serverEndpoint, endReceive , theSocket);
    38.        }
    39.        catch (Exception error)
    40.        {
    41.            //bla bla bla
    42.        }
    43.     }
    44. }
    45.  
    46. Stop()
    47. {
    48.    if (socket != null)
    49.    {
    50.       socket.Close();
    51.       socket = null;
    52.    }
    53. }
    54.  
    55.  
    56.  
    As i said this works fine the first time. but the 2nd time it is run in the editor unity crashes.

    //Perlohmann
     
  2. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
    apparently this wasn't reproducible in Unity 2.5 on windows vista.

    still have not found out what made the mac 2.1 unity editor crash though =(
     
  3. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
    bumping as it does it on Mac 2.5 too =/ but again it works perfectly on windows.

    =(
     
  4. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    When you hit play to stop the game in the editor, it does so very abruptly. Your sockets will not get closed and the OS will think the associated ports are still blocked. So when you run it a second time, there will be an exception.

    So to test it, before stopping your app via play button, close your sockets through an ingame event.

    This is my best guess, because I used to have the same problem in Unity 1. Now I just use Unitys networking, because I can't figure out how to do NAT punchthrough manually ;)
     
  5. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
    well there is actually an OnQuit event you can hook on to in classes dereiving from monobehaviour. This function will get called when you hit the play button (to stop it) but despite of hooking up to this event and stopping and closing the socket it still crashes on Mac.

    But again it dosnt crash on unity windows 2.5.

    btw thx "Der Dude" for the feedback (both here and in the other thread) its nice to know Im not the only one experiencing these problems.

    //perlohmann
     
  6. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Hey no problem. I've been there too a lot of times.

    Are you sure OnQuit exists? I couldn't find it in the docs.
     
  7. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
    ohh sry its called OnApplicationQuit()..... had to boot to Mac partition to check it out ;o)
    The OnQuit one is the one I made myself before I found the event ;o)
     
  8. fiedler

    fiedler

    Joined:
    Jun 23, 2009
    Posts:
    1
    I had the same problem. With this code it works for me:
    Code (csharp):
    1.  
    2. void OnApplicationQuit()
    3. {
    4.     this.socket.Shutdown(SocketShutdown.Both);
    5.     this.socket.Close();
    6. }
    7.  
    The Shutdown method seems to do the trick.
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    There are two things involved I think

    1. The sockets need to be closed again or will block as they are async and won't quit till you quit the .NET environment created by Unity
    2. With Unity 2.1, there were generally a few issues with async sockets that were fixed in 2.5, which also might mix in
     
  10. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
    1. They were closed. (tried with shutdown too, but msdn discourage the use of shutdown when using UDP)

    2. Well it was in 2.5 on Mac too.

    But anyways works like a charm on windows.

    //perlohmann