关于Indy10中IdTcpserver与IdTcpClient断开连接的问题,请高手指点(100)

Z

zhweizw

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大大,小弟在使用Indy10中遇到了问题,请不吝赐教。小弟使用IdTcpServer与IdTcpClient建立连接后,如果在Server端选在一条已连接的Client,执行DisConnect方法后,Client端不会触发OnDisConnected事件(但此时Client端的DisConnected属性是TRUE,说明连接确实已经断开,但却没有触发OnDisconnected事件)。相反,如果在Client端主动执行DisConnect方法,Server与Client端都能触发OnDisconnected事件。由于IdTcpClient继承自TIdTcpConnection类,所以小弟查看了IdTcpConnection.pas单元的源码发现:Client端的OnDisconnected事件只在自己的DisConnect方法中触发,其他位置没发现有触发此事件的代码。所以造成了Server端断开连接而Client端不触发OnDisconnected事件的现象。那么,如果在Server执行DisConnect 方法后,如何在Client端触发Ondisconnected事件呢,小弟想到2个方法:1、在Client端用timer定时检测;2、在Server端执行Disconnect方法前先通知Client。但是上面2种方法用起来总感觉不爽,起码不够“专业”1、请问各位大大,还有什么更好的方法吗?2、小弟不明白,IdTcpClient的Ondisconnected事件为什么只设计在自己的DisConnect方法中才触发呢?3、是不是小弟理解错误了?如果是,请大大指点,感谢涕零!!!
 
另外在IdTcpConnection.pas单元的519行,执行了DisconnectNotifyPeer方法,该方法在该单元的402行有说明: // This is called when a protocol sends a command to tell the other side (typically client to // server) that it is about to disconnect. The implementation should go here. procedure DisconnectNotifyPeer; virtual;大概的意思是,当协议发送一个命令告诉对方要断开连接时调用此方法(典型的应用是客户端发送给服务器端)。但是此方法的实现部分却是空的,没执行任何代码,并且在其子类中也没有被重载或覆盖,那就是说调用此方法没有任何意义了?
 
在Server端执行Disconnect方法前先通知Client,要断开的话都有client来发起,暂时没找到好办法
 
顶部