1

I'm trying to build a windows service tcpip server to install on some computer to be able to send messages to them... The following code is working perfectly if I run it as a normal windows application but if I use it to create a windows service it doesn't run as expected. Throught the Visual studio "attach debug" I can see the debug and every time I send a request from the client I see this:

The thread 0xf34 has exited with code 259 (0x103).

That means the thread was entered but no output, or console.write...

Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading

Public Class Main

Private serverSocket As TcpListener
Private Delegate Sub WriteMessageDelegate(ByVal msg As String)
Dim listenThread As New Thread(New ThreadStart(AddressOf ListenForClients))

Private Sub ListenForClients()
    serverSocket = New TcpListener(IPAddress.Any, 11000)
    serverSocket.Start()

    Console.WriteLine("Listen for clients...")

    While True  'blocks until a client has connected to the server
        Dim client As TcpClient = Me.serverSocket.AcceptTcpClient()
        Dim clientThread As New Thread(New ParameterizedThreadStart(AddressOf HandleClientComm))
        clientThread.Start(client)
    End While
End Sub

Private Sub HandleClientComm(ByVal client As Object)
    Dim tcpClient As TcpClient = DirectCast(client, TcpClient)
    Dim clientStream As NetworkStream = tcpClient.GetStream()

    Dim message As Byte() = New Byte(4095) {}
    Dim bytesRead As Integer

    Console.WriteLine("Handle client comm...")

    While True
        bytesRead = 0
        bytesRead = clientStream.Read(message, 0, 4096) 'blocks until a client sends a message

        If bytesRead = 0 Then
            Exit While 'the client has disconnected from the server
        End If

        'message has successfully been received

        'Dim encoder As New ASCIIEncoding()
        'Dim serverResponse As String = "Response to send"
        'Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(serverResponse)
        'clientStream.Write(sendBytes, 0, sendBytes.Length)

        Console.WriteLine(bytesRead)
        'message has successfully been received
        Dim encoder As New ASCIIEncoding()

        ' Convert the Bytes received to a string and display it on the Server Screen
        Dim msg As String = encoder.GetString(message, 0, bytesRead)
        Console.WriteLine(msg)
        'WriteMessage(msg)

    End While
    tcpClient.Close()

End Sub


Private Function BytesToString(
ByVal bytes() As Byte) As String
    Return Encoding.Default.GetString(bytes)
End Function

Private Sub WriteMessage(ByVal msg As String)

    If Me.MessagesLog.InvokeRequired Then
        Dim d As New WriteMessageDelegate(AddressOf WriteMessage)
        Me.MessagesLog.Invoke(d, New Object() {msg})
    Else
        Me.MessagesLog.AppendText(msg & Environment.NewLine)
    End If


End Sub

Protected Overrides Sub OnStart(ByVal args() As String)
    listenThread.Start()
    Console.WriteLine("Starting...")
End Sub




Protected Overrides Sub OnStop()
    ' Add code here to perform any tear-down necessary to stop your service.
    'listenThread.Abort()
End Sub

End Class

Can someone help me?

Adriano Pedro
  • 437
  • 5
  • 13
  • You might try adding logging to get the proper error, http://stackoverflow.com/questions/18887864/what-is-a-thread-exit-code – the_lotus Jan 13 '15 at 15:59
  • Yes... I understand... but that is not an error and means that the thread has just run and ended... If you see my code it has some output (console.writeline) that should be sent out... but nothing... In the windows app version I also get the "The thread 0xf34 has exited with code 259 (0x103)." output along with the consle.wrtie outputs... – Adriano Pedro Jan 13 '15 at 16:44
  • See if your service has generated any errors in the Event Viewer (Control Panel...Administrative Tools...Event Viewer). – sevzas Jan 14 '15 at 01:26
  • No errors are generated in Event Viewer... Just the normal Start and Stop Log of the service... Has I said from the client I can see that the connection is ok and the message is received... but the server (service) seems to do nothing, even the console.write (for debugging) is not triggered... It is very strange... – Adriano Pedro Jan 14 '15 at 17:45

1 Answers1

0

Found the problem...

Windows services dont do output to console.write()... it has to be with debug.print() "The Thread..." output is normal..

Thank you, AP

Adriano Pedro
  • 437
  • 5
  • 13