Jump to content
Alberto Fornés

Close a TIdTCPClient connection

Recommended Posts

         Hello, I have created a thread where there is a TIdTCPClient component that receives readings from a barcode reader connected through an ethernet base. The goal is for you to be in a connected service all day without stopping. The thread works, I can start and stop it several times, when I stop the thread I execute these steps to close the connection (FClient = TIdTCPClient):
 

FClient.IOHandler.InputBuffer.Clear;
FClient.IOHandler.CloseGracefully;
FClient.Disconnect;
FClient.Socket.Close;

 

       I don't know if they are all necessary, but I have tried to get the desired result. To test, I need to achieve a reconnection when a network failure is detected (for this I disconnect the cable from the pc), when I detect an error in the reading:


 

try
 FData: = FClient.IOHandler.ReadLn;
except on E: Exception do begin
 FClient.IOHandler.InputBuffer.Clear;
 FClient.IOHandler.CloseGracefully;
 FClient.Disconnect;
 FClient.Socket.Close;
 // Terminate thread
end;

 

In this second case, when I try to connect again, I get this message from the base: This channel is already in use, no slot is available. It seems that the old host-port socket remains active: how can I ensure that it is closed?

Share this post


Link to post
3 hours ago, Alberto Fornés said:

I don't know if they are all necessary

No, they are not.  All you really need is Disconnect(), or at least Socket.Close() if closing the socket from another thread.

3 hours ago, Alberto Fornés said:

In this second case, when I try to connect again, I get this message from the base: This channel is already in use, no slot is available.

That is not an Indy error message, or any OS error message I have ever seen.  So I am assuming you mean it is coming from the Barcoder Reader or Ethernet base (what is that supposed to be?) instead.  Does the reader/base only allow 1 connection at a time?  If you just yank out the network cable, it won't matter what you do on the client side, the connection will be lost on the server side and it will need time to process that loss, which may take awhile, depending on the server's implementation.

3 hours ago, Alberto Fornés said:

how can I ensure that it is closed?

Disconnect()/Close() will perform a graceful disconnect (send a FIN packet to notify the peer that the connection is being closed intentionally).  That should allow the reader/base to free up the used slot immediately.

 

However, it won't matter what you do in the case of the connection being lost abnormally, like from a network outage.  All you can do in that case is wait for the reader/base to handle the loss on its end in its own time to make the slot available again.

Share this post


Link to post
12 hours ago, Remy Lebeau said:

No, they are not.  All you really need is Disconnect(), or at least Socket.Close() if closing the socket from another thread.

That is not an Indy error message, or any OS error message I have ever seen.  So I am assuming you mean it is coming from the Barcoder Reader or Ethernet base (what is that supposed to be?) instead.  Does the reader/base only allow 1 connection at a time?  If you just yank out the network cable, it won't matter what you do on the client side, the connection will be lost on the server side and it will need time to process that loss, which may take awhile, depending on the server's implementation.

Disconnect()/Close() will perform a graceful disconnect (send a FIN packet to notify the peer that the connection is being closed intentionally).  That should allow the reader/base to free up the used slot immediately.

 

However, it won't matter what you do in the case of the connection being lost abnormally, like from a network outage.  All you can do in that case is wait for the reader/base to handle the loss on its end in its own time to make the slot available again.

 Thanks Remy, your words are greatly appreciated on this topic. That's right, the host only accepts one connection, and the message is sent by it. I will find a way to close the connection (maybe restart the device) so that I can connect again.

Edited by Alberto Fornés

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×