Randy
void __fastcall TForm1::ServerSocket1GetThread(TObject* Sender,
TServerClientWinSocket* ClientSocket, TServerClientThread* &SocketThread)
{
SocketThread = new TMyThread(ClientSocket);
}
class TMyThread : public TServerClientThread
{
protected:
void __fastcall ClientExecute();
public:
__fastcall TMyThread(TServerClientWinSocket* ASocket);
};
__fastcall TMyThread::TMyThread(TServerClientWinSocket* ASocket)
: TServerClientThread(true, ASocket)
{
// do any initializations that you need
Resume();
}
void __fastcall TMyThread::ClientExecute()
{
TWinSocketStream *strm = new TWinSocketStream(ClientSocket, 5000);
while(!Terminated && (ClientSocket && ClientSocket->Connected))
{
if(!strm->WaitForData(strm->TimeOut))
continue;
if(strm->Read(some_buffer, some_size) != some_size)
break;
// process some_buffer as needed
}
delete strm;
}
Gambit
"Randy Morrow" <rmo...@protus.com> wrote in message
news:3cd7f084$1_1@dnews...
Randy
"Remy Lebeau [TeamB]" <gamb...@yahoo.com> wrote in message
news:3cd7fca2$1_2@dnews...
Thanks - Michael
michael wrote:
>
> But how can you detect, that the client has disconnected? OnClientDisconnect does not work anymore.
> Have you an idea to solve the problem?
>
> >> while(!Terminated && (ClientSocket && ClientSocket->Connected))
Guess what? Right, ClientSocket->Connected returns false if the client
is disconnected.
Frank
Michael wrote:
>
> Thank you Frank, I will change the the "if condition" according to your suggestion.
> Michael
>
> Frank Birbacher <bloodym...@gmx.net> wrote:
> >Hi!
> >
> >michael wrote:
> >>
> >> But how can you detect, that the client has disconnected? OnClientDisconnect does not work anymore.
> >> Have you an idea to solve the problem?
> >>
> >> >> while(!Terminated && (ClientSocket && ClientSocket->Connected))
Where is the need for a change?
Frank
while(!Terminated && (ClientSocket && ClientSocket->Connected))
{
if(!strm->WaitForData(strm->TimeOut))
continue;
if(strm->Read(some_buffer, some_size) != some_size)
break;
// process some_buffer as needed
}
ClientSocket->Connected is true while the client is conected. When
Connected becomes false, the client has disconnected and the thread ends.
In the event that Connected is not updated correctly upon disconnection (a
known issue), then WaitForData() happens to return true even though there's
no awaiting data, and Read() fails, thus breaking the loop so the thread can
still end correctly.
Gambit
"michael" <gr...@dhzb.de> wrote in message news:3ce65580$1_1@dnews...
Well, my if-condition was:
while(!Terminated && ClientSocket->Connected). When I change the code there is still no difference:
When the client disconnects and I set a breakpoint to the "if condition line" then ClientSocket->Connected is still "true"
and the thread is still active. I can't figure out where the problem is.
Thank you for you help, anyways!
Michael
while(!Terminated && (ClientSocket && ClientSocket->Connected)){
try {
TWinSocketStream *pStream = new TWinSocketStream(ClientSocket, CLIENTWAITTIME);
try {
if(pStream->Read(buffer, sizeof(buffer)) > 0){
if (Form1->Debug){
Form1->Memo1->Lines->Add(AnsiString("(Client) ") + AnsiString(buffer));
Form1->Memo1->Lines->Add("Socket-Nr.: " + IntToStr(Sckt));
}// if Debug
Form1->befehl_parse(Sckt, buffer);
}// if pStream
}//try
__finally
{
delete pStream;
}
}//try
catch(...)
{
HandleException();
}
}// while !Terminated
Where is the problem? Any suggestions?
Michael
michael wrote:
> Well, my if-condition was:
> while(!Terminated && ClientSocket->Connected).
Well, it's a while condition, isn't it?
> When I change the code there is still no difference:
> When the client disconnects and I set a breakpoint to the "if condition line" then > ClientSocket->Connected is still "true"
> and the thread is still active. I can't figure out where the problem is.
I got that, too. Couldn't figure it out either. Posted here, too. Got no
answer to solve the problem. I stick with reading until an exception
arises. Wrap your code into try{...}catch(Exception&){break;}
Frank
Gambit
"Frank Birbacher" <bloodym...@gmx.net> wrote in message
news:3CE6DE86...@gmx.net...
1) You shouldn't be allocating a new TWinSocketStream in each iteration of
the loop, you should instead be allocating a single instance that the thread
re-uses each time.
2) You're not calling WaitForData() at all.
3) You're not breaking the loop if Read() fails
Btw, it's very dangerous to access components in the main VCL thread like
you are. You must synchronize the access or else you'll run the risk of
severe problems if the main VCL thread tries to access the same components
at the very same time.
Try this instead:
class TMyThread : public TServerClientThread
{
private:
char buffer[1024]; // whatever the real buffer is
void __fastcall Process();
...
};
void __fastcall TMyThread::ClientExecute()
{
TWinSocketStream *pStream = NULL;
try
{
pStream = new TWinSocketStream(ClientSocket, CLIENTWAITTIME);
while(!Terminated && (ClientSocket && ClientSocket->Connected))
{
try
{
if(!pStream->WaitForData(CLIENTWAITTIME))
continue;
if(pStream->Read(buffer, sizeof(buffer)) < 1)
break;
// always sync access to the main VCL thread !!!!!
Synchronize(Process);
}
catch(...)
{
HandleException();
}
}
}
__finally
{
if(pStream)
delete pStream;
}
}
void __fastcall TMyThread::Process()
{
if (Form1->Debug)
{
Form1->Memo1->Lines->Add("(Client) " + AnsiString(buffer));
Form1->Memo1->Lines->Add("Socket-Nr.: " + IntToStr(Sckt));
}
Form1->befehl_parse(Sckt, buffer);
}
Gambit
"Michael" <gr...@dhzb.de> wrote in message news:3ce6d49b$1_1@dnews...
> Apparanty, that does not work in my application. I add the code I
> am using:
<snip>
"Remy Lebeau [TeamB]" <gamb...@yahoo.com> wrote:
Michael wrote:
> see the answer from Remy Lebeau! It solved my problems and the thread stops! Great!
Yeah, I changed my code accordingly, too, and the problem is gone!
THANKS REMY!!
Frank