Article View: borland.public.cppbuilder.internet.socket
Article #1608Re: Server as Service - not working
From: Quentin David Jones
Date: Fri, 08 Dec 2006 03:25
Date: Fri, 08 Dec 2006 03:25
135 lines
5126 bytes
5126 bytes
Hiya,
thanks,
will follow that advice...
I appreciate your time and expertise Remy :-)
Q.
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message news:4578d02b$1@newsgroups.borland.com...
>
> "Quentin David Jones" <quentin.jones@romteck.com.spamprotect> wrote in
> message news:4578bc9c$1@newsgroups.borland.com...
>> Finally, here is the Server as a service.
>> This version does not work.
>> I suspect the inner loop in ServiceExecute.
>
> Get rid of that event handler altogether. Use the OnStart and
> OnStop/Shutdown events to activate/deactivate the server.
>
> And get rid of ShowMessage() as well. You cannot use it in a service at
> all. If you must popup a message box, then you have to use MessageBox()
> directly instead, so that you can specify the MB_SERVICE_NOTIFICATION flag.
> Otherwise, report messages to the NT Event Log instead.
>
> Try this code:
>
> void __fastcall TFileDateService::ServiceStart(TService *Sender, bool
> &Started)
> {
> ServerSocket->Active = true;
> LogMessage("Socket activated", EVENTLOG_INFORMATION_TYPE, 0, 0);
> Started = true;
> }
>
> void __fastcall TFileDateService::ServiceStop(TService *Sender, bool
> &Stopped)
> {
> ServiceShutdown(Sender);
> Stopped = true;
> }
>
> void __fastcall TFileDateService::ServicePause(TService *Sender, bool
> &Paused)
> {
> if( ServerSocket->Socket->ActiveConnections == 0 )
> {
> ServerSocket->Active = false;
> LogMessage("Socket de-activated for Svc pause",
> EVENTLOG_INFORMATION_TYPE, 0, 0);
> Paused = true;
> }
> else
> {
> ErrCode = 1; // use whatever you want
> LogMessage("Cannot pause Svc while clients are connected to the
> Socket", EVENTLOG_WARNING_TYPE, 0, 0);
> Paused = false;
> }
> }
>
> void __fastcall TFileDateService::ServiceContinue(TService *Sender, bool
> &Continued)
> {
> if( !ServerSocket->Active )
> {
> ServerSocket->Active = true;
> LogMessage("Socket re-activated for continue",
> EVENTLOG_INFORMATION_TYPE, 0, 0);
> }
> Continued = true;
> }
>
> void __fastcall TFileDateService::ServiceShutdown(TService *Sender)
> {
> if( ServerSocket->Active )
> {
> try
> {
> ServerSocket->Active = false;
> LogMessage("Socket de-activated", EVENTLOG_INFORMATION_TYPE,
> 0, 0);
> }
> catch(const Exception &e)
> {
> ErrCode = 1; // use whatever you want
> LogMessage("Socket not de-activated: " + e.Message,
> EVENTLOG_ERROR_TYPE, 0, 0);
> }
> }
> }
>
> void __fastcall TFileDateService::ServerSocketClientRead(TObject
> *Sender, TCustomWinSocket *Socket)
> {
> AnsiString FileName, FileResult;
> TSearchRec sr;
>
> // copy filename from client
> FileName = Socket->ReceiveText();
>
> // Lookup file date
> if( FindFirst(FileName, faAnyFile, sr) == 0 )
> {
> // File found - return date/time
> FindClose(sr);
>
> SYSTEMTIME st = {0};
> ::FileTimeToSystemTime(&sr.FindData.ftLastWriteTime, &st);
>
> FileResult = FileName + " date: " +
> SystemTimeToDateTime(st).DateTimeString();
> }
> else
> {
> // File Not found
> FileResult = FileName + " : File Not Found";
> }
>
> // Now return the result to the client
> Socket->SendText(FileResult);
> }
>
> Now, with that said, keep in mind that SendText() and ReceiveText() are very
> unreliable. They can send/receive partial data, which you have to take into
> account properly to ensure that your communication is not corrupted. Go to
> http://www.deja.com and search through the newsgroup archives. This has
> been discussed in great detail many times before.
>
>
> Gambit
>
>
Technical Details
Message-ID:
<4578dae2$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4578ba1b@newsgroups.borland.com> <4578bc9c$1@newsgroups.borland.com> <4578d02b$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,c2ba0c1ed7538b68 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Quentin David Jones" <quentin.jones@romteck.com.spamprotect> Newsgroups: borland.public.cppbuilder.internet.socket References: <4578ba1b@newsgroups.borland.com> <4578bc9c$1@newsgroups.borland.com> <4578d02b$1@newsgroups.borland.com> Subject: Re: Server as Service - not working Date: Fri, 8 Dec 2006 12:25:08 +0900 Lines: 137 Organization: Romteck P/L X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2869 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 203.59.8.178 Message-ID: <4578dae2$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1165548258 203.59.8.178 (7 Dec 2006 19:24:18 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail