Article View: borland.public.cppbuilder.internet.socket
Article #2898Re: TClientSocket won't disconnect safely from TServerSocket
From: Remy Lebeau (TeamB)
Date: Thu, 05 Jan 2006 18:15
Date: Thu, 05 Jan 2006 18:15
67 lines
3314 bytes
3314 bytes
"Maurice Anderson" <mauriceanderson@hotmail.com> wrote in message
news:43bcd080$1@newsgroups.borland.com...
> However, if I do a ClientSocket1->Close() on the client, my
> server hangs. When I check task manager, I can see that my
> server has pumped up CPU time way high.
Your thread code is failing to break out of its loop when a disconnect
happens. The TServerClientWinSocket::Connected property can (and usually
does) remain true in a blocking server, so your loop keeps running long
after the disconnect occurs. Also, the loop is running faster after the
disconnect because the TWinSocketStream::WaitForData() method is returning
immediately each time without any delay.
> Here is my code in my TServerClientThread thread's Execute function:
When a graceful disconnect occurs, WaitForData() will return true and then
reading from the TWinSocketStream will return 0. That is the disconnect
notifiction that you need to check for in your code. Then code you showed
is not performing any reading.
Try this instead:
void __fastcall TMyServerThread::ClientExecute(void)
{
try
{
pStream = new TWinSocketStream(ClientSocket, 6000);
try
{
while( (!Terminated) && (ClientSocket->Connected) )
{
if( pStream->WaitForData(500) )
{
// read something ...
if( BytesRead <= 1 )
{
if( BytesRead == 0 )
// disconnected ...
else
// error ...
break;
}
}
}
}
__finally
{
delete pStream;
}
}
catch(const Exception &)
{
HandleException();
}
}
> SocketThread->FreeOnTerminate = true;
Do not use FreeOnTerminate with TServerClientThread objects. TServerSocket
needs to maintain ownership of the thread memory at all times. And in fact,
will automatically reset FreeOnTerminate to false anyway.
Gambit
Technical Details
Message-ID:
<43bd61ba$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news4.google.com!news.glorb.com!newsgate.cistron.nl!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!feeder.xsnews.nl!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<43bcd080$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,3093e14c422eb03f X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <43bcd080$1@newsgroups.borland.com> Subject: Re: TClientSocket won't disconnect safely from TServerSocket Date: Thu, 5 Jan 2006 10:15:25 -0800 Lines: 69 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <43bd61ba$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1136484794 68.69.17.41 (5 Jan 2006 10:13:14 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news4.google.com!news.glorb.com!newsgate.cistron.nl!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!feeder.xsnews.nl!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail