Article View: borland.public.cppbuilder.internet.socket
Article #4510Re: stThreadBlocking Vs stNonBlocking confusion
From: Remy Lebeau (TeamB)
Date: Tue, 01 Feb 2005 19:11
Date: Tue, 01 Feb 2005 19:11
55 lines
2926 bytes
2926 bytes
"Stefano Bonifazi" <stefboombastic@email.it> wrote in message
news:41ffbb99@newsgroups.borland.com...
> void __fastcall TForm2::ServerSocket1ClientConnect(TObject *Sender,
> TCustomWinSocket *Socket)
> {
> Memo1->Lines->Add("Client connected");
> }
You cannot do that. In blocking mode, the server uses a separate worker
thread to listen for incoming connections. The OnConnect event is triggered
in the context of that thread, not the main thread. The VCL is not
thread-safe in general, and as such accessing the GUI directly from a worker
thread can be dangerous. You must synchronize access to the GUI so that the
code accessing the GUI is executed in the context of the main thread only.
> void __fastcall TForm2::ServerSocket1ClientRead(TObject *Sender,
> TCustomWinSocket *Socket)
> {
> Memo1->Lines->Add("Inside OnClientRead");
> Application->ProcessMessages();
> Memo1->Lines->Add("Client: "+Socket->ReceiveText());
> Application->ProcessMessages();
> Socket->SendText("Bye!");
> }
Same here. You are accessing the GUI in an unsafe manner, because
OnClientRead is going to be triggered in the context of a worker thread, not
the main thread. Not only that, but you should never call ProcessMessages()
from the events of a blocking server, either.
> void __fastcall TForm2::ServerSocket1ClientWrite(TObject *Sender,
> TCustomWinSocket *Socket)
> {
> Memo1->Lines->Add("OnClientWrite");
> Socket->SendText("Bye!");
> }
You don't need to use the OnClientWrite event with a blocking server.
> void __fastcall TForm2::ServerSocket1ClientError(TObject *Sender,
> TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
> {
> Memo1->Lines->Add("Error");
> }
Still accessing the GUI in an unsafe manner.
> It seems dead, nothing is written to the Memo :(
That is what happens when you do not access the GUI in a thread-safe manner.
Gambit
Technical Details
Message-ID:
<41ffd3aa$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<41fe906e@newsgroups.borland.com> <41fe99bf$1@newsgroups.borland.com> <41ff64b9@newsgroups.borland.com> <41ffbb99@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,4f7607071b9a8537 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: <41fe906e@newsgroups.borland.com> <41fe99bf$1@newsgroups.borland.com> <41ff64b9@newsgroups.borland.com> <41ffbb99@newsgroups.borland.com> Subject: Re: stThreadBlocking Vs stNonBlocking confusion Date: Tue, 1 Feb 2005 11:11:26 -0800 Lines: 57 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: <41ffd3aa$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1107284906 68.69.17.41 (1 Feb 2005 11:08:26 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail