Forum > Ported from Delphi/Kylix

Indy Client in a Thread

(1/3) > >>

JimKueneman:
I am running an Indy TCP client in a thread.  I am getting a random shut down of the socket during data transfer.  I am not getting any exceptions when it does it.  The thread shuts down because I am using this logic.. is this correct?  Should I be using Connected for the test that something is wrong or could the connection be shut down and restarted in the background and the next time I tried to send/receive it will be automatically reconnected?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---while not IsTerminated and idTCPClient.Connected do   begin       ....    end;                                                     

Remy Lebeau:

--- Quote from: JimKueneman on October 26, 2023, 04:16:43 pm ---I am running an Indy TCP client in a thread.  I am getting a random shut down of the socket during data transfer.  I am not getting any exceptions when it does it.  The thread shuts down because I am using this logic.. is this correct?

--- End quote ---

Makes sense, if the remote server closes its end of the connection, and your client runs out of data to read from its InputBuffer.  In which case, Connected() would return False, breaking your loop.


--- Quote from: JimKueneman on October 26, 2023, 04:16:43 pm ---Should I be using Connected for the test that something is wrong

--- End quote ---

Not really, that is not its purpose.  You should just read from the connection and let it block until data arrives or an error is raised.  Just be aware that if the server does close the connection intentionally, you would get an EIdConnClosedGracefully exception raised, and then you would have to handle that, even though it is not a real error.

So, what is the actual intent here?  Do you want your thread to terminate when the connection is closed, or do you want it to keep running and possibly reconnect?


--- Quote from: JimKueneman on October 26, 2023, 04:16:43 pm ---could the connection be shut down and restarted in the background and the next time I tried to send/receive it will be automatically reconnected?

--- End quote ---

No, you have to explicitly write code to close and reconnect the client.

JimKueneman:
Thanks Remy

JimKueneman:
New strangeness started now that I spent all afternoon trying to understand... this was a all working fine....

TIdTcpServer TidTcpClient

I send a message to the Server from the Client (Text form of a GridConnect message);
The Server receives it and sends it reply (I am using IOHandler.ReadStream/WriteStream if that matters).

I can see these message in WireShark.

The Client on the other hand never sees the message...

I am doing this in the method


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- property idTCPClient: TIdTCPClient read FidTCPClient write FidTCPClient;        .... procedure TLccEthernetClientThread.OnThreadComponentRun(Sender: TIdThreadComponent);var  iData: Integer;  LocalDataArray: TLccDynamicByteArray;  GridConnectStrPtr: PGridConnectString;  MessageStr: String;begin       if not IdTCPClient.IOHandler.InputBufferIsEmpty then  begin    ReceiveStreamConnectionThread.Position := 0;    ReceiveStreamConnectionThread.Size := 0;    // Would this set Postion too?    idTCPClient.IOHandler.ReadStream(ReceiveStreamConnectionThread, idTCPClient.IOHandler.InputBuffer.Size);     ..... bunch of useful stuff  end;      // https://stackoverflow.com/questions/64593756/delphi-rio-indy-tcpserver-high-cpu-usage    // There is another way to do this but with this simple program this is fine  IndySleep(THREAD_SLEEP_TIME);  end;         
The method is continuously called so it is still running and the connection has not been dropped at either end as I can repeat the process and send a message from the Client to the Server and the Server respond but


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if not IdTCPClient.IOHandler.InputBufferIsEmpty then 
  Always returns false.  I am using the same architecture to watch for incoming messages on the Server side as well.   This was working until today.  I am stumped....

Jim

JimKueneman:
This seems to have solved it for now...


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- procedure TLccEthernetClientThread.OnThreadComponentRun(Sender: TIdThreadComponent);var  iData: Integer;  LocalDataArray: TLccDynamicByteArray;  GridConnectStrPtr: PGridConnectString;  MessageStr: String;begin       IdTCPClient.IOHandler.CheckForDataOnSource(1);      <<< Added  if not IdTCPClient.IOHandler.InputBufferIsEmpty then  begin    ReceiveStreamConnectionThread.Position := 0;    ReceiveStreamConnectionThread.Size := 0;    // Would this set Postion too?    idTCPClient.IOHandler.ReadStream(ReceiveStreamConnectionThread, idTCPClient.IOHandler.InputBuffer.Size);     ..... bunch of useful stuff  end;     // https://stackoverflow.com/questions/64593756/delphi-rio-indy-tcpserver-high-cpu-usage    // There is another way to do this but with this simple program this is fine  IndySleep(THREAD_SLEEP_TIME);  end;    

Navigation

[0] Message Index

[#] Next page

Go to full version