我正在使用以下代码从特定服务器发送消息
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
send("Hello there")
TextBox1.Text = TextBox1.Text + "Send Successful"
End Sub
Public Sub send(ByVal data As String)
Dim tcpClient As New TcpClient()
Try
tcpClient.Connect("10.xxx.xx.xx", 7000)
TextBox1.text = TextBox1.Text + "Conneecte"
Catch e As Exception
Console.WriteLine(e)
End Try
If tcpClient.Connected Then
Dim networkStream As NetworkStream = tcpClient.GetStream()
Dim streamWriter As New StreamWriter(networkStream)
Console.WriteLine("Messege {0} to {1}", data, tcpClient.Client.RemoteEndPoint)
streamWriter.WriteLine(data)
streamWriter.Flush()
tcpClient.Close()
End If
End Sub
并且发送成功。
下面的代码是从另一个系统读取的。并且代码挂在do while循环中
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim tcpClient As New System.Net.Sockets.TcpClient()
Try
tcpClient.Connect("10.xxx.xx.xx", 7000)
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
'Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("$gimme")
'networkStream.Write(sendBytes, 0, sendBytes.Length)
' 'Read the NetworkStream into a byte buffer.
Do
Loop Until networkStream.DataAvailable
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
Dim returndata As String = Encoding.ASCII.GetString(bytes)
''Label1.Text = returndata
tcpClient.Close()
Else
If Not networkStream.CanRead Then
tcpClient.Close()
Else
If Not networkStream.CanWrite Then
tcpClient.Close()
End If
End If
End If
Catch ex As Exception
'sLabel1.Text = "Exception Caught"
End Try
End Sub
并且代码挂在这里在读取部分
循环直到 networkStream.DataAvailable
我在这里做错了什么?