Article View: borland.public.cppbuilder.internet.socket
Article #4151Re: threadblocking server connection handling
From: Remy Lebeau (TeamB)
Date: Thu, 24 Mar 2005 19:17
Date: Thu, 24 Mar 2005 19:17
108 lines
3745 bytes
3745 bytes
"havl" <havl83@wp.pl> wrote in message
news:4242da71@newsgroups.borland.com...
> by length i ment the size of data received by read
> operation ...which was caused by waitfordata returning true
Which is exactly the condition you are supposed to be testing for in the
first place. According to the WinSock API documentation:
"If the socket is connection oriented and the remote side has shut down
the connection gracefully, a recv will complete immediately with zero bytes
received."
> a single clientthread goes like this
Use this code instead:
bool __fastcall TMyThread::ReadBuffer(TWinSocketStream *Strm, void
*Buffer, int Count)
{
BYTE *pBuf = (BYTE*) Buffer;
int read;
try
{
while( Count > 0 )
{
read = Strm->Read(pBuf, Count);
if( u < 0 )
{
// socket error
return false;
}
if( u == 0 )
{
// socket disconnected
return false;
}
pBuf += read;
Count -= read;
}
return true;
}
catch(const Exception &)
{
// socket error
return false;
}
}
bool __fastcall TMyThread::WriteBuffer(TWinSocketStream *Strm, void
*Buffer, int Count)
{
BYTE *pBuf = (BYTE*) Buffer;
int written;
try
{
while( Count > 0 )
{
written = Strm->Write(pBuf, Count);
if( u < 0 )
{
// socket error
return false;
}
if( u == 0 )
{
// socket disconnected
return false;
}
pBuf += read;
Count -= read;
}
return true;
}
catch(const Exception &)
{
// socket error
return false;
}
}
void __fastcall TMyThread::ClientExecute()
{
TWinSocketStream* str = new TWinSocketStream(ClientSocket, timeout);
while( (!Terminated) && (ClientSocket->Connected) )
{
if( str->WaitForData(5) )
{
if( !ReadBuffer(str, rbuf, hsize);
break;
// process rbuf as needed...
}
if( w )
{
if( !WriteBuffer(wbuf, sw) )
break;
// clear buffer
}
}
delete strm;
}
Gambit
Technical Details
Message-ID:
<42431221$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4242c0bb@newsgroups.borland.com> <4242c5a1$1@newsgroups.borland.com> <4242da71@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,541706e3e4df9e6e 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: <4242c0bb@newsgroups.borland.com> <4242c5a1$1@newsgroups.borland.com> <4242da71@newsgroups.borland.com> Subject: Re: threadblocking server connection handling Date: Thu, 24 Mar 2005 11:17:58 -0800 Lines: 110 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: <42431221$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1111691809 68.69.17.41 (24 Mar 2005 11:16:49 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail