SocketException: No such host is known

Hi, I’m currently testing the networking capabilities of Unity3D in the web player. I ran into a problem where I receive the following exception only on a mac and only when I refresh the browser:

If I view the page from a PC in Windows Vista the code works fine (no exceptions). I can refresh the browser as many times as I want. I tried with IE 7 and Firefox which both work. If I view the same page on a Mac in Firefox or safari it works fine, but when I refresh the page I get the above exception.

Here is my code which is called from the Start() method:

public string TCPConnect(string host, int port)
        {
            string error = string.Empty;

            try
            {
                server = new TcpClient(host, port);
                ns = server.GetStream();
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }

            return error;
        }

I close the connections from the OnApplicationQuit() method:

public string TCPCloseConnection()
        {
            string error = string.Empty;
            try
            {
                server.Close();
                ns.Close();
            }
            catch (Exception ex)
            {
                error = ex.ToString();

            }
            finally
            {
                ns = null;
                server = null;
            }
            return error;
        }

If anyone has any insight into why the code works fine on browser through the PC but not the Mac, please let me know. I’m using Unity3d Pro 2.1 and the scripts are C# scripts.

Thanks!!

-Ian

Try calling ShutDown on the socket with SocketShutdown.Both.

Also try not doing the dns lookup.

It seems there’s a bug in Mono on OS X where doing DNS lookups fails after “something else” is done (in this case, refreshing the browser page). The bug is not on Windows. So if you would provide actual IP address instead of doing DNS host name lookup, or get IP via some other means (some server side script that returns it?), it should work for now.

Thank you very Much!

Yes, changing the connection to not use dns lookup worked!! Thank you again :smile:

Just for reference in case anyone else has a similar problem this is the new code that now works:

public string TCPConnect(string ip, int port)
        {
            string error = string.Empty;

            try
            {
                IPAddress ipAddress = IPAddress.Parse(ip);
                server = new TcpClient();
                server.Client.Connect(ipAddress, port);
                ns = server.GetStream();
            }
            catch (Exception ex)
            {
                error = ex.ToString();
            }

            return error;
        }

Also, if you are doing this stuff in another thread, there are a few threading goodies to lookout for.

Shutting down the TCP sockets in OnApplicationQuit is pretty important - although making this process work with threading is tricky.

Moving the networking threads to runInBackground seems to solve most problems related to unexpected quitting of the app - namely crashing the browser. Although this makes it likely your server won’t receive any logout messages, and basically can be interrupted anytime.

(For anyone searching the forums)

I just went back to this one to test if still present. And it seems to have been fixed. In both 2.5.1 and iPhone 1.5 I could connect with a hostname without issues - or so it seems.

Anyone else verify it?

looks to still be happening for me with unity version 2.6.1 on a mac. (web player only, of course.)

Yeah - I just ran into it again. So I was too quick on the trigger :frowning: