Results 1 to 5 of 5

Thread: Windows service creation gives an error

  1. #1
    Join Date
    Nov 2009
    Posts
    60

    Windows service creation gives an error

    I created a windows service but I get an error when I run it:
    service on local computer started and then it stopped. Some services stop automatically if they are not used by other services or programs.
    But I carry a calculator that will connect to this service will pay the bill and return the same result in this application. In short, please help me. The code for my service

    Code:
    Imports System.Net.sockets
     
    Public Class Service1
     
        Private listener As Net.Sockets.TcpListener
        Const PORT_NUM As Integer = 1039
        Dim EndPoint As Net.IPEndPoint
     
        Protected Overrides Sub OnStart(ByVal args() As String)
            Dim iip As Long = 16777343
            EndPoint = New Net.IPEndPoint(iip, 1039)
            listener.Server.Bind(EndPoint)
            listener.Start()
            Dim state As New StateObject()
            state.workSocket = listener
            listener.Server.Receive(state.buffer, 1, state.BufferSize, Net.Sockets.SocketFlags.None)
            Dim letter As String = state.sb.ToString
            Dim table As Array = letter.Split("/")
            Dim nb1, nb2, nb3 As Double
            nb1 = Double.Parse(table.GetValue(0))
            nb2 = Double.Parse(table.GetValue(1))
            nb3 = nb1 + nb2
            Dim fin As String = nb3.ToString
            Dim Content() As Byte = System.Text.Encoding.ASCII.GetBytes(fin.ToCharArray())
            Dim size As Integer = Content.Length
            listener.Server.Send(Content, 1, size, Net.Sockets.SocketFlags.None)
        End Sub
     
        Protected Overrides Sub OnStop()
            listener.Stop()
        End Sub
     
        Public Class StateObject
            Public workSocket As System.Net.Sockets.TcpListener = Nothing
            Public BufferSize As Integer = 256
            Public buffer(256) As Byte
            Public sb As New System.Text.StringBuilder()
        End Class 'StateObject
    End Class

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Windows service creation gives an error

    Your Sub OnStart does not seem to do much. If so, at the end of the sub, the service stops. Making a read or BeginRead on the socket, it should hold the program and thus prevent the service stop. General service in OnStart is just a start in a thread and the thread continues an infinite loop by processing.

  3. #3
    Join Date
    Nov 2009
    Posts
    60

    Re: Windows service creation gives an error

    Thank you for your answer! I did this and tried to see if the windows service is listening:
    Code:
    Imports System.Net.sockets
    Public Class Calculate
     
        Private listener As Net.Sockets.TcpListener
        Const PORT_NUM As Integer = 1039
        Dim EndPoint As Net.IPEndPoint
    
        Protected Overrides Sub OnStart(ByVal args() As String)
            'ip : 10.80.109.132 gives:'
            Dim iip As Long = 16777343
            'ip localhost : 127.0.0.1'
            EndPoint = New Net.IPEndPoint(iip, 1039)
            listener.Server.Bind(EndPoint)
            listener.Start()
     
            Dim data As String
            Dim bytes(1024) As Byte
            While True
                Dim client As TcpClient = listener.AcceptTcpClient()
                data = Nothing
                Dim Stream As NetworkStream = client.GetStream()
                Dim i As Int32
                i = stream.Read(bytes, 0, bytes.Length
                While (i <> 0)
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                    Dim nb1, nb2, nb3 As Double
                    Dim table As Array = data.Split("/")
                    nb1 = Double.Parse(table.GetValue(0))
                    nb2 = Double.Parse(table.GetValue(1))
                    nb3 = nb1 + nb2
                    Dim fin As String = nb3.ToString
                    Dim Content() As Byte = System.Text.Encoding.ASCII.GetBytes(fin.ToCharArray())
                    Stream.Write(Content, 0, Content.Length)
                    i = Stream.Read(bytes, 0, bytes.Length)
     
                End While
                client.Close()
            End While
     
        End Sub
     
        Protected Overrides Sub OnStop()
            listener.Stop()
        End Sub
     
    End Class

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Windows service creation gives an error

    Why don't you put a try catch block in your code with registration error somewhere, because if an error occurs, the service stops and you do not see the error. The OnStart should be light, and thus it should just be used to start the service, but I do not guarantee about the solution but this is just a suggestion.

  5. #5
    Join Date
    Nov 2009
    Posts
    60

    Re: Windows service creation gives an error

    I managed to part the service and I owe a lot (I set the automatic start). Unfortunately, here 20 minutes that I fight with the party sending data to the calculator and I'm looking on google I found nothing:

    I made this procedure that sends data on the ip and port that I made a listen:

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
     
            Dim n1 As String
            Dim n2 As String
            Dim nn As String
            Dim endpoint As Net.IPEndPoint
            Try
     
                n1 = TextBox1.Text
                n2 = TextBox2.Text
                nn = n1 + "/" + n2
     
                Dim Content() As Byte = System.Text.Encoding.ASCII.GetBytes(nn.ToCharArray())
                Dim ls As Integer = Content.Length
                'ip : 10.80.109.132 gives:'
                Dim iiip As Long = 16777343
                'ip localhost : 127.0.0.1'
                endpoint = New Net.IPEndPoint(iiip, 1039)
                Dim foo As Net.Sockets.TcpClient = New Net.Sockets.TcpClient(endpoint)
                tutu.Connect(endpoint)
                Dim stream As Net.Sockets.NetworkStream = foo.GetStream()
     
                stream.Write(Content, 0, Content.Length)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    End Class

Similar Threads

  1. sql server new database creation error. .
    By Rizwan_Akhter in forum Windows Software
    Replies: 1
    Last Post: 17-10-2011, 11:41 AM
  2. Replies: 5
    Last Post: 29-09-2011, 01:03 PM
  3. Replies: 3
    Last Post: 27-01-2010, 07:48 PM
  4. about creation file error
    By shortie009 in forum Operating Systems
    Replies: 3
    Last Post: 09-12-2008, 11:49 AM
  5. Error 0x80070032 - Restore point creation
    By RedZedSmee in forum Vista Help
    Replies: 14
    Last Post: 09-06-2008, 06:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,279,243.59808 seconds with 16 queries