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 ....
source share