大家帮忙看看:
Dim objClient As New System.Net.Sockets.TcpClient
objClient.Connect("10.10.0.216", 8888)
Dim objStream As System.Net.Sockets.NetworkStream
objStream = objClient.GetStream()
Dim objWriter As System.IO.StreamWriter = New System.IO.StreamWriter(objStream, System.Text.Encoding.Default)
objWriter.Write(TextBox1.Text)
objWriter.Flush()
Dim objReader As System.IO.StreamReader
objReader = New System.IO.StreamReader(objStream, System.Text.Encoding.Default)
Dim strRev As String
Dim i As Integer = 1
While objReader.Peek() > -1
ListBox2.Items.Add(objReader.Peek() & "/" & i)
strRev = objReader.ReadLine().ToString
ListBox2.Items.Add(strRev)
If Trim(strRev) = "</TX_INFO>" Then Exit While
i += 1
End While
ListBox2.Items.Add(objReader.Read())
ListBox2.Items.Add("</TX>")
Me.Refresh()
Me.ListBox2.SetSelected(ListBox2.Items.Count - 1, True)
objReader.Close()
objStream.Close()
objClient.Close()
其中的 While objReader.Peek() > -1
ListBox2.Items.Add(objReader.Peek() & "/" & i)
strRev = objReader.ReadLine().ToString
ListBox2.Items.Add(strRev)
If Trim(strRev) = "</TX_INFO>" Then Exit While
i += 1
End While
在数据流读取到一半时会退出(objReader.Peek() =-1),而用While objReader.Read() > -1则可以全部读出来,但Read会移动位置,读出来的每行数据流前面会少了2个字节。这是为什么啊?在线等,谢谢了!