C#SocketException不会被捕获 [英] C# SocketException doesn't get caught
问题描述
我的代码中出现了一个非常奇怪的现象.我正在开发一个c#聊天客户端-服务器应用程序.当我关闭服务器时,我希望客户端自动关闭.客户端正在使用StremReader从TcpClient对象读取.客户端处于while循环中,在该循环中,它读取一行(StreamReader.ReadLine()),然后对其读取的行进行一些操作.当服务器关闭时,我还关闭了服务器端的tcp连接.因此,我希望客户端看到由readline引起的SocketException,将其捕获并退出.但是,异常不会被捕获!这是客户端循环代码:
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);
}
当我关闭服务器时,Visual Studio告诉我System.dll中出现了"System.Net.Sockets.SocketException"第一次机会异常,但是我无法捕获它.为什么会这样呢?我还尝试使用
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.
任何帮助将不胜感激!
再尝试了几次之后,我实际上发现SocketException根本没有引发.就像我在评论中所说的那样,这真是太奇怪了,在相反的情况下,当客户端在服务器之前关闭时,就会引发异常,并且我可以取消它.我真的不知道发生了什么....
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....
推荐答案
如果我很了解您的情况,则是您在 TcpListener
对象" _server.Stop()
",当在流上调用 Read
时,它不会在客户端抛出 SocketException
...我不知道为什么这正在发生,但是我有工作要做.通过访问 TcpListener
上的基础 Socket
并在其上调用 Shutdown
:
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
您在评论中表示:
实际上,我正在关闭侦听器在accept方法上返回的tcpclient.connClient.Close()
actually i'm closing the tcpclient returned by the listener on the accept method. connClient.Close()
如果要停止 tcpListerner
" _server.Stop()
",然后关闭从 _server.AcceptTcpCleint()
方法,然后在 reader.ReadLine()
处将抛出 IOException:无法从传输连接中读取数据:阻塞操作被对WSACancelBlockingCall <的调用中断了/code>我自己进行了测试.
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.
这篇关于C#SocketException不会被捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!