Thread View: borland.public.cppbuilder.internet.socket
3 messages
3 total messages
Started by Curt
Sun, 21 Aug 2005 23:43
Threaded Client Application
Author: Curt
Date: Sun, 21 Aug 2005 23:43
Date: Sun, 21 Aug 2005 23:43
36 lines
2156 bytes
2156 bytes
Hi,
I have a very basic client application which needs a threaded client
which will :
1- connect to a proprietary server
2- login
3- receive streaming data continually and store it in a flat file.
4- if a disconnect should occur for any reason the client should try to
reconnect
My question is, what is the best way to implement this behaviour ?
I am creating a threaded client using TIdThread and TIdTCPClient.
In Form1->OnCreate() I instantiate the TIdThread by calling TIdThread *
ClientThread = new TIdThread();
Then, within ClientThread, how should I use BeforeExecute, BeforeRun,
AfterRun, AfterExecute ? Where should I put the Connect() and Disconnect()
and where should I check to make sure that the connection has not been
broken?
Is it Connect() in BeforeExecute and Disconnect() in AfterExecute ? If so,
how do I break out of the Run() loop without terminating the thread ?
I also thought of connecting in BeforeRun and then spawning an additional
thread in Run() which would do the work of reading the Connection and then
terminate itself when a disconnect takes place returning control to the
original thread which would reestablish the connection.
Please forgive the barrage of questions.
Thanks,
Curt
Re: Threaded Client Application
Author: Remy Lebeau (TeamB)
Date: Mon, 22 Aug 2005 16:54
Date: Mon, 22 Aug 2005 16:54
58 lines
3298 bytes
3298 bytes
"Curt" <noemailaddress> wrote in message news:43091119$1@newsgroups.borland.com... > In Form1->OnCreate() Do not use the OnCreate event in C++. It is a Delphi remnant that produces illegal behavior in C++ because it can be triggered before the constructor. > I instantiate the TIdThread by calling TIdThread * ClientThread = new TIdThread(); That will not work. Like TThread, TIdThread is a base class that you need to derive a new class from in order to write the code that the thread will actually run. TIdThread by itself does not do anything. In fact, TIdThread is an abstract class, so you cannot instantiate it directly anyway. > Then, within ClientThread, how should I use BeforeExecute, BeforeRun, > AfterRun, AfterExecute ? Where should I put the Connect() and > Disconnect() Use BeforeRun() to Connect() and AfterRun() to Disconnect(). Use Run() to read a packet of data. > where should I check to make sure that the connection has not been broken? Put an try..catch block inside of Run(). If the connection is broken, reading from the client socket will thrown an exception. As long as an exception does not escape from Run(), the thread will continue running. > how do I break out of the Run() loop without terminating the thread ? The only way is to call the thread's Stop() method. There is a StopMode property that tells the thread what to do when stopped. If set to smTerminate, the thread terminates itself. If set to smSuspend, the thread suspends itself. In the latter case, you would have to Start() the thread again at a later time from another thread, including when shutting the thread down for good. On the other hand, if you don't want the thread to suspend itself, just to go back to BeforeRun() and start over, then you can override the DoStopped() method (or use the OnStopped event) to call the thread's Start() method. Then the thread will not suspend itself at all. > I also thought of connecting in BeforeRun and then spawning an > additional thread in Run() You do not need another thread. You are already in a thread. > which would do the work of reading the Connection and then terminate > itself when a disconnect takes place returning control to the original thread > which would reestablish the connection. You do not need to do that. Gambit
Re: Threaded Client Application
Author: Curt
Date: Wed, 24 Aug 2005 02:28
Date: Wed, 24 Aug 2005 02:28
78 lines
3676 bytes
3676 bytes
Thank you very much Gambit. Your comments are right on. I programmed my app according to your directions and it works well! "Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:430a0326$1@newsgroups.borland.com... > > "Curt" <noemailaddress> wrote in message > news:43091119$1@newsgroups.borland.com... > > > In Form1->OnCreate() > > Do not use the OnCreate event in C++. It is a Delphi remnant that produces > illegal behavior in C++ because it can be triggered before the constructor. > > > I instantiate the TIdThread by calling TIdThread * ClientThread = new > TIdThread(); > > That will not work. Like TThread, TIdThread is a base class that you need > to derive a new class from in order to write the code that the thread will > actually run. TIdThread by itself does not do anything. In fact, TIdThread > is an abstract class, so you cannot instantiate it directly anyway. > > > Then, within ClientThread, how should I use BeforeExecute, BeforeRun, > > AfterRun, AfterExecute ? Where should I put the Connect() and > > Disconnect() > > Use BeforeRun() to Connect() and AfterRun() to Disconnect(). Use Run() to > read a packet of data. > > > where should I check to make sure that the connection has not been broken? > > Put an try..catch block inside of Run(). If the connection is broken, > reading from the client socket will thrown an exception. As long as an > exception does not escape from Run(), the thread will continue running. > > > how do I break out of the Run() loop without terminating the thread ? > > The only way is to call the thread's Stop() method. There is a StopMode > property that tells the thread what to do when stopped. If set to > smTerminate, the thread terminates itself. If set to smSuspend, the thread > suspends itself. > > In the latter case, you would have to Start() the thread again at a later > time from another thread, including when shutting the thread down for good. > On the other hand, if you don't want the thread to suspend itself, just to > go back to BeforeRun() and start over, then you can override the DoStopped() > method (or use the OnStopped event) to call the thread's Start() method. > Then the thread will not suspend itself at all. > > > I also thought of connecting in BeforeRun and then spawning an > > additional thread in Run() > > You do not need another thread. You are already in a thread. > > > which would do the work of reading the Connection and then terminate > > itself when a disconnect takes place returning control to the original > thread > > which would reestablish the connection. > > You do not need to do that. > > > Gambit > >
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads