Results 1 to 2 of 2

Thread: [2008] Services - Advice on usage, implementation and code

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    279

    [2008] Services - Advice on usage, implementation and code

    Hi,

    I'm trying to sort of run a web server. It works fine as a standard executable, but unfortunately on Vista, "some blocked programs do not run on startup". I did fix this by using another short script that does not require admin permissions to run to open the program on system startup and it worked... with a big UAC dialogue.

    Running a webserver, I noticed it must be installed with servicutil.exe, can I not use my SC.exe? It is part of a pre-existing suite of applications, all doing bits and I wish my installer could just run the sc create command to create the service.

    Is this practical? The application is supposed to run all the time the PC is on, I think it might be as stopping, starting, pausing makes it much easier than normal.

    I noticed there is no onload event in the Service, so I just put all my code (which loops and stuff) in the bit where it says I should startup, that should work right?

    Also, I tried a copy and paste from my application which worked. After a few tricks like getting rid of Public it still didn't work. Claims ListenSocket has an error. Can I use sockets?

    Windows Form Application Code:
    1. Option Strict On
    2. Imports System.Net.Sockets
    3. Imports System.Net
    4. Public Class WebFilter
    5.     Public listenThread As System.Threading.Thread
    6.     Public listenSocket As System.Net.Sockets.Socket
    7.     Public Const PORT_NUMBER As Integer = 80
    8.     Public Const BACKLOG As Integer = 10
    9.     Public Sub webFilter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         listenSocket = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
    11.         listenSocket.Bind(New System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), PORT_NUMBER))
    12.         listenSocket.Listen(BACKLOG)
    13.         listenThread = New System.Threading.Thread(AddressOf doListen)
    14.         listenThread.IsBackground = True
    15.         listenThread.Start()
    16.     End Sub
    17.     Public Sub doListen()
    18.  
    19.         Dim client As System.Net.Sockets.Socket
    20.         Dim clientThread As System.Threading.Thread
    21.         Do
    22.             client = listenSocket.Accept()
    23.             clientThread = New System.Threading.Thread(AddressOf RespondToHost)
    24.             clientThread.IsBackground = True
    25.             clientThread.Start(client)
    26.         Loop
    27.     End Sub
    28.     Private Sub RespondToHost(ByVal obj As Object)
    29.         Dim resp() As Byte
    30.         Dim client As System.Net.Sockets.Socket = DirectCast(obj, System.Net.Sockets.Socket)
    31.         Dim rbuff(client.Available - 1) As Byte
    32.         client.Receive(rbuff, rbuff.Length, SocketFlags.None)
    33.         resp = BuildResponse("D:\EI\databound\dt.xDms")
    34.         client.Send(resp, resp.Length, Net.Sockets.SocketFlags.None)
    35.         client.Close()
    36.  
    37.     End Sub
    38.  
    39.  
    40.  
    41.     Public Sub webFilter_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    42.         Me.Visible = False
    43.     End Sub
    44.  
    45.  
    46.  
    47.     Public Function BuildResponse(ByVal file As String) As Byte()
    48.         Dim response As New System.Text.StringBuilder
    49.         Dim sReader As System.IO.StreamReader
    50.         Dim fileContents As String
    51.         If System.IO.File.Exists(file) Then
    52.             sReader = New System.IO.StreamReader(file)
    53.             fileContents = sReader.ReadToEnd()
    54.             response.AppendLine("HTTP/1.1 200 OK")
    55.             response.AppendLine("Content-type: text/html")
    56.             response.AppendLine(fileContents.Length.ToString())
    57.             response.Append(Environment.NewLine)
    58.             response.Append(fileContents)
    59.             sReader.Close()
    60.  
    61.         Else
    62.             response.AppendLine("HTTP/1.1 404 OK")
    63.             response.Append(Environment.NewLine)
    64.         End If
    65.         Return System.Text.Encoding.UTF8.GetBytes(response.ToString())
    66.     End Function
    That is the normal code, any chance of getting it into a service? Also, if I must use VS2K8s service installer, can I automate it so I can run it hidden during installation part of my app?
    Last edited by samtheman; Dec 17th, 2008 at 04:20 PM. Reason: Changed to VBCode rather than Code
    If you found any of my posts helpful then please rate them.

    CodeBank

    Form Fading Effects in VB.NET and C#

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width