Alchemy-Websockets - WebSocketClient throws a NullReferenceException

I have two C # programs that should communicate via WebSockets, and the server has been working fine (also using Alchemy) and for some time now (with javascript clients). However, an Alchemy WebSocketClient connection causes a NullReferenceException be NullReferenceException on the client side.

After some debugging, I found that the exception was thrown on line 187 of WebSocketClient.cs due to a call to _context.Connection.Connected . _context.Connection is the same instance as the local _client , and none of them are null at this point in time. The TcpClient.Connected page on msdn.microsoft.com lists possible exceptions (indicating that it should not throw any exceptions).

Going back, I noticed that the variable of the _client class _client changed in line 176 of WebSocketClient.cs from a regular looking instance to an instance filled with exceptions:

BEFORE LINE 176 To line 176

AFTER LINE 176 After line 176

Immediately after this line, the Dispose () method for Context , which has just been added on line 187, is called Connection disconnecting (which is the same instance as _client that I mentioned earlier on line 131 Context.cs .

My question is: why is this happening and how can I stop it and successfully connect to the desired WebSocketServer?

If you need more information, please feel free to ask me in the comments.

EDIT 1

An exception occurs only when the server is actually listening, if the server is inactive on line 176 WebSocketClient.cs not reached (this is the correct behavior because OnRunClient() is called when creating a connection to the server).

EDIT 2

As requested by StackTrace:

 System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=System StackTrace: at System.Net.Sockets.TcpClient.get_Connected() at Alchemy.WebSocketClient.SetupContext(Context context) in C:\...\Alchemy\WebSocketClient.cs:regel 187 at Alchemy.WebSocketClient.HandleClientThread() in C:\...\Alchemy\WebSocketClient.cs:regel 94 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: 
+6
source share
1 answer

As I can see, you are using TcpClient. This class uses the Socket class to transfer data (TcpClient has such a field). As far as I know, in some cases this field can be zero. The property associated with TcpClient uses the Connected property of the internal Socket instance. Something like that:

 public bool Connected { get { return _socket.Connected; // Possible NullRef here!!! } } 

So, when this field is null, you will get a NullReferenceException. As far as I know, this can happen if the connection was not successfully established.

For example, I see this in your code:

 try { _client.EndConnect(result); } catch (Exception ex) { Disconnect(); connectError = true; } using (_context = new Context(null, _client)) { _context = new Context(null, _client); _context.BufferSize = 512; // some code } 

If the connection goes wrong, you will get an exception and take some action, but in this case your _client will not initialize the Socket field inside. In the context constructor, you try to use it in the field (in the connection), and you get a NullReferenceException.

I think you need to add return to catch catch.

OR

Check the base Client property of your TcpClient ( http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.client(v=vs.110).aspx ) as follows:

 if (_client.Client != null && _client.Connected) { // do what you need } 

Hope this helps you.

+2
source

Source: https://habr.com/ru/post/958562/


All Articles