C # SocketException misses

There is a very strange movement in my code. I am developing a client-server C # chat application. When I close the server, I want the client to be automatically closed. The client reads the TcpClient object using StremReader. The client is in the while loop, where it reads the line (StreamReader.ReadLine ()), and then performs some operations on the line that it reads. When serever closes, I also close the tcp server connection. So, I expect the client to see a SocketException thrown by readline, catch it and exit. But the exception is not caught! Here's the client loop code:

while (true) { try { ricev = read.ReadLine(); } catch(SocketException exc) { //It never gets in here } chat.Invoke(showMessage, ricev); } 

When I close the server, the visual studio tells me that an exception was raised in System.dll from the first possibility "System.Net.Socket.SocketException", but I can not catch it. Why is this happening? I also tried to catch any general exception with

 catch { } 

but it doesn’t work either.

Any help would be appreciated!

EDIT: after several attempts, I found that SocketException does not occur at all. This is so strange that, as I said in a comment, in the opposite situation, when the client closes in front of the server, an exception occurs and I can throw it away. I really don't know what is happening ....

+6
source share
6 answers

If I understand well, the script is when you call the Stop method on the TcpListener object " _server.Stop() ", it will not throw a SocketException on the client side when a Read called in the stream. I don’t know why this is happening, but I have a job site. this is accessing the base Socket on the TcpListener and calling Shutdown on it:

 _server.Stop(); _server.Server.Shutdown(SocketShutdown.Both);//now at your "read.ReadLine();" will throw SocketException 

Edit: You pointed to a comment:

In fact, I close the tcpclient returned by the listener in the accept method. connClient.Close ()

If tcpListerner " _server.Stop() " stops, and then the clients method obtained from _server.AcceptTcpCleint() closes, then IOException: Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall thrown at at reader.ReadLine() IOException: Unable to read data from the transport connection: A blocking operation was interrupted by a call to WSACancelBlockingCall . I tested it myself.

+1
source

If you call Invoke , then the odds are that the exception will be wrapped in a TargetInvocationException .

0
source

visual studio tells me that a "System.Net.Sockets.SocketException" exception from the first chance was raised in System.dll, but I cannot catch it. Why is this happening?

The first random exceptions are caught by the debugger and interrupt you. This is the second random exception that gets into your catch block, and why you didn't hit the catch the first time. See the article for more information.

Extracted from the article mentioned above

Is there an exception at the first chance that there is a problem in my code? The first exception message for an exception most often does not mean that there is a problem in the code. For applications / components that handle exceptions gracefully, exception messages with the first chance allow the developer to know that an exception has occurred and are being processed.

On the one hand, you can always configure your debugger so that it does not stop at the first time exceptions.

0
source

When you close the server socket, the other side receives a message with zero bytes. This means that the server has shut down correctly:

If the remote host disconnects the Socket connection with the Shutdown method and all available data has been received, the Receive method will end immediately and return zero bytes.

http://msdn.microsoft.com/en-us/library/8s4y8aff.aspx

Exceptions are thrown only for exceptional situations - for example, if you restart the server computer while the client is connected to it, and not for the normal program flow.

The ReadLine() operation should also not throw an exception, but simply return null when this happens:

Return value

The next line from the input stream or null if the end of the input stream is reached.

http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx

0
source

I have this problem before and it turned out that I activated the "Break while throwing CLR Exception and did not cancel it"

So, to verify that you have not disabled this feature, press Alt + Ctr + E, scroll down to the Common Execution Language Exception and make sure to uncheck the Abandoned box. Hope this helps you.

0
source

This is because .Net uses unsafe methods for Send / Receive methods. You must handle your software Coontext UnhandledException

-2
source

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


All Articles