且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

客户端断开连接后,Indy服务器TIdTCPServer.OnExecute连续调用

更新时间:2022-02-21 22:15:50

解决方案:请勿使用 try ...除外来捕获和忽略 OnExecute 中的所有异常.

Solution: do not use try ... except to catch and ignore all exceptions within OnExecute.

这将吞噬Indy异常,该异常在客户端断开连接时发生,如果 IOHandler.InputBuffer 中仍然有数据,服务器将执行 OnExecute 的下一次迭代,和 ReadBytes 引发另一个异常,因为对等方断开连接.

This will swallow the Indy exception which happens when the client disconnects, the server will execute the next iteration of OnExecute if the IOHandler.InputBuffer still has data in it, and ReadBytes raises another exception because the peer disconnected.

如果引发任何异常并退出该方法,则Indy TCP服务器将关闭并清理连接.

If any exception is raised and leaves the method, the Indy TCP server will close and clean up the connection.

Indy在 OnExecute 中将异常用于错误处理和通知,因此请不要使用空"异常处理程序来抑制它们.如果需要捕获异常,请重新捕获捕获的所有 EIdException 派生的异常,然后由服务器处理.

Indy uses exceptions for error handling and notifications in OnExecute, so do not suppress them with an "empty" exception handler. If you need to catch exceptions, re-raise any EIdException-derived exceptions you catch and let the server handle them.

p.s .:发布的代码似乎使用了非本地变量 b .因为 TIdTCPServer 是多线程组件,所以必须始终以线程安全的方式访问非局部变量,例如使用关键部分,或将变量放入 TIdContext 对象.

p.s.: the posted code seems to use a non-local variable b. Because TIdTCPServer is a multi-threaded component, non-local variables must always be accessed in a thread-safe way, for example using a critical section, or putting the variable inside of the TIdContext object.