Search Unity

Network Socket Exception Problem

Discussion in 'Multiplayer' started by Frieza, Aug 29, 2008.

  1. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    Hi Guys,

    I'm having some difficulties with developing a solid network interface in Unity using C# (before you ask, yes, I'm developing my own network stuff as I have some specific needs).

    I have an object which lurks on the playing field with a script attached to it (by the name NetworkLayer.cs), occasionally the user may need to change map, and I am doing so thusly:

    // called from the network obj script
    DontDestroyOnLoad(this);
    Application.LoadLevel("some level");


    The network stuff itself is using the C# TcpClient class (will do UDP also later, but one thing at a time) and data is sent/recieved using the Stream class.

    Just to really spice things up, I achieve my non-blocking fun via means of a service thread which allows me to listen on the network for messages while allowing the game to run as expected. Said functionality is from the Thread/ThreadStart stuff.

    So far, so normal C#. Nothing particularly thrilling here.

    However I am recieving some really weird funk whenever I switch map.

    SOMETIMES it works flawlessly and I am transferred to the next map without error and the connection stays alive.

    However, sometimes I am thrown clean out with the following exception:

    System.IO.IOException: Read Failure ---> System.Net.Sockets.SocketException: The descriptor is not a socket at System.Net.Sockets.Socket.Recieve(System.Byte[] buf,Int32 offset,Int32 size,SocketFlags flags) [0x00000]

    Arrrgh the dreaded null! I'm definitely not killing threads or objects myself so I don't understand what I'm doing wrong.

    The read thread is infact still alive and quite happy despite all this (as it is this thread that informs me of the exception).

    The Network Object itself *should* (and in the inspector appears to) remain in the next map, and it's even weirder that the error happens on random occasion.

    To me, it sounds like the socket itself has been destroyed in some hideous fashion but I am unsure how/why it's destroying other than switching map is causing it.

    Any ideas?

    Many thanks in advance!
     
  2. AmazingRuss

    AmazingRuss

    Joined:
    May 25, 2008
    Posts:
    933
    Since you're running another thread, I would make very sure you don't have any concurrency issues...maybe you have your thread writing to some data structure that unity also accesses, and are sometimes hitting it in an inconsistent state.

    The other thing you might try is putting your socket thread to sleep before loading the next level and awakening it after the level loads...if you can stand a little hackyness :)
     
  3. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    Thanks for the suggestions, I'll give the sleep stuff a try for sure and I'll double check that the thread isn't accessing anything it shouldn't be (but it doesn't seem to be). I'll give it a whirl and let you know how it goes, thanks!
     
  4. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    Sadly it wasn't anything to do with this as far as I can tell. Even without the socket read thread doing anything during the switch over, the socket itself seems to just vanish into thin air.

    For what it's worth, the read thread ONLY writes into member variables of the same class it is in (NetworkLayer.cs), all of the network stuff is self-contained in this script, and it is up to other pieces of scripts to pull data from the variables of the network layer. The socket itself is private so I don't see why it's being destroyed when I go from one screen to another and even then only on random occasion.
     
  5. AmazingRuss

    AmazingRuss

    Joined:
    May 25, 2008
    Posts:
    933
    That's too bad. At this point I would start a fresh project and add just the socket code and some empty scenes to switch between, then add things until it breaks.

    Good luck
     
  6. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    Do you have any code in Awake, OnLevelWasLoaded, OnEnable, OnDisable, etc. that might be trying to reinit. the socket?