I have this following code written and running as a windows service. Each time some data is received, the data will be written to the log file. However, after i send more than 1 set of data to that listening port, it simply would not record in the logfile more than 1 record, anyone can show me what is wrong with my codes? and 1 more thing, is it possible or advisable to use a window service such as this to store the data it received to a database via ODBC or do i need to create another project to talk to this windows service? if so, how should i go about doing it? thanks.
Imports System.ServiceProcess
Imports System.Net.Sockets
Imports System.Threading
Private Listener As TcpListener
Private StopListener As Boolean
Private AlreadyConnected As Boolean = False
Private LogFile = "c:\temp\test.log"
Private StateObject As StateObjectClass
Private TEST_ID As String
Private Default_TEST_ID = "TEST01"
Private CIL_Len As Integer
Private TCP_Port As Integer
Private myLog As Common.myEventLog
Protected Overrides Sub OnStart(ByVal args() As String)
myLog = New Common.myEventLog()
myLog.mySource = "TCPIP"
myLog.WriteToLog("TCPIP Started.")
WriteToLogFile("Service Started.")
If args.Length = 0 Then
TEST_ID = Default_TEST_ID
Else
If Len(args(0)) = 4 Then
TEST_ID = args(0)
Else
TEST_ID = Default_TEST_ID
End If
End If
WriteToLogFile("TEST_ID = " + TEST_ID)
CIL_Len = 116
TCP_Port = 6006
Listener = New TcpListener(TCP_Port)
Listener.Start()
StateObject = New StateObjectClass()
Dim timerDelegate As New TimerCallback(AddressOf TimerTick)
Dim timer As New Timer(timerDelegate, StateObject, 1000, 1000)
StateObject.TimerReference = timer
End Sub
Protected Overrides Sub OnStop()
StopListener = True
Listener.Stop()
StateObject.TimerReference.Dispose()
myLog.WriteToLog("TCPIP Stopped.")
WriteToLogFile("Service Stopped.")
End Sub
Protected Sub ProcessRequest()
Dim CurSocket As Socket
Dim Buffer(CIL_Len) As Byte
Dim Bytes As Integer
Dim IncomingString As String
Dim LeftOverString As String
WriteToLogFile("Connection established.")
CurSocket = Listener.AcceptSocket
While Not StopListener
If CurSocket.Available > 0 Then
Bytes = CurSocket.Receive(Buffer, Buffer.Length, 0)
WriteToLogFile("Bytes = " + Bytes.ToString)
IncomingString = System.Text.Encoding.Default.GetString(Buffer)
If Bytes = CIL_Len And IncomingString.Substring(2, 1) = " " Then
ProcessString(Left(IncomingString, CIL_Len - 5))
Else
WriteToLogFile(IncomingString)
End If
Else
System.Threading.Thread.CurrentThread.Sleep(100)
End If
If Not CurSocket.Connected Then
StopListener = True
End If
End While
CurSocket.Close()
AlreadyConnected = False
WriteToLogFile("Connection dropped.")
End Sub
Private Sub ProcessString(ByVal Str As String)
WriteToLogFile(Str)
Dim Logger As New localhost.CallLogging()
Logger.LogData(TEST_ID, Str)
End Sub
Private Sub WriteToLogFile(ByVal strLog As String)
FileOpen(1, LogFile, OpenMode.Append, OpenAccess.Write, OpenShare.Shared)
PrintLine(1, "(" + Now + ")" + vbCrLf + strLog)
FileClose(1)
End Sub
Private Sub TimerTick(ByVal StateObj As Object)
If Not Listener.Pending() Then
Exit Sub
End If
If Not AlreadyConnected Then
AlreadyConnected = True
ProcessRequest()
End If
End Sub
End Class
Class StateObjectClass
Public TimerReference As System.Threading.Timer
Public TimerCanceled As Boolean
End Class
Imports System.ServiceProcess
Imports System.Net.Sockets
Imports System.Threading
Private Listener As TcpListener
Private StopListener As Boolean
Private AlreadyConnected As Boolean = False
Private LogFile = "c:\temp\test.log"
Private StateObject As StateObjectClass
Private TEST_ID As String
Private Default_TEST_ID = "TEST01"
Private CIL_Len As Integer
Private TCP_Port As Integer
Private myLog As Common.myEventLog
Protected Overrides Sub OnStart(ByVal args() As String)
myLog = New Common.myEventLog()
myLog.mySource = "TCPIP"
myLog.WriteToLog("TCPIP Started.")
WriteToLogFile("Service Started.")
If args.Length = 0 Then
TEST_ID = Default_TEST_ID
Else
If Len(args(0)) = 4 Then
TEST_ID = args(0)
Else
TEST_ID = Default_TEST_ID
End If
End If
WriteToLogFile("TEST_ID = " + TEST_ID)
CIL_Len = 116
TCP_Port = 6006
Listener = New TcpListener(TCP_Port)
Listener.Start()
StateObject = New StateObjectClass()
Dim timerDelegate As New TimerCallback(AddressOf TimerTick)
Dim timer As New Timer(timerDelegate, StateObject, 1000, 1000)
StateObject.TimerReference = timer
End Sub
Protected Overrides Sub OnStop()
StopListener = True
Listener.Stop()
StateObject.TimerReference.Dispose()
myLog.WriteToLog("TCPIP Stopped.")
WriteToLogFile("Service Stopped.")
End Sub
Protected Sub ProcessRequest()
Dim CurSocket As Socket
Dim Buffer(CIL_Len) As Byte
Dim Bytes As Integer
Dim IncomingString As String
Dim LeftOverString As String
WriteToLogFile("Connection established.")
CurSocket = Listener.AcceptSocket
While Not StopListener
If CurSocket.Available > 0 Then
Bytes = CurSocket.Receive(Buffer, Buffer.Length, 0)
WriteToLogFile("Bytes = " + Bytes.ToString)
IncomingString = System.Text.Encoding.Default.GetString(Buffer)
If Bytes = CIL_Len And IncomingString.Substring(2, 1) = " " Then
ProcessString(Left(IncomingString, CIL_Len - 5))
Else
WriteToLogFile(IncomingString)
End If
Else
System.Threading.Thread.CurrentThread.Sleep(100)
End If
If Not CurSocket.Connected Then
StopListener = True
End If
End While
CurSocket.Close()
AlreadyConnected = False
WriteToLogFile("Connection dropped.")
End Sub
Private Sub ProcessString(ByVal Str As String)
WriteToLogFile(Str)
Dim Logger As New localhost.CallLogging()
Logger.LogData(TEST_ID, Str)
End Sub
Private Sub WriteToLogFile(ByVal strLog As String)
FileOpen(1, LogFile, OpenMode.Append, OpenAccess.Write, OpenShare.Shared)
PrintLine(1, "(" + Now + ")" + vbCrLf + strLog)
FileClose(1)
End Sub
Private Sub TimerTick(ByVal StateObj As Object)
If Not Listener.Pending() Then
Exit Sub
End If
If Not AlreadyConnected Then
AlreadyConnected = True
ProcessRequest()
End If
End Sub
End Class
Class StateObjectClass
Public TimerReference As System.Threading.Timer
Public TimerCanceled As Boolean
End Class
Last edited by a moderator: