There has been a lot of talk about how we can do multiplayer on iPhone using .NET sockets, but I’ve tried numerous methods (that all work in the editor) but don’t work on the device. It appears that Mono’s implementation of the System.Net framework is seriously bugged.
For starters, you can’t even do trivial things like:
IPAddress ipAddr = new IPAddress("127.0.0.1");
but you can do:
IPAddress idAddr = new IPAddress(0x0100007F);
Also, none of the Dns methods seem to work.
I’ve tried using TcpClient with TcpListener with no avail, UdpClient with no avail, and I’ve even tried going down to using the Socket class, again with no avail.
Has anyone had any luck with this? For example, does anyone have any working code for something like a simple TCP chat client or some code to do UDP broadcasts and listen on them?
Thanks in advance.
(We have Unity Pro)
The SmartFoxServer API uses sockets and the sources are available to run through.
So yes - its possible. And yes - there are limitations on e.g. hostname lookups.
/Thomas
Cool, thanks. I’ll check it out.
Ok, so I tried running:
IPAddress ip = IPAddress.Parse("127.0.0.1");
TcpClient client = new TcpClient();
client.Connect(ip, 9339);
With various IP addresses and ports. It works in the editor, but on the device I get:
Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for System.Net.Sockets.Socket ---> System.Net.Sockets.SocketException: An address incompatible with the requested protocol was used
at System.Net.Sockets.Socket..ctor (AddressFamily family, SocketType type, ProtocolType proto) [0x00000]
at System.Net.Sockets.Socket.CheckProtocolSupport () [0x00000]
at System.Net.Sockets.Socket..cctor () [0x00000]
--- End of inner exception stack trace ---
at System.Net.Sockets.TcpClient.Init (AddressFamily family) [0x00000]
The code is exactly the same as used in the SmartFoxClient.
Any ideas?
Curious about this as well. Anyone? Or can anyone comment if sockets at least work in the upcoming 1.1 release?
I’m having the same issue as well. Anyone from Unity?
sockets have worked with the original release and since 1.5 the async don’t create headache anymore too.
This problem here sounds a lot like using the wrong way to generate the ip adress, so you actually don’t get a valid one to use to connect too.
Just to quote the original code from the dimeRocker thread which gives you an idea of how it could look (http://forum.unity3d.com/viewtopic.php?t=27753):
var masterServerUrl = "masterserver.dimerocker.com";
var ip = System.Net.Dns.GetHostEntry (masterServerUrl);
var ipA = ip.AddressList;
if (ipA.Length > 0)
{
MasterServer.ipAddress = ipA[0].ToString();
MasterServer.port = masterServerPort;
}
This takes all the required lookup into account for your server (ensure you really have one thats reachable and accepting connections)
Also its naturally assumed that you are on iPhone Advanced, because otherwise you can not use sockets at all, you can use WWW only.
I’ve actually solved this now. There was no problem with my code after all – the problem was that it didn’t work with Strip Bytecode for some reason.
I would recommend filing a bug then. Potentially it might still be related to your code in some way but if not, its good if they are notified of such issues
I did file a bug for the original problem and had an email conversation with someone at Unity about it. However, when I told them about the stripping problem, they didn’t reply.
if you added your source that causes it they will likely look into it.
Conversations commonly only happen if they need clarification on some point, be it the provided sample or the problem they should be seeing or something alike.
I reported a few bugs and so far didn’t have conversations actually, none the less the bugs were fixed.
Just ensure that you file a new bug report for a new issue than the original one so it can be handled appropriately.
Did the problem happen with 1.0.3 or 1.5?
because there were a few issues in the stripping with 1.0.x that were solved in 1.5
I provided source and they said that they couldn’t reproduce it (it worked fine for them), which was what caused me to start playing around with the stripping level.
As you can see from date of the original post, this was a while ago, so it was probably 1.0.3. I can’t remember exactly.
Have you tried bytecode stripping? I’m having trouble w/ bytecode.
Try running:
function Update () {
System.Net.Dns.GetHostEntry (“”);
}
Like I said, it doesn’t work with Bytecode Stripping. You have to use Strip Assemblies or no stripping at all.
Also, I don’t believe the Dns class works at all in Mono, or maybe it’s just hostname lookups. I can’t remember.
Yeah looks like I will have to stick with assemblies. Thanks!