0

I'm trying to make a simple client-side application to recieve small text data, compare it and then does something on client machine depending on what server sent.

Server Logic: The server side is made in java, so can't change anything there. Server sends string "abc001" on connecting to client.

Client Logic: Client recieves the string "abc001" from server & checks if it's recieved string is the same as "abc001", then does something accordingly.

Problem: When the client recieves data, I display it in msgbox. But instead of just "abc001", there pops up an extra blank msgbox(image included).

Client Code - On Start:

    Try
        ' declare vals
        Dim ip As String = "127.0.0.1"
        Dim port As Integer = 5000

        ' set client
        _client = New TcpClient(ip, port)

        ' disable cross thread calls checking
        CheckForIllegalCrossThreadCalls = False

        ' recieve msg
        Threading.ThreadPool.QueueUserWorkItem(AddressOf RecieveMessages)
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

Client Code - Recieve Data

Private Sub RecieveMessages(state As Object)
        Try
            While True
                Dim ns As NetworkStream = _client.GetStream()
                Dim toRecieve(_client.ReceiveBufferSize) As Byte
                ns.Read(toRecieve, 0, CInt(_client.ReceiveBufferSize))
                Dim txt As String = Encoding.ASCII.GetString(toRecieve)
                MsgBox(txt)
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

MsgBox 1 String Recieved

MsgBox 2 Blank Popup?

How to not get the blank msgbox. Even when compared, the data recived does not match parameters. Tried to use delay, tried fixing the buffer size to 6 bytes but no use.. Any help is appreciated. Thanks.

EDIT 1: Tried my best to figure it out but can't.. Tried cleaning the returned string data and even tried storing each return data in array. Saw the stack and it says the msgbox has "nothing" in it. It's null.. I don't even know what to do.. Here's the code for strings clean:

Private Sub RecieveMessages(state As Object)
        Dim message(0) As String
        Dim command_raw, command_clean, command As String
        Dim counter As Integer = 0
        Try
            While True
                Dim ns As NetworkStream = _client.GetStream()
                Dim toRecieve(_client.ReceiveBufferSize) As Byte
                ns.Read(toRecieve, 0, CInt(_client.ReceiveBufferSize))
                Dim txt As String = Encoding.ASCII.GetString(toRecieve)
                message(0) = txt
                command_raw = message(0)
                command_clean = command_raw.Replace(vbCrLf, Nothing)
                command = command_clean.Substring(0, 6)
                MsgBox(command)
            End While

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
  • Port is 5000 or 58818? Are you sure the server sends abc001 and not cmd001 as it seems? – Alessandro Mandelli Nov 19 '18 at 16:06
  • abc001 is example. 6 bytes data is sent. When reading the data, there is something extra or something wrong with code. The port is same in both server and client, because set from txtbox. There should be only 1 msgbox open when 1 thing is sent.. but 2 open – SimpleCoder Nov 19 '18 at 21:04
  • 1
    This right here is the first indication that you're doing something very wrong: `CheckForIllegalCrossThreadCalls = False` - _**NEVER**_ change this to `False`! It's an important check that stops you from doing bad things that breaks your application completely! Do it right from the beginning instead so that you don't have to change anything later. See my answer at [How can I run code in a background thread and still access the UI?](https://stackoverflow.com/a/45571728/3740093) for how you can access the UI in a thread-safe manner. – Visual Vincent Nov 20 '18 at 06:31
  • It's probably just an extra cr+lf – Alessandro Mandelli Nov 20 '18 at 07:17
  • Thanks @VisualVincent Will try implementing safe way. – SimpleCoder Nov 20 '18 at 13:01
  • @AlessandroMandelli It's not. I tried replacing vbCrLf with replace() and also tried regex but still gives that 1 extra msgbox – SimpleCoder Nov 20 '18 at 13:08
  • Will you please put a breakpoint on Msgbox(command) and print the exact content of the variable "command"? Also please comment out command = command_clean.Substring(0, 6) and let us know command.length – Alessandro Mandelli Nov 20 '18 at 14:37
  • In addition to Alessandro's suggestion could you also show us what `toRecieve` contains the second time the message box is about to be shown? No need for the entire array if it's all zeros, only the non-zero parts are necessary (if such exist). – Visual Vincent Nov 20 '18 at 16:18

0 Answers0