Not logged in. · Lost password · Register
Forum: MatriX RSS
Avatar
Geetha #1
Member since Oct 2012 · 9 posts
Group memberships: Members
Show profile · Link to this post
Subject: socketException
Socket exception is rising when my app switches from background to foreground. 

I found that OnClose event is not firing every time app comes to foreground. Trying to send a packet in that case leads socketException.

The stacktrace of the exception is as follows.


StackTrace:

System.Net.Sockets.SocketException was unhandled
  Message=A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
  ErrorCode=10057
  StackTrace:
       at Matrix.Net.ClientSocket.Send(Byte[] data)
       at Matrix.XmppStream.?(String ?)
       at Matrix.XmppStream.?(String ?)
       at Matrix.XmppStream.SendStreamHeader()
       at Matrix.XmppStream.3q2mzyj4wtv734ttjnj6bnrw7kquujzy? ??(Object ?, EventArgs ?)
       at Matrix.Net.BaseSocket.FireOnConnect()
       at Matrix.Net.ClientSocket.?(Object ?, SocketAsyncEventArgs ?)
       at System.Net.Sockets.SocketAsyncEventArgs.OnCompleted(SocketAsyncEventArgs e)
       at System.Net.Sockets.SocketAsyncEventArgs.OnCompletedInternal(SocketAsyncEventArgs e)
       at System.Net.Sockets.SocketAsyncEventArgs.FinishOperationSuccess(SocketError socketError, Int32 bytesTransferred, SocketFlags flags)
       at System.Net.Sockets.Socket.AsyncCallback(IAsyncResult ar)
       at System.Net.LazyAsyncResult.InvokeCallback(Boolean asyncCompletion)
       at System.Net.Sockets.Socket.SocketAsyncRequestManager.WorkerThread.doWork()
       at System.Net.Sockets.Socket.SocketAsyncRequestManager.WorkerThread.doWorkI(Object o)
       at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadPool.WorkItem.doWork(Object o)
       at System.Threading.Timer.ring()


Using BOSH transportation, it works fine. It only occurs while using socket transportation.
Avatar
Alex #2
Member since Feb 2003 · 4295 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
This should be fixed in the latest release. Please download the latest binary.

The problem is that WP7 stops the execution of all code when your apps goes in background, and it hard disconnects socket without giving us any chance to close the xmpp connection properly.

I suggest to unsubscribe to all your Matrix handlers in the Application_Deactivated event and call Close(). When you want to reconnect in the Application_Activated event then create a new XmppClient and subscribe to the handlers again. This is the safest method. Because the phone stops all code of MatriX in the middle of a proper shutdown and continues this code when the app gets reactivated.
Avatar
Geetha #3
Member since Oct 2012 · 9 posts
Group memberships: Members
Show profile · Link to this post
Thank you very much for your quick reply!!!!!
Avatar
Geetha #4
Member since Oct 2012 · 9 posts
Group memberships: Members
Show profile · Link to this post
Subject: SocketException in poor network Conditions
Hi Alex,

I noticed socketException while trying to send a packet, if the Network connection lost.

In case of BOSH transportation, if the Network connection lost, OnClose event fires. In that i am opening xmpp. It works fine.

OnClose doesn't fire every time in socket transportation. so i tried to reconnect in OnError event there i got SocketException.

The stackTrace is as follows.

Stack Trace:

System.Net.Sockets.SocketException was unhandled
  Message=A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
  ErrorCode=10057
  StackTrace:
       at Matrix.Net.ClientSocket.Send(Byte[] data)
       at Matrix.XmppStream.?(String ?)
       at Matrix.XmppStream.?(String ?)
       at Matrix.XmppStream.?(Object ?, EventArgs ?)
       at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Will you please tell me how to handle Network lost and also availability of network.
Thank you in advance.
Avatar
Alex #5
Member since Feb 2003 · 4295 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
you cannot reconnect directly in the OnError event. When this event is fired MatriX is still in a callback of the socket code.
When you loose your conection the OnClose event must fire. But it can take some time until the OS detects the socket loss and notifies MatriX.
Avatar
SiamakM #6
Member since Nov 2012 · 3 posts · Location: Canada
Group memberships: Members
Show profile · Link to this post
In reply to post #2
Hi Alex,

I have downloaded the latest version (Matrix.dll - 562 KB - Sep 13 2012 09:29:47 AM) and I do call Close() in Application_Deactivated, and in Application_Activated create a new XmppClient and call Open().
This method fixes the problem very good, but still it is not 100%! <<<<<

How I can break is if I repeatedly put the application in background (Windows Key) and bring it back (Back Key), after several times (maybe trying about a minute or so, you have to press the two keys very fast) I can eventually cause the following exception in Application_UnhandledException:

>>>> System.Net.Sockets.SocketException: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
[928]    at Matrix.Net.ClientSocket.Send(Byte[] data)
[928]    at Matrix.XmppStream.?(String ])
[928]    at Matrix.XmppStream.](String ])
[928]    at Matrix.XmppStream.](Object ], EventArgs ?)
[928]    at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
[928]    at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)


Obviously I don't want my application to terminate in Application_UnhandledException, so I set ApplicationUnhandledExceptionEventArgs e.Handled = true; and continue with running the application.

But my problem now is that the above exception never ends!!!
Once this starts happening, I keep getting the same exception every '10 seconds' <<<<<<<<
Even though I call _xmppClient.Close() and expect that to clear it, but that doesn't seem can terminate XmppClient session and end the error.
Looks like there is a timer in one of XmppClient background threads that does this check periodically and ignores Close().

We'd like to purchase the XmppClient but this is holding us at the moment :(
Could you please help me with how we could prevent this recurring exception?

Thanks

Siamak
Avatar
Alex #7
Member since Feb 2003 · 4295 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Please attach an example (test case)  and detailed instructions how we can cause this exceptions.
Then we will try to debug this.

Alex
Avatar
SiamakM #8
Member since Nov 2012 · 3 posts · Location: Canada
Group memberships: Members
Show profile · Link to this post
Ok, I have created a simple Windows Phone test program. It might take you a while to make this exception happen, but please keep trying and it will happen! In our dev environment this happens only once or twice a week, so it's very rare, but with this test app you can see it happen after several minutes.

Please see attachment MatrixException.zip
Read _Readme.text file for instructions.
The author has attached one file to this post:
MatrixException.zip 378.4 kBytes
You have no permission to open this file.
Avatar
Alex #9
Member since Feb 2003 · 4295 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
a new beta build is attached. Please let me know if this fixed your problems. Then this build goes to the latest binaries.

Alex
This post was edited on 2012-11-14, 08:34 by Alex.
Edit reason: removed attachment, fix is in release now.
Avatar
SiamakM #10
Member since Nov 2012 · 3 posts · Location: Canada
Group memberships: Members
Show profile · Link to this post
Alex, thank you for quick response.

The new Matrix.dll seems to be good so far, I cannot replicate the problem with this dll. I think you have solved the issue with SocketException.

We are in the middle of testing this, so I will let you know if we find any other problems.

Thanks

Siamak
Avatar
Alex #11
Member since Feb 2003 · 4295 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Thanks for your confirmation. This version goes to release then.

Alex
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: MatriX RSS