dcsimg
Results 1 to 5 of 5

Thread: TCPClient Receive String Delimiters

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    2

    TCPClient Receive String Delimiters

    I'm working on a project that handles TCP data strings coming in on one port, it will make a simple translation and then throw them back out as HTTP requests to a server. For ease at the minute I am writing it as a forms application. Sadly I have no influence over the strings but they have start and ends that are string value delimiters (the words "QE" and "QS"), sadly there are no control characters like line feed or such like else my life would be somewhat easier, the data is also not fixed length so I am going to have to check for the delimiters. All of the simple TCP libraries for .net seem to rest on single byte value delimiters rather than strings and I can't split on 'S' because that may be elsewhere in the data - it has to be the full 'QS' string.

    Can you please advise the best way of handling this, I am just muddling through .net really and don't really understand the finer points of streams and the like. I've created a TCP client and I get data here - obviously it is only reading in 100 bytes and finishing (just to determine whether or not it works - it does) but this probably needs to be extended to 1024 or such. What I don't get is the best way to determine the end of a string and then do something about it? I've looked at some similar examples and it looks as if the bytes are incrementally read on the thread but I'm at a bit of a loss how I would queue everything up and not get caught out with mixed messages.

    Many Thanks for your help,

    Code:
     Private Sub ReceiveMessages()
    
            Try
    
                While QServer.Connected
    
                    Dim ns As NetworkStream = QServer.GetStream
    
                    Dim ToReceive(100) As Byte
    
                    ns.Read(ToReceive, 0, ToReceive.Length)
    
                    'end of message is QE and the start is QS
    
    
    
                    Debug.WriteLine(Encoding.ASCII.GetString(ToReceive))
    
                End While
            Catch ex As Exception
                Debug.WriteLine("RX Exception: " & ex.Message)
            End Try
    
        End Sub

  2. #2
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    144

    Re: TCPClient Receive String Delimiters

    may be this could help you out?

    Code:
    Dim Bytes As Integer = -1
            Dim Buffer(1024) As Byte
            Dim Message As String
            Dim ClientStream As System.Net.Sockets.NetworkStream = Client.GetStream
    
            Do While Bytes <> 0
                Bytes = ClientStream.Read(Buffer, 0, Buffer.Length)
                Message &= System.Text.Encoding.ASCII.GetString(Buffer)
                If Message.IndexOf("QE") <> -1 Then
                    ' Found the end of the message...
                    Exit Do
                End If
            Loop
    Last edited by Goggy; May 22nd, 2019 at 06:56 AM. Reason: For got the "&"
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2019
    Posts
    2

    Re: TCPClient Receive String Delimiters

    Quote Originally Posted by Goggy View Post
    may be this could help you out?

    Code:
    Dim Bytes As Integer = -1
            Dim Buffer(1024) As Byte
            Dim Message As String
            Dim ClientStream As System.Net.Sockets.NetworkStream = Client.GetStream
    
            Do While Bytes <> 0
                Bytes = ClientStream.Read(Buffer, 0, Buffer.Length)
                Message &= System.Text.Encoding.ASCII.GetString(Buffer)
                If Message.IndexOf("QE") <> -1 Then
                    ' Found the end of the message...
                    Exit Do
                End If
            Loop
    Thank you for the reply...indeed I think I am getting closer. I've modified your code slightly to this;

    Code:
        Private Sub ReceiveMessages()
    
            Try
    
                Dim Bytes As Integer = -1
                Dim Buffer(1024) As Byte
                Dim Message As String = ""
                Dim ClientStream As NetworkStream = QServer.GetStream
    
                Do While Bytes <> 0
    
                    Bytes = ClientStream.Read(Buffer, 0, Buffer.Length)
    
                    Message &= Encoding.ASCII.GetString(Buffer)
    
                    If Message.IndexOf("QS") = 0 And Message.IndexOf("QE") > 1 Then
    
                        ParseMessage(Message)
                        Array.Clear(Buffer, 0, 1024)
                        Message = Nothing
    
                    End If
    
                Loop
    
            Catch ex As Exception
                Debug.WriteLine("RX Exception: " & ex.Message)
            End Try
    
        End Sub
    But it still does not work as I expected because ParseMessage (which just writes out the message in the debug window) just prints out the message continually rather than by each line. As this is a continual stream of incoming data then if I exit the do loop as you suggest then I will only get one message - unless I should be kicking off the thread again which seems inefficient? I thought by resetting the array then I would just go back to the top again.

    Thanks

  4. #4
    Addicted Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    144

    Re: TCPClient Receive String Delimiters

    Wrap it all in a while loop...

    something like so i would imagine

    Code:
            Dim clientStream As NetworkStream
            Dim encoder As ASCIIEncoding = New ASCIIEncoding()
            Dim bytes As Integer = -1
            Dim data As Byte() = New Byte(1023) {}
            Me.responseData = String.Empty
    
            If client IsNot Nothing Then
                clientStream = client.GetStream()
    
                While True
                    Try
    
                        If clientStream.CanRead Then
                            bytes = -1
                            Me.responseData = String.Empty
    
                            Do
                                bytes = clientStream.Read(data, 0, data.Length)
                                Me.responseData += Encoding.ASCII.GetString(data, 0, bytes)
    
                                If Me.responseData.ToString().IndexOf("QE") <> -1 Then
                                    Exit Do
                                End If
                            Loop While bytes <> 0
    
                            ' Do something with the message
    
                        End If
    
                    Catch ex As Exception
                        Console.WriteLine(ex.ToString())
                        Exit While
                    End Try
                End While
            End If
    
            Console.WriteLine("Thats it, I'm out of here! (o.O)")
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  5. #5
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,169

    Re: TCPClient Receive String Delimiters

    Quote Originally Posted by faceache View Post
    But it still does not work as I expected because ParseMessage (which just writes out the message in the debug window) just prints out the message continually rather than by each line.
    Maybe I'm not understanding what you mean, but if you append ControlChars.CrLf to Message they would appear on separate lines in the output window.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width