Article View: borland.public.cppbuilder.internet.socket
Article #1606Re: Server as Service - not working
From: Remy Lebeau (TeamB)
Date: Fri, 08 Dec 2006 02:39
Date: Fri, 08 Dec 2006 02:39
121 lines
4637 bytes
4637 bytes
"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:
<4578d02b$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<4578ba1b@newsgroups.borland.com> <4578bc9c$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: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <4578ba1b@newsgroups.borland.com> <4578bc9c$1@newsgroups.borland.com> Subject: Re: Server as Service - not working Date: Thu, 7 Dec 2006 18:39:13 -0800 Lines: 123 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: 75.83.34.171 Message-ID: <4578d02b$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1165545515 75.83.34.171 (7 Dec 2006 18:38:35 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail