TCP question

Hello...

I'm new to network programming. I have a windows service that uses TCP to listen for incoming packets and updates an xml file for display on a web page. My problem is that sometimes when I start the service, it seems as though I am getting the same packet twice and/or I get only part of the packet. This is especially true when I try to run a second instance of the service on a seperate machine. I assume that the answer will be obvious to experienced network programmers. Here is my code:

Imports System.data
Imports System.Net
Imports System.Net.Sockets
Imports System.IO

Public Class Service

    Private bServiceStarted As Boolean
    Private tWorkerThread As Threading.Thread
    Private MasterDataSet As New MasterDataSet()

    'Public Declare Function ntohl Lib "ws2_32.dll" (ByVal netlong As Long) As Long

    Protected Overrides Sub OnStart(ByVal args() As String)

        'Threading allows OnStart to complete and sends windows the message that the service has fully started
        Dim st As New Threading.ThreadStart(AddressOf Listen)
        tWorkerThread = New Threading.Thread(st)
        bServiceStarted = True
        tWorkerThread.Start()

    End Sub

    Protected Overrides Sub OnStop()

        'Allows Listener to finish any pending work
        Dim ts As New TimeSpan(0, 0, My.Settings.Item("OnStopSeconds"))
        bServiceStarted = False
        tWorkerThread.Join(ts)
        ts = Nothing

    End Sub

    Private Sub Listen()
        Dim cdr As CDR
        Dim i As Int32
        Dim stream As NetworkStream
        Dim bytes(My.Settings.Item("BufferSize")) As Byte ' Buffer for reading data
        Dim data As String = Nothing
        Dim prevData As String = Nothing ' Stores the last CDR to make sure we don't get the same one twice (sometimes occurs at start up)
        Dim sDatePrefix As String = Now.Month & Now.Day & Now.Year

        System.Threading.Thread.Sleep(20000) 'Makes sure log file is released on a service restart
        Dim oWriter As StreamWriter = System.IO.File.AppendText(My.Settings.Item("CDRRecordLoc") & sDatePrefix & "CDRRecord.log")

        'System.Diagnostics.Debugger.Launch()
        'System.Diagnostics.Debugger.Break()

        Try
            While bServiceStarted

                Dim client As New TcpClient
                client.Connect(New IPEndPoint(IPAddress.Parse(My.Settings.Item("ListenerIP")), My.Settings.Item("ListenerPort")))
                data = Nothing
                stream = client.GetStream() ' Get a stream object for reading and writing
                i = stream.Read(bytes, 0, bytes.Length) ' Perform a blocking call to accept requests.

                While bServiceStarted ' Loop to receive all the data sent by the client.
                    Try
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)

                        If (data <> "") And (Right(data, 275) <> Right(prevData, 275)) Then
                            If sDatePrefix <> (Now.Month & Now.Day & Now.Year) Then 'Create a new log file if the data changes
                                oWriter.Close()
                                oWriter = System.IO.File.AppendText(My.Settings.Item("CDRRecordLoc") & Now.Month & Now.Day & Now.Year & "CDRRecord.log")
                                sDatePrefix = Now.Month & Now.Day & Now.Year
                            End If

                            If My.Settings.Item("RecordCDR") Then oWriter.WriteLine(Now & ":" & data) : oWriter.Flush()
                            data = Right(data, 276)
                            cdr = New CDR(data)
                            MasterDataSet.Update(cdr)
                            cdr = Nothing
                            prevData = data
                        End If

                        i = stream.Read(bytes, 0, bytes.Length)

                    Catch ex As Exception
                        i = 0
                        oWriter.WriteLine("------------ EXCEPTION OCCURRED ------------")
                        oWriter.WriteLine("Time: " & Now)
                        oWriter.WriteLine("Message: " & ex.Message)
                        oWriter.WriteLine("Source: " & ex.Source)
                        oWriter.WriteLine("StackTrace: " & ex.StackTrace)
                        oWriter.WriteLine("--------------------------------------------")
                        oWriter.Flush()
                    End Try

                End While
                client.Close() ' Shutdown and end connection
            End While

        Catch ex As Exception
            oWriter.WriteLine("------------ EXCEPTION OCCURRED ------------")
            oWriter.WriteLine("Time: " & Now)
            oWriter.WriteLine("Message: " & ex.Message)
            oWriter.WriteLine("Source: " & ex.Source)
            oWriter.WriteLine("StackTrace: " & ex.StackTrace)
            oWriter.WriteLine("--------------------------------------------")
            oWriter.Flush()
        Finally
            oWriter.Close()
        End Try

    End Sub

End Class


Any questions/comments would be greatly appreciated!



Answer this question

TCP question