🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #1675

Re: TClientSocket in consolle applications

#1675
From: Bob Gonder
Date: Sun, 24 Dec 2006 01:32
102 lines
3979 bytes
Stefano Bonifazi wrote:

>> You might be able to have your connect function spawn a new thread to
>> pump messages (create a hidden window to receive messages). The
>> disconnect function could then send a private message for the thread
>> to terminate (WM_CLOSE the hidden window).
>
>Please could U be so kind to show how to do? or a link where it is showed?

I was going to show the Charles Petzold way (plain WinAPI), but VCL is
probably going to be easier (not that I know what I'm doing in VCL....
In fact, I'm pretty sure this won't work as written, but this is where
I would start working.)

Probably easiest is to have a TApplication::Main1 in your dll.
Set the form's visibility property to hidden.
Drop a TClientSocket on the form.
Also create a thread to run the "application-form".

class TMyThread : public TThread
{
private:
protected:
  void __fastcall Execute();
public:
__fastcall TMyThread(bool CreateSuspended);
};


TMain1*	Main1 = NULL;
TMyThread *MyThread;


__fastcall TMyThread::TMyThread(bool CreateSuspended)
: TThread(CreateSuspended)
{
}

void __fastcall TMyThread::Execute()
{
#if ThisWorks
	Main1 = new TMain1( NULL );
	Main1->Hide();
	while( ! terminated )
	{
		//process all outstanding messages
		Main1->ProcessMessages();	
		//slow it down so the thread doesn't hit so hard
		Sleep(0);
	};
	delete Main1; //not sure about this
#else
                Application->Initialize();
                Application->CreateForm(__classid(TMain1), &Main1);
                Application->Run();
#endif
	Main1 = NULL;
}


You wanted a simple 3-function interface, so....
The DLL interfaces:

MyConnect( AnsiString &Host )
{	MyThread = new TMyThread(false);
	while( Main1 == NULL )	/* could change to event semiphore */
		Sleep( 10 );	/* to avoid the dreaded Sleep() */
	Main1->Client->Connect( Host );
}
MySend( AnsiString &Message )
{	if( Main1 )
	Main1->Client->Send( Message );
}
MyDisconnect()
{	if( Main1 )
	Main1->Client->Disconnect();
}


Then inside the Client Disconnect, at the very end...
	Main1->Close();
#if ThisWorks
	MyThread->Terminate();
#else
	//thread self-terminates
#endif
which should allow the thread to exit.


I would be tempted to create and destroy the form in the dll entry
point (see DllMain in help) instead of the user interface, using the
DLL_PROCESS_ATTACH and  DLL_PROCESS_DETACH flags to determine
if creating or destroying the form.  That should help eliminate some
of the race conditions in the above code, and also removes specialized
code from the TClientSocket methods.


Now that I look at this, it looks like swatting a gnat with a
thermonuclear device.  In actuallity, I've done all this in plain
WinAPI with blocking WinSocks, and it didn't turn out to be that bad.
Bringing in the entire VCL just to run one socket seems a tad
overkill.
Technical Details
Message-ID: <fv1ro296vpodms19ois98boakdgo5cd59r@4ax.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <458d3f0d@newsgroups.borland.com> <rklqo2pjrnk6arfk43f665lq65djkr55vq@4ax.com> <458d72cc@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,dab0ff5b2d1109
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Bob Gonder <notbg@notmindspring.invalid>
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Re: TClientSocket in consolle applications
Date: Sat, 23 Dec 2006 17:32:39 -0800
Organization: MindSpring Enterprises, Inc.
Reply-To: notbg@notmindspring.invalid
Message-ID: <fv1ro296vpodms19ois98boakdgo5cd59r@4ax.com>
References: <458d3f0d@newsgroups.borland.com> <rklqo2pjrnk6arfk43f665lq65djkr55vq@4ax.com> <458d72cc@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 71.0.35.145
X-Trace: newsgroups.borland.com 1166923939 71.0.35.145 (23 Dec 2006 17:32:19 -0800)
Lines: 105
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail