C# SocketException doesn't get caught

一笑奈何 提交于 2019-12-01 02:19:35

问题


i have a really weird sitation going on in my code. I'm developping a c# chat client-server application. When i close the server i want the client to be automatically closed. The client is reading from the TcpClient object using a StremReader. The client is in a while loop where it reads a line(StreamReader.ReadLine()) and then doing some operations with the line it reads. When the serever gets closed, i also close the tcp connection server-side. So, i'm expecting the client to see a SocketException caused by the readline, catch it and exit. But the exception doesn't get 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, visual studio tells me that a "System.Net.Sockets.SocketException" first-chance exception was raised in System.dll, but i cannot catch it. Why does that happen? i also tried to catch any generic exception with a

catch
{
}

block, but that doesn't work either.

Any help will be appreciated!

EDIT: after trying some more times, i actually found out that SocketException doesn't get raised at all. That's so weird, as i said in a comment, in the opposite situation, when the client gets closed before the server, the exception gets raised and i can canth it. I don't really know what's going on....


回答1:


If I understand you well the scenario is when you call Stop method at TcpListener object "_server.Stop()", it will not throw SocketException at the client side when call Read on the stream... I don't know why this is happening but I have a work ground around it. it is by accessing to the underlying Socket on the TcpListener and call Shutdown on it:

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

Edit: You stated at comment:

actually i'm closing the tcpclient returned by the listener on the accept method. connClient.Close()

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




回答2:


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




回答3:


visual studio tells me that a "System.Net.Sockets.SocketException" first-chance exception was raised in System.dll, but i cannot catch it. Why does that happen?

First chance exceptions are intercepted by the debugger and interrupts you. It is the second chance exception that gets you into your catch block and that's why you do not get into your catch block on first chance. See this article for more information.

Extracted from the article I mentioned above

Does a first chance exception mean there is a problem in my code? First chance exception messages most often do not mean there is a problem in the code. For applications / components which handle exceptions gracefully, first chance exception messages let the developer know that an exceptional situation was encountered and was handled.

On a side note, you can always configure your debugger to not stop on first chance exceptions.




回答4:


When you close the server socket, the other side receives a message of zero bytes. This indicates that the server has closed down properly:

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

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

Exceptions are thrown only for exceptional situations - for instance if you reboot the server computer while your client was connected to it - not for 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




回答5:


I've this problem before and turn's out that I've been activate "Break when CLR Exception thrown and didn't uncheck it back"

So,check of you didn't uncheck this feature by
Press Alt+Ctr + E scroll down to Common Runtime Language Exception and make sure to uncheck "Thrown" Check box. I hope this help you.




回答6:


This is because .Net uses unsafe methods for Send/Receive methods.you have to handle your Program Coontext's UnhandledException



来源:https://stackoverflow.com/questions/6665563/c-sharp-socketexception-doesnt-get-caught

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!