【发布时间】:2009-07-08 18:59:23
【问题描述】:
我在这里很密集吗? StreamReader.ReadLine 声明:
行定义为一系列字符后跟换行符 ("\n")、回车符 ("\r") 或回车符后紧跟换行符 ("\r\n" )
那么,为什么这没有按预期工作?
' Server
Dim tcpL as New TcpListener(IPAddress.Any, 5000)
tcpL.Start(1)
Using tcpC as TcpClient = tcpL.AcceptTcpClient(), _
s as NetworkStream = tcpC.GetStream(), _
sr as New StreamReader(s, System.Text.Encoding.ASCII, True)
Dim message As String = sr.ReadLine()
Console.WriteLine(message)
End Using
Console.ReadLine()
' Client
Using tcpC as New TcpClient()
tcpC.Connect(IPAddress.Loopback, 5000)
Using s as NetworkStream = tcpC.GetStream(), _
sw as New StreamWriter(s, System.Text.Encoding.ASCII)
sw.AutoFlush = True
sw.Write("Hello there!")
sw.Write(vbCR) ' Note the CR terminator
Console.ReadLine()
End Using
End Using
在断开连接之前,服务器不会从ReadLine 返回 - 即使发送了 CR。如果我更改为vbLF 或vbCRLF,它将按预期返回。
是文档有误,还是我在这里搞砸了……?
【问题讨论】: