Windows service creation gives an error
I created a windows service but I get an error when I run it:
Quote:
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
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.
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
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.
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