Article View: borland.public.cppbuilder.internet.socket
Article #5071TServerSocket: GetThread() & OnExecute()
From: Vladimir Stefanovic
Date: Fri, 05 Nov 2004 12:05
Date: Fri, 05 Nov 2004 12:05
96 lines
3459 bytes
3459 bytes
Hi,
Why for some requests I send to server my 'HandlingRequests()'
is executed twice, if I *DO NOT* close the connection in server
itself, like I did in this example: ClientSocket->Close()?
That means, if I omitted 'ClientSocket->Close()' and let the
client app to close the connection in OnRead() after receiving
response, somehow my HandlingRequests() is executed twice.
I do not mean that ClientExecute() is executed twice, I suspect
that...
while ( ( !Terminated ) && ( ClientSocket->Connected ) )
...loop continued before I managed to close the connection
on client side, and so forced HandlingRequests() to do the same
task that once was completed.
I think that is the case because HandlingRequests() f-on truncates
the COMMAND part of InboxMemortyStream:
GET DataToProcess -> becomes -> DataToProcess
... and in the second pass
DataToProcess -> becomes -> ToProcess
( command part, 3 bytes + blanko, is missing, and in my LOG I got
unknown commad: Dat )
What can I do? The code below works, but I think that's normal
for client to close the connection - not server.
Maybe I should try with some flags, to avoid the second pass?
What do you suggest?
--- code ---
void __fastcall TAccountsServerThread::ClientExecute()
{
TMemoryStream *InboxMemoryStream = new TMemoryStream;
InboxMemoryStream->Position = 0;
int NewSize, OldSize = 0;
TWinSocketStream *Stream = new TWinSocketStream ( ClientSocket, 5000 );
try
{
while ( ( !Terminated ) && ( ClientSocket->Connected ) )
{
if ( Stream->WaitForData ( 5000 ) )
{
int BufferSize = 0;
// Read file length ...
if( !ReadData ( Stream, &BufferSize, sizeof(int) ) )
break;
char *SomeBuffer = new char[ BufferSize ];
try
{
// Read the file...
if( !ReadData ( Stream, SomeBuffer, BufferSize ) )
break;
OldSize = InboxMemoryStream->Size;
NewSize = OldSize + BufferSize;
InboxMemoryStream->Size = NewSize;
InboxMemoryStream->WriteBuffer( SomeBuffer, BufferSize );
InboxMemoryStream->Position=0;
HandlingRequests( Stream, InboxMemoryStream );
ClientSocket->Close();
}
__finally
{
delete [] SomeBuffer;
}
}
}
}
__finally
{
delete Stream;
delete InboxMemoryStream;
}
}
Best regards,
Vladimir Stefanovic
Technical Details
Message-ID:
<418b7aa7$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 11508e,3fbffcd0acc17958,start X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Vladimir Stefanovic" <a@a.com> Sender: "Vladimir Stefanovic" <a@a.com> Reply-To: "Vladimir Stefanovic" <a@a.com> Subject: TServerSocket: GetThread() & OnExecute() Newsgroups: borland.public.cppbuilder.internet.socket X-User-Info: 213.137.116.166 X-Forwarded-For: 128.128.129.165 NNTP-Posting-Host: 207.105.83.66 Message-ID: <418b7aa7$1@newsgroups.borland.com> Date: 5 Nov 2004 05:05:43 -0700 X-Trace: newsgroups.borland.com 1099659943 207.105.83.66 (5 Nov 2004 05:05:43 -0700) Lines: 97 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail