Article View: borland.public.cppbuilder.internet.socket
Article #3287Indy TCP C/S: Timeout , Concurrent access, Number of threads
From: Vladimir Stefanovic
Date: Thu, 06 Oct 2005 11:00
Date: Thu, 06 Oct 2005 11:00
172 lines
5416 bytes
5416 bytes
Hi,
I have few questions about functioning of TIdTCPClient &
TIdTCPServer components (the code sample is below).
1) How can I set the time in which:
IdTCPClient->Connect();
... will be waited to connect. Now, at my PC it's about
15 seconds, and I want to change that. If the separate
thread is the only solution, can you please show me a
piece of code.
Note:
I still want to be blocked (like ctBlocking at
TClientSocket), but I want to reduce the time for
waiting for a connection to be established.
2) Is it OK that client is responsible for both opening &
closing the connection?
C: Opens connection
C: Sends request
S: Receives request
S: Sends response
C: Receives response
C: Closes connection
3) Is the property TIdTCPServer::TerminateWaitTime, the
time when server will anyway close the thread by itself
(connection with client) which was not closed within the
agreed time period?
4) Is the code below acceptable for concurrent client's
access (except that I didn't protected Memo at server
in this example)? In other words is threading prepared
for me automatically (contrary to TServerSocket where
I should spawn the new thread from OnGetThread() )?
5) TServerSocket can stay in 'Active == true' but unable
to receive new requests in some circumstances.
Is that the same at TIdTCPServer? And if that's true, how
should I protect TIdTCPServer::OnExecute() from stopping
functioning?
6) How to know the number of active connections/threads?
To be more precise, I want to limit the heavy operations
in TIdTCPServer::OnExecute() so I want to do something
like this /PSEUDO/:
// ... global ...
int NumberOfHeavyThreads = 0;
// ...
void __fastcall TIndyServerForm::IdTCPServerExecute(
TIdPeerThread *AThread)
{
if ( NumberOfHeavyThreads > 20 )
{
AThread->Connection->Write...TOO MANY CONNECTIONS ...
}
else
{
// Increment NumberOfHeavyThreads in constructor
// Decrement NumberOfHeavyThreads in destructor
RAII_Inc_Dec Object( NumberOfHeavyThreads );
// HEAVY OPERATIONS ...
}
}
Is this OK?
--- CLIENT ---
void __fastcall TIndyClientForm::AskButtonClick(TObject *Sender)
{
AskButton->Enabled = false;
try
{
// CONNECTING
// ----------
IdTCPClient->Connect();
// PREPARING REQUEST
// -----------------
AnsiString REQUEST = "Can you please tell me what's the time?";
Memo->Lines->Add( REQUEST + " (prepared)" );
// SENDING TASKS
// -------------
// Sending the request length to server ...
IdTCPClient->WriteInteger( REQUEST.Length() );
// Sending the request to server ...
TStringList *SL = new TStringList;
SL->Add( REQUEST );
IdTCPClient->WriteStrings( SL );
delete SL;
Memo->Lines->Add( REQUEST + " (sent)" );
// RECEIVING TASKS
// ---------------
// Receiving expecting response size
int BytesToReceive = IdTCPClient->ReadInteger();
// Receiving Response
AnsiString RESPONSE = IdTCPClient->ReadString( BytesToReceive );
Memo->Lines->Add( RESPONSE );
}
__finally
{
IdTCPClient->Disconnect();
AskButton->Enabled = true;
}
}
--- SERVER ---
void __fastcall TIndyServerForm::IdTCPServerExecute(TIdPeerThread *AThread)
{
// RECEIVING TASKS ...
// -------------------
// How many bytes to expect ...
int BytesToRead = AThread->Connection->ReadInteger();
// Receive request ...
AnsiString REQUEST = AThread->Connection->ReadString( BytesToRead );
Memo->Lines->Add( REQUEST + " (received)" );
// PROCESSING & PREPARING ANSWER TASKS ...
// ---------------------------------------
AnsiString RESPONSE = FormatDateTime( "hh:mm AM/PM", Now() );
Memo->Lines->Add( RESPONSE + " (prepared)" );
// SENDING TASKS ...
// -----------------
// How many bytes client should expect ...
AThread->Connection->WriteInteger( RESPONSE.Length() );
// Send to respondse to client ...
TStringList *SL = new TStringList;
SL->Add( RESPONSE );
AThread->Connection->WriteStrings( SL );
delete SL;
Memo->Lines->Add( RESPONSE + " (sent)" );
}
Best Regards,
Vladimir Stefanovic
Technical Details
Message-ID:
<434503e6$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 11508e,cf1d5664f47b34d3,start X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Vladimir Stefanovic" <antivari@po.sbb.co.yu> Sender: "Vladimir Stefanovic" <antivari@po.sbb.co.yu> Reply-To: "Vladimir Stefanovic" <antivari@po.sbb.co.yu> Subject: Indy TCP C/S: Timeout , Concurrent access, Number of threads Newsgroups: borland.public.cppbuilder.internet.socket X-User-Info: 80.93.241.226 X-Forwarded-For: 128.128.129.165 NNTP-Posting-Host: 207.105.83.66 Message-ID: <434503e6$1@newsgroups.borland.com> Date: 6 Oct 2005 04:00:54 -0700 X-Trace: newsgroups.borland.com 1128596454 207.105.83.66 (6 Oct 2005 04:00:54 -0700) Lines: 174 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail