简单的说,当TidTCPServer调用Read方法接收数据时、或调用Write方法发送数据时,客户端主动直接断开了连接,就会触发该异常;这是正常,忽略这一错误就可以了。

Indy : Connection Closed Gracefully

Connection Closed Gracefully
Author: Chad Z. Hower
Homepage: http://www.atozedsoftware.com

Many Indy users are annoyed by the EIdConnClosedGracefully exception that is raised with Indy servers, especially the HTTP and other servers. EIdConnClosedGracefully is an exception signaling that the connection has been closed by the other side intentionally. This is not the same as a broken connection which would cause a connection reset error. If the other side has closed the connection and the socket is read or written to, EIdConnClosedGracefully will be raised by Indy. This is similar to attempting to read or write to a file that has been closed without your knowledge.

很多Indy组件的用户常常因为 Indy server 抛出的EIdConnClosedGracefully 异常而烦恼,特别是 HTTP 和其他的server。EIdConnClosedGracefully 是连接已经被另一方故意的关闭的象征。这与导致连接复位的错误所致的断开的连接不同。如果另一方已经关闭了连接,并且仍然向套接字中读写,Indy组件将 会抛出EIdConnClosedGracefully异常。这与您尝试向一个在您不知情的情况下被关闭的文件中读写数据是很相似的。

In some cases this is a true exception and your code needs to handle it. In other cases (typically servers) this is a normal part of the functioning of the protocol and Indy handles this exception for you. Even though Indy catches it, when running in the IDE the debugger will be triggered first. You can simply press F9 to continue and Indy will handle the exception, but the constant stopping during debugging can be quite annoying. In the cases where Indy catches the exception, your users will never see an exception in your program unless it is run from the IDE.

在有些情况下这是一个真的异常,你需要编程处理它。在另外一些情况下(典型的服务器),这是协议功能的正常的一部分,Indy组件会为您处理这个异常。即 使Indy组件捕捉到了这个异常,当在IDE编程环境中运行时,调试器也将会首先触发它。你只要按F9键继续运行,Indy组件将会处理这个异常,但是在 调试期间不断的停止运行是非常烦人的。在Indy组件捕捉到这个异常的情况下,除非在IDE中运行,否则您的用户将不会再看到异常提示。

Simple solution

简单的解决方法

Because the EIdConnClosedGracefully is a common exception especially with certain servers it descends from EIdSilentException. On the Language Exceptions tab of Debugger Options (Tools Menu) you can add EIdSilentException to the list of exceptions to ignore. After this is added the exceptions will still occur in the code and be handled, but the debugger will not stop the program to debug them.

因为对于从EIdSilentException继承来的服务器,EIdConnClosedGracefully是一个正常的异常。您可以在 “Tools”菜单的“Debugger Options”的“Language Exceptions”选项卡中将EIdSilentException加到忽略的异常的列表中。当加到忽略的异常的列表中后这个异常仍然会在代码中触发 并被处理,但是调试器将不会停止程序的运行。

Is it an error?

这是一个错误吗?

All exceptions are not errors. Many developers have been taught or assumed that all exceptions are errors. However this is not the case, and this is why they are called exceptions and not errors.

异常不一定是错误。很多开发者都认为所有的异常都是错误。但是事实不是这样的,这就是为什么之所以叫异常而不叫错误的原因。

Exceptions are exactly that - exceptions. Delphi and C++ Builder use exceptions to handle errors in an elegant way. However exceptions have other uses besides errors as well. EAbort is one example of an exception that is not necessarily an error. Exceptions such as these are used to modify standard program flow and communicate information to a higher calling level where they are trapped. Indy uses exceptions in such a way as well.

异常就是异常。Delphi 和 C++ Builder 使用异常处理错误。但是异常有错误之外的其他用途。EAbort是一个异常不一定是错误的例子。这样的异常用来将标准的程序流程和交流信息调整到一个更高 的能被捕捉到的层次。Indy组件也是这样使用异常的。

Why is it an exception?

它为什么是一个异常?

Many users have commented that maybe it there should be a return value to signal this condition instead of an exception. However this is the wrong approach in this case.

许多用户说它应该返回一个信号而不是一个异常。但是在这种情况下这是一个错误的方式。

The EIdConnClosedGracefully is raised from a core routine, however when this routine is called it is normally several method calls deep. The EIdConnClosedGracefully is in fact an exception and needs to be trapped by the topmost caller in most cases. The proper way to handle this is an exception.

EIdConnClosedGracefully异常从核心循环抛出,但是当这个循环被调用时,有几种在深层调用的方法。 EIdConnClosedGracefully实际上是一个异常,并且在很多情况下需要被顶层调用者捕捉。处理它的正确的方法是捕获异常。

When is it an error?

什么时候它是一个错误呢?

When EIdConnClosedGracefully is raised in a client, it is an error and you should trap and handle this exception.

当客户端抛出一个EIdConnClosedGracefully时,它就是一个错误,您就应当捕捉并且处理这个异常。

In servers it is an exception. However sometimes it is an error, and sometimes it is an exception. For many protocols this exception is part of the normal functioning of the protocol. Because of this common behavior, if you do not catch the EIdConnClosedGracefully in your server code, Indy will. It will then mark the connection as closed and stop the thread assigned to the connection. If you wish to handle this exception yourself you may, otherwise Indy will handle it and take the appropriate actions for you automatically.

在服务器中它是一个异常。但是在有些情况下这是一个错误,有些情况下是异常。对于很多协议,这个异常是协议正常功能的一部分。因为这个,如果您在你的服务 器代码中没有捕捉EIdConnClosedGracefully,Indy 组件将会捕获它。它将会将这个连接标记为关闭,并且会停止有关这个连接的所有线程。您可以以您自己的方式处理这个异常,否则的话 Indy 组件将会处理这个异常并且为您实现正确的处理。

Why does this exception occur in servers?

为什么这个异常会在服务器中触发?

When a client is connected to a server there are two common ways to handle the disconnection:

当一个客户端连接到服务器上时,有两种常用的方式处理这个断开的连接:

1. Mutual Agreement - Both sides agree to mutually disconnect by one side signaling (and the other optionally acknowledging) and then both sides disconnecting explicitly.
2. Single Disconnect - Disconnect and let the remote side take notice.

With the Mutual Agreement method both sides know when to disconnect and both explicitly disconnect. Most conversational protocols such as Mail, News, etc disconnect in this manner. When the client is ready to disconnect it sends a command to the server telling it that it will disconnect. The server replies with an acknowledgement of the disconnect request, and then both the client and server disconnect. In these cases an EIdConnClosedGracefully should not be raised, and if one occurs it is in fact an error and should be handled.

协商方式-双方都通过信号同意另一方的连接断开,并且双方都会明确的断开连接。大多数会话协议(例如Mail,News等)都是以这种方式断开连接。当客户端准备断开连接时,它将向服务器发出一个请求告诉服务器它将断开连接。服务器会返回一个同意断开连接的确认信号,然后客户端和服务器都将断开连接。在这种情况下,EIdConnClosedGracefully不应该被触发,如果被触发了,那么它就确实是一个错误,您必须处理它。

In some cases of Mutual Disconnect no command will be issued, but both sides know when the other will disconnect. Often a client will connect, issue one command, receive the response from the server and disconnect. While no explicit command was issued by the client, the protocol states that the connection should be disconnected after one command and response. Some of the time protocols are examples of this.

在有的协商方式中,不会发出这样的请求,但是双方都知道对方何时断开连接。一般的,客户端连接、发出一个请求,然后收到从服务器返回的消息。当客户端没有发出明确的请求时,协议规定:当请求和回应后连接必须被断开。一些时间协议就是这样的例子。

With Single Disconnect, one side just disconnects. The other side is left to detect this and then take appropriate action to terminate the session. With protocols that use this disconnection method you will see EIdConnClosedGracefully and it is normal. It is an exception, but Indy knows about it and will handle it for you. The whois protocol is an example of this. The client connects to the server and sends a string containing the domain to query. The server then sends the response and disconnects when the response is finished. No other signal is sent to the client other than a normal disconnection that the response is finished.

单方断开连接方式-一方自主的断开连接。另一方稍后会检测到并且会采取一定的措施来终止这个会话。使用这样连接断开策略的协议将会引发EIdConnClosedGracefully异常,这是正常的。它是一个异常,但是Indy组件将会为您处理它。whois就是一个典型的例子。客户端连接到服务器并且发送一个包含自己域名的字符串到服务器队列中。服务器会回应并且在回应完毕后断开连接。除了回应导致的正常的连接断开外,没有任何信号发送给客户端。

The HTTP allows for both Mutual Agreement and Single Disconnect and this is why it is common to see the EIdConnClosedGracefully with the HTTP server. HTTP 1.0 works similar to the whois protocol in that the server signals the client simply by disconnecting after the request has been served. The client then must use new connections for each request.

HTTP协议允许上述两种方式(协商方式、单方断开连接方式),这就是为什么在HTTP服务器中经常可以见到EIdConnClosedGracefully异常的原因。HTTP 1.0协议和whois都是在请求被处理后由服务端断开连接。客户端必须在每次请求数据时都要建立新的连接。

HTTP 1.1 allows a single connection to request multiple documents. However there is no command to signal a disconnect. At any time, either the client or the server can disconnect after a response. When the client disconnects but the server is still accepting requests, a EIdConnClosedGracefully will be raised. In most cases in HTTP 1.1, the client is the one that disconnects. A server will disconnect when it implements part of HTTP 1.1, but does not support the keep alive option.

HTTP 1.1允许使用一个连接获取多个文件。但是没有断开连接的命令。当客户端已经断开,但是服务端仍然接受请求时,EIdConnClosedGracefully异常就会被触发。对于HTTP 1.1,大部分情况下都是客户端断开连接。如果服务器声明了HTTP 1.1,那么它将会断开连接,但是不支持保持连接的选项。

More!

This article is an extract from the book Indy in Depth. Indy in Depth is an e-book which you can subscribe to and receive the complete book by e-mail. Also check out the Atozed Indy Portal at www.atozedsoftware.com

About the Author

Chad Z. Hower, a.k.a. "Kudzu " is the original author and project coordinator for Internet Direct (Indy). Indy consists of over 110 components and is included as a part of Delphi, Kylix and C++ Builder. Chad 's background includes work in the employment, security, chemical, energy, trading, telecommunications, wireless, and insurance industries. Chad 's area of specialty is TCP/IP networking and programming, inter-process communication, distributed computing, Internet protocols, and object-oriented programming. When not programming, he likes to cycle, kayak, hike, downhill ski, drive, and do just about anything outdoors. Chad, whose motto is "Programming is an art form that fights back ", also posts free articles, programs, utilities and other oddities at Kudzu World at http://www.Hower.org/Kudzu/. Chad is an American ex-patriate who currently spends his summers in St. Petersburg, Russia and his winters in Limassol, Cyprus. Chad can be reached using this form.

Chad works as a Senior Developer for Atozed Software.

关于Indy TIdTCPServer控件出现 connection closed gracefully 错误的处理相关推荐

  1. tsqlconnection连接datasnap出现connection closed gracefully错误的解决办法

    1.在服务器上的两个主连接控件中的 filter中去除RSA选项,依次设置zlibcompress和pc1 key=test 2.在客户机的sqlconnection的flter中,依次设置pc1 k ...

  2. delphi的connection closed Gracefully错误

    将这个放到调用页面上....一个处理地址函数.. function GetWebPage(URL: string):string; var IDHTTP: TIDHttp; ss: String; b ...

  3. datasnap出现connection closed gracefully错误的解决办法

    1.去掉DSTCPServerTransport1.Filters中的RSA选项. 转载于:https://www.cnblogs.com/yipin/p/8504306.html

  4. 在用Delphi 10.2.3 提示connection closed gracefully的处理

    在用Delphi 10.2.3制作一个Android app的时候用了idhttp控件,post数据能成功,但每次都会提示connection closed gracefully 错误信息,网上找了一 ...

  5. XE8-indy10中关于Connection Closed Gracefully的源码与解读

    在XE8中,使用indy10里有关TCP连接的控件时,断开连接有时候会抛出Connection Closed Gracefully的异常,下面我找到了抛出该异常的源码: procedure TIdIO ...

  6. Idhttp get baidu 错误 connection closed Gracefully

    url:=IdHTTP1.get('http://www.baidu.com'); memo1.text:=url; 就这么简短的一个代码,他既然报个connection closed Gracefu ...

  7. 关于Connection Closed Gracefully问题

    Connection Closed Gracefully 温和的关闭连接 Many Indy users are annoyed by the EIdConnClosedGracefully exce ...

  8. 解决莫名其妙出现connection closed的错误

    最近发现使用了springMVC的新项目偶尔出现connection closed,同事反映当访问同一个controller时经常出现类似错误 上回发现在使用 <context:componen ...

  9. [delphi]ssl connection closed gracefully

    之前写了一个基于ssl的API接口程序,原先用得好好的,也有段时间没有用过了,在解析的时候,出现了一个Connection Closed Gracefully的错误提示,并且程序中断了后面的代码执行, ...

最新文章

  1. sql数据类型转换(cast() and convent())函数)
  2. Java ArrayList去重
  3. matlab作业参考4,matlab第四章作业
  4. java poll()是什么方法,JAVA学习中的一点小发现:LinkedList中.poll()方法在输出时使用应注意的小点...
  5. 手把手教您如何识别翻新机子
  6. JVM发生OOM的 8 种原因、及解决办法
  7. MySQL的索引存储数据结构BTree和B+Tree的区别
  8. centos安装后两个启动项、_Windows安装Centos7双系统后Windows启动项消失
  9. 随笔-机器如何学习我们的知识?
  10. linux套接字端口,多端口监听套接字linux(multiport listening socket linux)
  11. python基于udp的网络聊天室再用tkinter显示_Python实现网络聊天室的示例代码(支持多人聊天与私聊)...
  12. crt查看oracle安装目录,ORACLE 11g数据库安装步骤
  13. 计算机网络实验视频word,(完整word版)《计算机网络与通信》实验.docx
  14. eclipse Tomcat 启动报错
  15. mmap为什么比read快
  16. numpy - 数组随机排序
  17. 计算机术语bisoin,[焦仔推荐]菜鸟起飞!让你清楚认识电脑BIOS(经典知识性文章)...
  18. jsp房屋出租管理系统带合同
  19. SBC在企业IP通信系统中的应用
  20. Visio绘制维恩图举例

热门文章

  1. MT5 EA交易期货-限价单开仓平仓
  2. 《Redis系列》Redis部署
  3. 通过信任文件绕过火绒
  4. kdb代码分析(一)
  5. SpringBoot集成百度AI实现人脸识别
  6. arcgis api 4.x加载天地图(经纬度和墨卡托投影)(一篇文章搞透天地图加载)
  7. 博途中用的是c吗_S7-1500系列博途中使用SCL语言编程方法简介
  8. [算法]DFS(深度优先搜索)C++
  9. 从来不敷面膜的人_从来不敷面膜的人会怎么样,突然敷了会怎样
  10. anaconda 创建虚拟环境