You need to handle the result of the read in the Callback and send it to the UI from there.
The way it is currently setup looks like a synchronous read which just doest't work async.
And with a bit more explanation:
Dim NetStream As NetworkStream
Dim AsynCallback As AsyncCallback
Dim Client As TcpClient = New TcpClient()
Dim ByteStream(1024) As Byte
Private Sub Login()
Try
If IsNothing(Client) Then
Client = New TcpClient()
End If
With Client
.Connect("127.0.0.1", 5482)
End With
If Client.Connected = True Then
Dim mData As String = "This is Client"
Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(mData)
NetStream = Client.GetStream()
NetStream.Write(data, 0, data.Length)
If Client.Connected = True Then
NetStream.BeginRead(ByteStream, 0, ByteStream.Length, New AsyncCallback(AddressOf CallBack), NetStream)
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub CallBack(ByVal AsyncResults As IAsyncResult)
NetStream.EndRead(AsyncResults)
Console.WriteLine(System.Text.Encoding.ASCII.GetString(ByteStream))
NetStream.BeginRead(ByteStream, 0, ByteStream.Length, New AsyncCallback(AddressOf CallBack), NetStream)
End Sub
Now I know that I kindly step over little things as makeing sure that the buffer is not accessed outside by another thread etc. You might also take a good look at Invoking the Me.txtMessages.Text to get it on the UI thread. not that this agian depends on how you start the async call.
in any case it's always better to do the
if(InvokeRequired)
Invoke(...
I'm pretty sure this is all you need to get going again.
Cheers, AT