Delphi 中的 TCPclient.connected 问题 - Indy
我在使用 Delphi 中 Indy 的 IdTCPclient.connected 函数时遇到问题。我使用的是Indy10和Delphi2010环境。我的问题是每次我使用 IdTCPclient.connected 检查 TCP 连接时,都会引发这些错误的异常 EidSocketError ,EidReadTimeOut。有什么办法可以断开连接并重新连接吗? (如重置连接)。
注意:我设置了 TCPClient.ReTimeout:= 30000;
重置连接的实现编码如下。
if IdTCPclient.connected then
begin
IdTCPclient.IOHandler.InputBuffer.Clear;
IdTCPclient.Disconnect;
end;
sleep(1000);
try
IdTCPclient.connect;
except
on E: Exception do
MessageDlg('Connecting Error: '+E.Message, mtError, [mbOk], 0);
end;
但在某些时候,我遇到了异常,它根本无法连接。我不确定我做错了什么。
我应该这样做吗?
- 首先断开连接
- 清除输入缓冲区
- 销毁 TCPclient
- 重新创建新的 TCPclient
- 然后再次连接
如果是这种情况,有人可以为我提供一种正确执行此操作的方法吗?
另外,我的代码中还有另一个重新连接 TCP 的函数。这也给了我例外。我在向 TCP 发送消息之前检查连接。如果没有连接,我会重新连接五次。
result := IdTCPclient.connected
if not result then
begin
for k:=0 to 4 do
beign
sleep(1000);
try
TCPclient.connect;
except
on E: Exception do
MessageDlg('Connecting Error: '+E.Message, mtError, [mbOk], 0);
end
result := TCPclient.connected;
if result then break;
end;
通过以上两个编码,程序可以很好地处理重新连接和重置连接。但有时程序根本无法重新连接或重置连接。
- 当我遇到异常时我该怎么办?我应该从异常中重新连接吗?
- 我们如何编写代码来定期检查连接?
- 我们如何构建编码以在连接丢失时恢复连接?
亲切的问候,
I am having problem with IdTCPclient.connected function from Indy in Delphi. I am using Indy10 and Delphi2010 environment. My problem is every time i check the TCP connection with IdTCPclient.connected, it raises exception with these errors EidSocketError, EidReadTimeOut. Is there any way to disconnect and reconnect the connection? (like reset the connection).
Note: I set TCPClient.ReTimeout:= 30000;
The implemented coding for reset the connection is follow.
if IdTCPclient.connected then
begin
IdTCPclient.IOHandler.InputBuffer.Clear;
IdTCPclient.Disconnect;
end;
sleep(1000);
try
IdTCPclient.connect;
except
on E: Exception do
MessageDlg('Connecting Error: '+E.Message, mtError, [mbOk], 0);
end;
But some point, i get exception and it couldn't connect at all. I am not sure what i am doing wrong.
Should i do this?
- Disconnect first
- Clear input buffer
- Destroy TCPclient
- Re-create new TCPclient
- And then connect it again
If it is the case, can someone provide me a way how to do it properly?
Also, there is another function to re-connecting the TCP in my coding. It also gives me exception as well. I check the connecting before i send a message to TCP. If there is no connection, i reconnect for five times.
result := IdTCPclient.connected
if not result then
begin
for k:=0 to 4 do
beign
sleep(1000);
try
TCPclient.connect;
except
on E: Exception do
MessageDlg('Connecting Error: '+E.Message, mtError, [mbOk], 0);
end
result := TCPclient.connected;
if result then break;
end;
With above two coding, program handles reconnecting and reset the connection pretty well. But some point the program cannot re-connect or reset the connection at all.
- What should i do when i get exception? Should i reconnect from exception?
- How do we build coding to check the connection regularly?
- How do we build coding to to get back the connection when it lost?
Kind regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Connected()
根本不应该引发任何异常。如果是,那么它很可能是一个错误。请提供显示该情况的堆栈跟踪。最好的选择是尽可能避免使用
Connected()
。当您需要执行 I/O 操作时,就这样做,并在发生故障时让 Indy 引发异常。然后你就可以在那一刻处理它,例如:Connected()
should not be raising any exceptions at all. If it is, then it is likely a bug. Please provide a stack trace showing that.The best option is to simply avoid using
Connected()
whenever possible. When you need to perform an I/O operation, just do so, and let Indy raise an exception if a failure occurs. You can then handle it at that moment, eg:连接之前请确保 idftp 的被动布尔值为 false
当您需要文件传输时,使用二进制文件选项将其更改为 true
before you connect make sure that the passive Boolean of idftp is false
when you need file transfert change it to true with binary file option