[英]How do you handle exceptions when using TServerSocket in blocking mode?
I am writing a C++ application using Embarcadero RAD studio and TServerSocket
component in blocking mode. 我正在使用Embarcadero RAD studio和
TServerSocket
组件以阻塞模式编写C ++应用程序。 I have overridden the OnGetThread
handler for the socket to create a custom class derived from TServerClientThread
which overrides the default ClientExecute()
method. 我已经为套接字重写了
OnGetThread
处理程序,以创建一个从TServerClientThread
派生的自定义类, TServerClientThread
重写了默认的ClientExecute()
方法。 Within this function I use a TWinSocketStream
and make calls to WaitForData()
, Read()
, and Write()
to receive and send data. 在此函数中,我使用
TWinSocketStream
并调用WaitForData()
, Read()
和Write()
来接收和发送数据。 According to everything I have read this is an acceptable way to go about things (although please correct me if this is wrong). 根据我读过的所有内容,这是解决问题的一种可接受的方式(但是如果这是错误的,请更正我)。
From this answer What events are fired for a blocking socket? 从这个答案中,阻塞套接字会触发什么事件? I am lead to believe that using an
OnClientError
handler in blocking mode is OK since the events will fire. 我被认为可以在阻塞模式下使用
OnClientError
处理程序,因为事件将触发。 In my event handler I set the error code to zero every time so that the exception does not get thrown. 在事件处理程序中,我每次都将错误代码设置为零,以免引发异常。
In addition, every time I call Read()
or Write()
from within my ClientExecute()
function I wrap it in a try-catch block and catch ESocketError
exceptions. 另外,每次我从
ClientExecute()
函数中调用Read()
或Write()
,我都将其包装在try-catch块中并捕获ESocketError
异常。
My question is this: Which is the best approach: 我的问题是:哪种方法最好?
This is an old component and has been deprecated but I have to use this but as I have been unable to find a cast iron guide to using it correctly I have pieced together an approach from many sources. 这是一个很旧的组件,已经过时了,但是我必须使用它,但是由于我找不到正确使用它的铸铁指南,因此我从许多来源整理了一种方法。 It has been working well for quite some time, but every now and again I get a silent error which prevents the server from accepting any further client connections - but I get no output from
OnClientError
and no ESocketErrors
are thrown. 它已经运行了好一段时间了,但是我时不时地收到一个无提示错误,该错误阻止服务器接受任何进一步的客户端连接-但是我没有从
OnClientError
获得任何输出,也没有抛出ESocketErrors
。 This application is running on an embedded device so the only way it is detected is when it becomes non-responsive. 此应用程序正在嵌入式设备上运行,因此检测到它的唯一方法是无响应。
If anyone could give me advice on which of the 3 approaches above is best (or suggest an alternative) I would be very grateful. 如果有人可以给我建议以上三种方法中哪一种最好(或提出替代方法),我将不胜感激。
If TServerSocket
is not accepting new connections anymore, then either its internal TServerAcceptThread
thread has crashed so TServerWinSocket.Accept()
is not being called anymore, or else Accept()
is being called but is encountering OS errors (lack of system resources, etc). 如果
TServerSocket
不再接受新的连接,则其内部TServerAcceptThread
线程已崩溃,因此不再调用TServerWinSocket.Accept()
,或者正在调用Accept()
但遇到操作系统错误(系统资源不足等)。 。 Either way, TServerSocket
does not expose any kind of error information from those particular areas of its code, so you have no way of detecting and handling when your server stops accepting connections, unless your connections are frequent enough that you can use a timer to detect long runs of inactivity between OnClientConnect
events. 无论哪种方式,
TServerSocket
都不会从其代码的特定区域中公开任何类型的错误信息,因此,当服务器停止接受连接时,您将无法检测和处理,除非您的连接频率足够高以至于您可以使用计时器来检测OnClientConnect
事件之间的长期不活动。 If you suspect your server has entered a non-accepting state, all you can do is have your app close and re-open the TServerSocket
. 如果您怀疑服务器已进入不接受状态,那么您所能做的就是关闭应用程序并重新打开
TServerSocket
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.