.net Windows 8应用程序TcpClient代码端口到StreamSocket



我在。net 4.5之前有这个代码

_Connection = new TcpClient(hostname, port);
_Stream = _Connection.GetStream();

对于Windows 8应用程序,我更改为:

_Connection = new StreamSocket();
await _Connection.ConnectAsync(new HostName(hostname), port.ToString());
_DataReader = _Connection.InputStream.AsStreamForRead();
_DataWriter = _Connection.OutputStream.AsStreamForWrite();

我认为这是最简单的解决方案,因为我不必在其他任何地方更改任何底层代码,因为我仍然使用Stream来读取/写入数据。

这段代码没有像预期的那样工作,虽然我成功地在流上写了一些东西,但是当要读取流的时候,我一直得到'n' -这与我预期的响应相去甚远。

找到解决方案了。希望有人觉得有用。

我所需要做的就是冲洗_DataWriter中的流(这需要在每次写入流时完成),然后_DataReader流开始按预期工作。

最新更新