I have made an example for you.
Make a new console application.
Add references to"System.configuration", "System.Configuration.Install",
"System.ServiceProcess" and "System.Web"
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceProcess;
using System.Configuration.Install;
namespace WebRequestService
{
class Program : System.ServiceProcess.ServiceBase
{
public static String navn = "My Web log";
private bool RunThread = true;
public void StartMe()
{
System.Net.IPAddress localAddr =
System.Net.IPAddress.Parse("127.0.0.1");
System.Net.Sockets.TcpListener server = new
System.Net.Sockets.TcpListener(localAddr, 1234);
server.Start();
Byte[] bytes = new Byte[1024];
String data = null;
while (RunThread)
{
System.Net.Sockets.TcpClient client =
server.AcceptTcpClient();
data = null;
System.Net.Sockets.NetworkStream stream =
client.GetStream();
stream.Read(bytes, 0, bytes.Length);
data = System.Text.Encoding.ASCII.GetString(bytes);
System.IO.StreamWriter sw = new
System.IO.StreamWriter("c:\\MyLog.txt", true);
sw.WriteLine(data);
sw.Close();
client.Close();
}
}
protected override void OnStart(string[] args)
{
System.Threading.Thread thr = new System.Threading.Thread(new
System.Threading.ThreadStart(this.StartMe));
thr.Start();
base.OnStart(args);
}
protected override void OnStop()
{
RunThread = false;
base.OnStop();
}
static void Main(string[] args)
{
// Register server in WindowsServices
// install : c:\> installutil /LogToConsole=false
cmd_Server.exe
// uninstall : c:\> installutil /LogToConsole=false /u
cmd_Server.exe
System.ServiceProcess.ServiceBase.Run(new Program());
}
}
[System.ComponentModel.RunInstallerAttribute(true)]
public class Installer : System.Configuration.Install.Installer
{
public Installer()
{
ServiceInstaller ServiceInstaller = new ServiceInstaller();
ServiceProcessInstaller ProcessInstaller = new
ServiceProcessInstaller();
ProcessInstaller.Account = ServiceAccount.LocalSystem;
ServiceInstaller.StartType = ServiceStartMode.Automatic;
ServiceInstaller.ServiceName = "My web log";
Installers.Add(ServiceInstaller);
Installers.Add(ProcessInstaller);
}
}
}
Compile it and open a cmd.
Type:
C:\Lars-Inge\WebRequestService\bin\Debug>installutil /LogToConsole=false
WebRequestService.exe
To install it as a Windows Service.
To uninstall it, type:
C:\Lars-Inge\WebRequestService\bin\Debug>installutil /LogToConsole=false /u
WebRequestService.exe
Open the Control Panel + "Administrative tools" + "Services".
Locate "My web log" and right click it and choose "start".
Your Windows Service web listener should now be running.
To test it:
open IE and navigate to
http://localhost:1234/hhhh http://localhost:1234/skjshksajfkkkkttttttgwgggggtggssw http://localhost:1234/kjfkdjf
Open the file explorer to see the result "c:\MyLog.txt".
GET /skjshksajfkkkkttttttgwgggggtggggss HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: no
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322; .NET CLR 2.0.50727; InfoPath.1)
Host: localhost:1234
Connection: Keep-Alive
Regards,
Lars-Inge Tønnessen
Siemens Medical - Norway