Jump to content
ChrisChuah

Indy TIdTCPClient connect, send and disconnect

Recommended Posts

Hi

Is there a way to use Indy TCP Client to have a synchronous method whereby 

IdTCPClient1.Connect;

IdTCPClient1.Send();

IdTCPClient1.Disconnect

 

rather than to wait for OnConnect event to send the message and followed by Disconnect as I would need to put the buffer on queue so that it will be written to the socket onConnect event

The reason for doing this is that i need to send multiple message to different TCP servers and they will accept one command and need to disconnect after sending.

What would be the best way to implement this?

 

please advise

regards

chris

 

Share this post


Link to post
17 hours ago, ChrisChuah said:

Is there a way to use Indy TCP Client to have a synchronous method whereby 

IdTCPClient1.Connect;

IdTCPClient1.Send();

IdTCPClient1.Disconnect

That is literally exactly how Indy is designed to be used.  Indy uses blocking sockets and synchronous I/O.  Connect() will block the calling thread and not exit until the connection is established.  Sends will block the calling thread and not exit until the data has been passed to the kernel.  Etc.  So, just do exactly what you said above, it will work fine.  Just be sure to put the send in a try..finally to ensure the connection is closed even if the send raises an error, eg:

IdTCPClient1.Connect;
try
  // send whatever you need...
finally
  IdTCPClient1.Disconnect;
end;
Quote

rather than to wait for OnConnect event to send the message

Most Indy clients are not event-driven.  You do not need to wait for the OnConnect event.  It is just a status event, not a logic-driving event.

Edited by Remy Lebeau
  • Like 1

Share this post


Link to post

Hi Remy

 

Now i m having another problem

In my app, i have 2 idTCPClient sockets that connect to 2 different ports of same server application.

When one of the port or idTCPClient (Client A) disconnect, it should disconnect the other idTCPClient (Client B) as there may be some issues with the server app.

When reconnecting the idTCPClient B to the server, whenever i try to use ClientB.IOHandler.Read, it will give me an exception like this

-----

<07/11/2023 10:02:31> IP to Connect to CM: 172.20.11.9:16194
<07/11/2023 10:02:31> Connecting..CM User TCP: 172.20.11.9:16197 Own IP: 172.20.13.201
<07/11/2023 10:02:36> Error in Thread Run. Type: EIdConnClosedGracefully Msg: Connection Closed Gracefully.
-----

When disconnecting idTCPClient B, should i release the IOHandler? 

If not how can i handle this exception as I am using a IdThreadComponent to read the IdTCPClient B socket for messages

 

regards

chris

 

Share this post


Link to post
14 hours ago, ChrisChuah said:

In my app, i have 2 idTCPClient sockets that connect to 2 different ports of same server application.

Why?  Can't you design your protocol to send your 2 sets of data over a single connection?

14 hours ago, ChrisChuah said:

When one of the port or idTCPClient (Client A) disconnect, it should disconnect the other idTCPClient (Client B) as there may be some issues with the server app.

When reconnecting the idTCPClient B to the server, whenever i try to use ClientB.IOHandler.Read, it will give me an exception

That means the server is closing the connection on its end.  Can you show your actual code that is managing your connections?  Do you really need 2 separate connections to the same server, though?

14 hours ago, ChrisChuah said:

When disconnecting idTCPClient B, should i release the IOHandler? 

Not release specifically.  However, if you are planning on re-connecting the same TIdTCPClient again, you should at least make sure its IOHandler.InputBuffer is cleared of any unread data after disconnecting, so Indy won't think the connection is still alive, eg:

idTCPClientB.Disconnect;
if idTCPClientB.IOHandler <> nil then
  idTCPClientB.IOHandler.InputBuffer.Clear;

 

Edited by Remy Lebeau

Share this post


Link to post

Hi Remy

The data sent to 2 ports are different. The server was designed in such a way that Port1 is sending Command and Control data while Port2 is used for sending and receiving Configuration data.

That was how the server was implemented long time ago.

Since both ports will be getting data from the server at any 1 time so i cant just use 1 client to send and receive from 2 different ports at the same time isnt it?

Am i able to use 1 idTCPClient port to connect to 2 ports? once connected, both ports can receive data at the same time?

please advise

 

regards

chris

 

Share this post


Link to post
8 hours ago, ChrisChuah said:

The data sent to 2 ports are different. The server was designed in such a way that Port1 is sending Command and Control data while Port2 is used for sending and receiving Configuration data.

That was how the server was implemented long time ago.

I probably would have used a single port and delimited the 2 datas, but that's just me.  Whatever works best for you.

8 hours ago, ChrisChuah said:

i cant just use 1 client to send and receive from 2 different ports at the same time isnt it?

Am i able to use 1 idTCPClient port to connect to 2 ports?

No, you need a separate client for each server port.  But you can run the 2 clients in parallel, such as with threads.

Share this post


Link to post
On 11/8/2023 at 9:12 PM, Remy Lebeau said:

I probably would have used a single port and delimited the 2 datas

Quite a non-trivial task if there's an intensive data stream over one channel while control channel must remain responsible.

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
×