简体   繁体   English

在阻塞模式下使用TServerSocket时如何处理异常?

[英]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: 我的问题是:哪种方法最好?

  1. Use a socket error event handler to deal with everything (this is nice as I can get the socket error code to display for debugging purposes) 使用套接字错误事件处理程序来处理所有事情(这很好,因为我可以获取套接字错误代码以进行调试)
  2. Use the try-catch statements to prevent my application from throwing exceptions 使用try-catch语句防止我的应用程序引发异常
  3. Use both (although not set the error code to zero in the handler otherwise the exception will not get thrown making 2. above pointless) 两者都使用(尽管不要在处理程序中将错误代码设置为零,否则将不会抛出异常,使以上2点毫无意义)

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.

相关问题 为什么在阻塞模式下不会为TServerSocket触发OnClientConnect和OnClientError事件 - Why do OnClientConnect and OnClientError events not fire for TServerSocket in blocking mode 使用DLLImport时,异常如何工作 - How Do Exceptions Work When Using DLLImport 当使用 lambdas 进行复杂的变量初始化时,如何处理从内部抛出的 lambda 异常? - When using lambdas for complex initialization of variables, how can I handle outside of the lambda exceptions that are thrown from inside? 你如何使用CUFFT的批处理模式? - How do you use the batch mode of CUFFT? 你如何等待 `QtConcurrent::run` 在没有线程阻塞的情况下完成? - How do you wait for `QtConcurrent::run` to finish without thread blocking? 你如何处理C ++中的字符串? - How do you handle strings in C++? 如何处理失败的方法:通过使用异常或使方法返回bool? - How to handle failed methods: by using exceptions or making the methods return bool? 从Lua调用函数时如何处理C ++异常? - How to handle C++ exceptions when calling functions from Lua? 当URI不好时,如何处理来自StorageFile :: OpenAsync的异常 - How to handle exceptions from StorageFile::OpenAsync when URI is bad 将范围传递给C ++算法时,如何处理iterator / const_iterator不匹配? - How do you handle iterator/const_iterator mismatches when passing ranges to C++ algorithms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM