TcpClient - doesn't get message

vbBart

Member
Joined
Feb 27, 2008
Messages
16
Programming Experience
1-3
I have some code what is working at the moment. But when I trie to send a message from the server to client, the client don't get it. Here is my code

VB.NET:
    Dim client As Sockets.TcpClient = New Net.Sockets.TcpClient()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        client.Connect("192.168.1.102", 3333)
        sendMessage("TEST")

        Dim tr As Threading.Thread
        tr = New Threading.Thread(AddressOf read)
        tr.Start()
    End Sub

    Private Sub sendMessage(ByVal text)
        If client.Connected = True Then
            Dim buffer() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(Text)
            client.GetStream.Write(buffer, 0, buffer.Length)
            client.GetStream.Flush()
        End If
    End Sub

    Private Sub read()
        Dim listen As Boolean = True

        Do While listen
            Dim buffer(1000) As Byte
            networkStream = client.GetStream
            networkStream.Read(buffer, 0, 1000)
            If Not System.Text.ASCIIEncoding.ASCII.GetString(buffer) = "" Then
                MsgBox(System.Text.ASCIIEncoding.ASCII.GetString(buffer))
                listen = False
                client.Close()
            End If
        Loop
    End Sub
 
TcpClient doesn't recieve message

Hello,

I've create a working code for my tcp client and tcp listener. But when the Client is connected to the server. And when the client send data to the server, and the server sends back it don't recive but I've I send a message on a moment it will recieve by the client. See my code

VB.NET:
'Server
    Private Sub verwerk(ByVal tekst)
      if tekst = "TEST" then
         sendMessage("OK")
      end if
    End Sub

    Private Sub sendMessage(ByVal text)
        Dim responseString As String = text
        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
        networkStream.Write(sendBytes, 0, sendBytes.Length)
    End Sub

'Client
    Private Sub send(ByVal text)
        ' Do a simple write.
        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(text)
        networkStream.Write(sendBytes, 0, sendBytes.Length)
        bgWorker.RunWorkerAsync()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        send("TEST")
    End Sub
 
Back
Top