Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#.net Windows 8应用程序TcpClient代码端口到StreamSocket_C#_Windows_Microsoft Metro_.net 4.5_Stream Socket Client - Fatal编程技术网

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

c#.net Windows 8应用程序TcpClient代码端口到StreamSocket,c#,windows,microsoft-metro,.net-4.5,stream-socket-client,C#,Windows,Microsoft Metro,.net 4.5,Stream Socket Client,我在.NET4.5之前就有这个代码 _Connection = new TcpClient(hostname, port); _Stream = _Connection.GetStream(); 对于Windows 8应用程序,我更改为: _Connection = new StreamSocket(); await _Connection.ConnectAsync(new HostName(hostname), port.ToString()); _DataReader = _Connect

我在.NET4.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'
-这与我预期的响应相去甚远。

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


我所需要做的就是在
\u DataWriter
中刷新流(每次有东西写入流时都需要这样做),然后
\u DataReader
流开始按预期工作。

能否请您提供一个示例,说明如何使用\u DataReader读取数据。据我所知,在不知道预期数据长度的情况下,没有明显的方法可以读取数据。