Exception when trying to send handshake

Oct 2, 2012 at 6:21 PM

There is a small window that causes an exception when the web socket is trying to connect and the application calls close before calling the SendHandShake method.  Is there a way for the websocket handles the exception?

 

System.Exception: The socket is not connected! ---> 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   --- End of inner exception stack trace ---   at SuperSocket.ClientEngine.TcpClientSession.DetectConnected()   at SuperSocket.ClientEngine.TcpClientSession.Send(Byte[] data, Int32 offset, Int32 length)   at WebSocket4Net.Protocol.DraftHybi10Processor.SendHandshake(WebSocket websocket)   at WebSocket4Net.WebSocket.OnConnected()   at WebSocket4Net.WebSocket.client_Connected(Object sender, EventArgs e)   at SuperSocket.ClientEngine.ClientSession.OnConnected()   at SuperSocket.ClientEngine.SslStreamTcpSession.OnAuthenticated(IAsyncResult result)   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)   at System.Net.Security.SslState.FinishHandshake(Exception e, AsyncProtocolRequest asyncRequest)   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)   at System.Net.Security.SslState.ReadFrameCallback(AsyncProtocolRequest asyncRequest)   at System.Net.AsyncProtocolRequest.CompleteRequest(Int32 result)   at System.Net.FixedSizeReader.CheckCompletionBeforeNextRead(Int32 bytes)   at System.Net.FixedSizeReader.ReadCallback(IAsyncResult transportResult)   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)   at System.Net.ContextAwareResult.CompleteCallback(Object state)   at System.Threading.ExecutionContext.runTryCode(Object userData)   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)   at System.Net.ContextAwareResult.Complete(IntPtr userToken)   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)   at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

Oct 9, 2012 at 8:54 PM

There is a similar exception on Close also.

System.Exception: The socket is not connected! ---> 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
   --- End of inner exception stack trace ---
   at SuperSocket.ClientEngine.TcpClientSession.DetectConnected()
   at SuperSocket.ClientEngine.TcpClientSession.Send(Byte[] data, Int32 offset, Int32 length)
   at WebSocket4Net.Protocol.DraftHybi10Processor.SendCloseHandshake(WebSocket websocket, Int32 statusCode, String closeReason)
   at WebSocket4Net.WebSocket.Close(Int32 statusCode, String reason)
   at WebSocket4Net.Command.Close.ExecuteCommand(WebSocket session, WebSocketCommandInfo commandInfo)
   at WebSocket4Net.WebSocket.ExecuteCommand(WebSocketCommandInfo commandInfo)
   at WebSocket4Net.WebSocket.OnDataReceived(Byte[] data, Int32 offset, Int32 length)
   at WebSocket4Net.WebSocket.client_DataReceived(Object sender, DataEventArgs e)
   at SuperSocket.ClientEngine.ClientSession.OnDataReceived(Byte[] data, Int32 offset, Int32 length)
   at SuperSocket.ClientEngine.SslStreamTcpSession.OnDataRead(IAsyncResult result)
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
   at System.Net.Security._SslStream.ProcessFrameBody(Int32 readBytes, Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.ReadFrameCallback(AsyncProtocolRequest asyncRequest)
   at System.Net.AsyncProtocolRequest.CompleteRequest(Int32 result)
   at System.Net.FixedSizeReader.CheckCompletionBeforeNextRead(Int32 bytes)
   at System.Net.FixedSizeReader.ReadCallback(IAsyncResult transportResult)
   at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
   at System.Net.ContextAwareResult.CompleteCallback(Object state)
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.ContextAwareResult.Complete(IntPtr userToken)
   at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
   at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

Any ideas to avoid the exception?  The only thing I can think of doing is to modify the source code and I do not want to do that. 

 

Thanks for your help.

Coordinator
Oct 10, 2012 at 2:42 AM

Did you sync to the latest code? I did a change to fix a related issue one these days.

Oct 10, 2012 at 3:24 AM

No, I didn't know you had a fix.  I will get the latest and see if I still see the problem.  Thank you.

Coordinator
Oct 10, 2012 at 3:47 AM

BTW, could tell me your test work flow about this issue. I probably consider add it into WebSocket4Net testcases.

Oct 10, 2012 at 4:17 AM

I'm just trying to reconnect and most of the time works, just one a while it fails.  I can't reproduce the problem all the time.  In fact, it is hard to reproduce it. I will look into the work flow and let you know.

Oct 10, 2012 at 4:17 AM

Do you have plan to make a release build?

Coordinator
Oct 10, 2012 at 4:18 AM

Yes, but I am still checking other issues.

Oct 10, 2012 at 11:07 PM

When I built the WebSocket4net dll is different than the one you release.  Is there a way to package it like the way you do?  You have one SuperSocket.ClientEngine dll and Simple.Json.

Coordinator
Oct 11, 2012 at 2:49 AM

In latest source code, Simple.Json is not required.

Build the project like other normal project will get what you want. All merge script has been put in build event.

The build script Build.bat also can build all the projects without VS.

Oct 16, 2012 at 10:47 PM

Thank you for fixing the exception.  I have been testing the reconnect and I haven't seen the exception on sending handshake.  However, I got a familiar exception sending ping message.  Here is the stack trace.

 System.Exception: The socket is not
connected! ---> 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
  --- End of inner exception stack trace ---
  at SuperSocket.ClientEngine.TcpClientSession.DetectConnected()
  at SuperSocket.ClientEngine.TcpClientSession.TrySend(ArraySegment`1 segment)
  at SuperSocket.ClientEngine.ClientSession.Send(ArraySegment`1 segment)
  at WebSocket4Net.Protocol.DraftHybi10Processor.SendMessage(WebSocket
websocket, Int32 opCode, String message)
  at WebSocket4Net.Protocol.DraftHybi10Processor.SendPing(WebSocket
websocket, String ping)
  at WebSocket4Net.WebSocket.OnPingTimerCallback(Object state)
  at System.Threading._TimerCallback.TimerCallback_Context(Object state)
  at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state, Boolean
ignoreSyncCtx)
  at System.Threading._TimerCallback.PerformTimerCallback(Object state)