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:
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.
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.
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.
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.