Article View: borland.public.cppbuilder.internet.socket
Article #5329Re: What is actually happening with this simple (TCS/TSS) example?
From: Remy Lebeau (TeamB)
Date: Wed, 18 Aug 2004 17:55
Date: Wed, 18 Aug 2004 17:55
58 lines
2953 bytes
2953 bytes
"Vladimir Stefanovic" <antivari@ptt.yu> wrote in message
news:41230302$1@newsgroups.borland.com...
> void __fastcall TForm1::SendButtonClick(TObject *Sender)
> {
> if ( ClientSocket1->Active == true )
> ClientSocket1->Close();
> ClientSocket1->Address="128.128.129.162";
> ClientSocket1->Open();
> ClientSocket1->Socket->SendText( "Test!" );
> }
In non-blocking mode, you can't send data until the TClientSocket's
OnConnect event has been triggered, which won't happen in that code example
because non-blocking sockets require a message queue for triggering socket
events. Calling Open() posts a message to the message queue to trigger the
OnConnect event at a later time, which you have to wait for before then
sending your data. For example:
void __fastcall TForm1::SendButtonClick(TObject *Sender)
{
if ( ClientSocket1->Active )
ClientSocket1->Close();
ClientSocket1->Address="128.128.129.162";
ClientSocket1->Open();
}
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
Socket->SendText( "Test!" );
}
> CASE 3: (Works on second and all other
> SendButton clicks, but fails on first click ?!
You probably just aren't calling ProcessMessages() often enough. You would
have to keep calling it until the socket actually finishes connecting, ie:
void __fastcall TForm1::SendButtonClick(TObject *Sender)
{
if ( ClientSocket1->Active )
ClientSocket1->Close();
ClientSocket1->Address="128.128.129.162";
ClientSocket1->Open();
while( !ClientSocket1->Socket->Connected ) // <-- added this
Application->ProcessMessages();
ClientSocket1->Socket->SendText( "Test!" );
}
> Why all this works in Blocking mode and not in NonBlocking?
Because you are not taking message handling into account properly. Blocking
mode does not use messages at all.
Gambit
Technical Details
Message-ID:
<412397c0$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<41230302$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,2324423512352dca X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <41230302$1@newsgroups.borland.com> Subject: Re: What is actually happening with this simple (TCS/TSS) example? Date: Wed, 18 Aug 2004 10:55:08 -0700 Lines: 60 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: <412397c0$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1092851648 68.69.17.41 (18 Aug 2004 10:54:08 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail