Article View: borland.public.cppbuilder.internet.socket
Article #2977Re: Modify the timeout for open() method
From: Remy Lebeau (TeamB)
Date: Fri, 02 Dec 2005 19:20
Date: Fri, 02 Dec 2005 19:20
53 lines
2964 bytes
2964 bytes
"enzo" <enzo.arlati@aesys.it> wrote in message
news:43901442$1@newsgroups.borland.com...
> Application->ProcessMessages() used inside a thread should
> not be a good idea, but for now is working.
You are right that it is not a good idea. ProcessMessages() is not designed
to be called from outside of the main thread. It is not a thread-safe
method. DO NOT call ProcessMessages() from the context of a worker thread.
If you must use a non-blocking socket in a thread, then you should write
your own message queue manually by calling PeekMessage(), TranslateMessage()
and DispatchMessage() instead of ProcessMessages(), ie:
clientSck = new TClientSocket( NULL );
clientSck->ClientType = ctNonBlocking;
clientSck->Address = ipTargetFep;
clientSck->Port = portTargetFep;
clientSck->Open();
MSG msg;
for(int idx = 0; (idx < 100) && (!clientSck->Active); ++idx)
{
if( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Sleep(10);
}
if( !clientSck->Active )
{
//...
}
> clientSck->Close();
> clientSck->ClientType = ctBlocking;
> clientSck->Open();
Why are you closing the socket immediately after opening it? You should not
be doing that. There is no guarantee that the second Open() will succeed,
so you'll be right back to square one again.
If you really want to use a blocking socket in a thread with a connect
timeout, then I suggest not using TClientSocket in the first place. Use
Indy (http://www.indyproject.org) instead. It uses blocking sockets for
everything and is designed to be used with threading. Its TIdTCPClient
component has a connect timeout available.
Gambit
Technical Details
Message-ID:
<43909ead$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!news.wiretrip.org!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!nntp.theplanet.net!inewsm1.nntp.theplanet.net!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<438ed040@newsgroups.borland.com> <438f4204$1@newsgroups.borland.com> <438ffe59@newsgroups.borland.com> <43901442$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,234cf6f94ba290e1 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: <438ed040@newsgroups.borland.com> <438f4204$1@newsgroups.borland.com> <438ffe59@newsgroups.borland.com> <43901442$1@newsgroups.borland.com> Subject: Re: Modify the timeout for open() method Date: Fri, 2 Dec 2005 11:20:28 -0800 Lines: 55 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: <43909ead$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1133551277 68.69.17.41 (2 Dec 2005 11:21:17 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!news.wiretrip.org!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!nntp.theplanet.net!inewsm1.nntp.theplanet.net!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail