Unity 5.1.1f1 Asynchronous Sockets always disconnect when recieved messages

I try to use IL2CPP → Universal to get the 64-bit IOS app, but the socket always disconnect when recieved messages.
When I use mono2x and set the stripping-level disabled,it works ok, but now I want to get the 64-bit app and let the asynchronous sockets work ok .
Please help me.!!!

@JoshPeterson

@JoshPeterson

error : System.Net.Sockets.SocketException: Operation on non-blocking socket would block
at System.Collections.Specialized.ListDictionary+DictionaryNodeEnumerator.MoveNext () [0x00000] in :0
at System.Net.Sockets.Socket+SocketAsyncResult.CheckIfThrowDelayedException () [0x00000] in :0
at System.Net.Sockets.Socket.EndReceive (IAsyncResult asyncResult, SocketError& errorCode) [0x00000] in :0
at System.Net.Sockets.Socket.EndReceive (IAsyncResult result) [0x00000] in :0
at SocketManager.ReceiveCallback (IAsyncResult ar) [0x00000] in :0
at System.AsyncCallback.Invoke (IAsyncResult ar) [0x00000] in :0
at System.Net.Sockets.Socket+SocketAsyncResult.Complete () [0x00000] in :0
at System.Net.Sockets.Socket+Worker.Receive () [0x00000] in :

this is my error stack.

and this is my playersettings

Unity5.1.1f1

2209783--146915--ss.jpg

@asciima

We’ll likely need to have a look a this project. Can you submit a bug report with this issue? We did see one similar issue, but it was actually caused by a subtle race condition in the project itself, and that race condition was exposed by the switch to IL2CPP. I can’t say yet whether this is a similar issue though.

@JoshPeterson

Thank u very much.

Can u give me some tips?
What caused this error, my codes or IL2CPP settings ?

@asciima

In the other case we saw, the issue was a race condition, where the exception was actually valid, because a call to Receive was being made on a non-blocking socket when there was no data to Receive. The response from the server side had not yet come back in some cases. The Mono scripting backend code was a bit slower to execute, so the response was always back in time, and the error did not occur.

With non-blocking sockets, it is usually necessary to do some synchronization or to handle this exception and try to call Receive again later.

In any case though we would need to try your project here to be sure.

Thank u for your reply .

In my codes, I called BeginRecieve with the AysncCallback, when AsyncCallback was executed, then call next BeginRecieve,and wait for AsyncCallback repeated .

I called the asynchronous BeginRecieve ->Receive Callback in my codes. Mono2x is ok, but when I use IL2CPP-> universial , it usually could connect success. When receiving messages, the socket sometimes worked ok and could got the message, sometimes not ok and throw the exception I got for you.

My confusion is that what is the difference between Mono2x and IL2CPP when the asynchronous socket was used. and what can i do for this exception?

@JoshPeterson
Thank u very much.:slight_smile:

@asciima

It is difficult to know exactly what to do without seeing the exact code. Since it seems to work sometimes, I suspect that if you get this exception, you’ll need to catch it, then try again. You may need to retry for a fixed number of times so as not to get into an infinite loop.

The main difference between Mono and IL2CPP in this case is that the Mono scripting backend code is a bit slower to execute than IL2CPP. However, the API was not changed at all.

if others have the similar problems with this like my problem?

@asciima
I have the same problem like yours.Did you fixed it?

@JoshPeterson
Have you found a solution to this problem?

@glegoo

We’re looking at solutions now, but nothing is completed yet. I’ll respond on this thread if/when we get something working for this.

You can also watch the public link for this bug report: http://issuetracker.unity3d.com/issues/il2cpp-notsupportedexception-is-thrown-when-calling-socket-dot-dispose-from-httplistener

@JoshPeterson

My English is so poor, and i wish you can understand what i say.
I fixed this problem by this solution.
Before I fix this, the connect callback method is like this.

        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                ar.AsyncWaitHandle.Close();
                m_ar_Connect = null;
                Socket socket = (Socket)ar.AsyncState;
                socket.EndConnect(ar);
                socket.Blocking = false;
                if (m_ClientSocket != null)
                {
                    m_ClientSocket.ReceiveTimeout = 3000;
                    m_ClientSocket.SendTimeout = 3000;
                }
                Receive();
                m_eNetWorkState = EClientNetWorkState.E_CNWS_CONNECTED;
            }
            catch (Exception e)
            {
                DidConnectError(e);
            }
        }

It’s worked good at mono 2.0 but il2cpp.
Then I deleted the part of blocking setting and timeout setting, like this.

        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                ar.AsyncWaitHandle.Close();
                m_ar_Connect = null;
                Socket socket = (Socket)ar.AsyncState;
                socket.EndConnect(ar);
                Receive();
                m_eNetWorkState = EClientNetWorkState.E_CNWS_CONNECTED;
            }
            catch (Exception e)
            {
                DidConnectError(e);
            }
        }

I don’t know why but it’s worked!
Maybe this will be helpful.

@glegoo

Thanks for posting this work around.

Internally we have Thread::Abort and Thread::ResetAbort working now, so we’re hoping to ship a solution for this issue soon, likely 5.3.0p2 release.

Setting socket.Blocking= true,
or comment // socket.Blocking=false;

this unity IOS/IL2CPP Bug, the reason (socket.Blocking=false;)
Solution :
Setting socket.Blocking= true,
or comment // socket.Blocking=false;

1 Like