tags:

views:

98

answers:

2

Hi ,

When my Tcpclient is working , with this code :

TCPClient.Disconnect;
TCPClient.Connect;

I get "raised exception class EIdAlreadyConnected with message 'Already connected.'." error still (whereas , it has been disconnected before) .

So , how can i disconnect it totally ?

Thank you

+1  A: 

You say it is disconnected, but you only gave the command to disconnect.

Network traffic takes time, and probably you reconnected before you were really disconnected.

Probably you need to monitor some connection state or event to wait till you really are disconnected.

... or try to process the exception and ignore it, using try..except

Marco van de Voort
Thank you . Please show me an example
Kermia
Indy does *not* use asynchronous communication, so intuition would be that when the command returns it has already done its job.
Smasher
The problem is the definition of the job. The sending of the disconnect, or it being confirmed/timeout so that it is fully shutdown.
Marco van de Voort
+3  A: 

using at indy 10 you must sure inputbuffer is empty.

if idTcpClient.connected then
begin
 idTcpClient.IOHandler.InputBuffer.clear;
 idTcpClient.Disconnect;
end;
sabri.arslan
Thank you , I'll try it
Kermia
To elaborate, the Connected() method considers a connection to still be open, even if the physical socket has been closed, if the InputBuffer still has pending unread data in it that can satisfy read requests without going back to the socket. This is by design. Typically, you would disconnect the socket only after you have read all of the data that the connection has to offer. If you are disconnecting prematurely, then you have to clear any already-received-but-unread data manually by clearing the InputBuffer.
Remy Lebeau - TeamB
Thank you Mr Lebeau
Kermia