C# System.IO.Stream.Read被卡住了
我使用System.IO.Stream.Read与服务器端建立了HTTP连接,以读取HTTP请求正文消息。 问题是,每隔几分钟服务器就会被Read语句卡住一次,直到套接字超时或客户端关闭连接后才会继续C# System.IO.Stream.Read被卡住了,c#,.net,sockets,tcp,stream,C#,.net,Sockets,Tcp,Stream,我使用System.IO.Stream.Read与服务器端建立了HTTP连接,以读取HTTP请求正文消息。 问题是,每隔几分钟服务器就会被Read语句卡住一次,直到套接字超时或客户端关闭连接后才会继续 int bytesRead = 0; while (bytesRead < contentLength) { int got = stream.Stream.Read(buffer.Buffer, bytesRead, contentLength - bytesRead); byt
int bytesRead = 0;
while (bytesRead < contentLength)
{
int got = stream.Stream.Read(buffer.Buffer, bytesRead, contentLength - bytesRead);
bytesRead += got;
}
int字节读取=0;
while(字节读取<内容长度)
{
int get=stream.stream.Read(buffer.buffer,bytesRead,contentLength-bytesRead);
bytesRead+=got;
}
为什么它会被卡住而不继续读取数据?我想知道流是否报告提前终止;您还应该查看
Read
是否返回了一个非正数,即
while (bytesRead < contentLength)
{
int got = stream.Stream.Read(
buffer.Buffer, bytesRead, contentLength - bytesRead);
if(got <= 0) throw new EndOfStreamException(string.Format(
"Expected {0} bytes; {1} bytes received", contentLength, bytesRead));
bytesRead += got;
}
while(bytesRead if(get我想知道流是否报告提前终止;您还应该查看Read
是否返回了一个非正数,即
while (bytesRead < contentLength)
{
int got = stream.Stream.Read(
buffer.Buffer, bytesRead, contentLength - bytesRead);
if(got <= 0) throw new EndOfStreamException(string.Format(
"Expected {0} bytes; {1} bytes received", contentLength, bytesRead));
bytesRead += got;
}
while(bytesRead 如果(get尝试此实现,则计数(contentLength-bytesRead)错误。它应该是缓冲区大小
byte[] buffer = new byte[bufferSize];
int count;
while ((count = stream.Stream.Read(buffer, 0, buffer.Length)) != 0)
{
// do something with the buffer using count as the end marker
destination.Write(buffer, 0, count);
}
如果您只想从流中获得一个字节数组,这更像您正在尝试的:
byte[] buffer = stream.Stream.ToArray()
或复制到另一个缓冲区:
byte[] data = stream.Stream.ToArray();
Array.CopyTo(data , buffer.Buffer, data.Length)
尝试此实现,您的计数(contentLength-bytesRead)错误。它应该是缓冲区大小
byte[] buffer = new byte[bufferSize];
int count;
while ((count = stream.Stream.Read(buffer, 0, buffer.Length)) != 0)
{
// do something with the buffer using count as the end marker
destination.Write(buffer, 0, count);
}
如果您只想从流中获得一个字节数组,这更像您正在尝试的:
byte[] buffer = stream.Stream.ToArray()
或复制到另一个缓冲区:
byte[] data = stream.Stream.ToArray();
Array.CopyTo(data , buffer.Buffer, data.Length)
我只在没有刷新流的客户端上遇到过类似的错误。System.IO.Stream.Read上的MSDN doc说:“如果没有可用的数据,实现将一直阻塞,直到至少可以读取一个字节的数据。”。因此,由于某些原因,没有可用的数据。我认为您可以设置某个ReadTimeout
,并在相当短的时间后停止等待更多数据
此处还发布了一个相关问题:。也许他的解决方案也可以帮助您。我只在没有刷新流的客户端上遇到过类似错误。System.IO.Stream.Read上的MSDN doc说:“如果没有可用数据,实现将阻塞,直到至少可以读取一个字节的数据。”。因此,由于某些原因,没有可用的数据。我认为您可以设置某个ReadTimeout
,并在相当短的时间后停止等待更多数据
此处还发布了一个相关问题:。也许他的解决方案也可以帮助您。这似乎与我使用的Read方法(即上面代码中看到的调用)和通过将NetworkStream对象传递给其构造函数创建的Read方法有关
NetworkStream.Read用于读取http请求消息正文,而StreamReader.Read用于读取请求的其余部分(startine、headers…)。
虽然调用是由同一线程同步进行的,但这可能是我所经历的行为的原因
将代码更改为仅与套接字一起工作并直接从套接字执行读取时,解决了问题。这似乎与我正在使用这两种方法的读取方法有关(这是上面代码中看到的调用)以及通过将NetworkStream对象传递给其构造函数而创建的
NetworkStream.Read用于读取http请求消息正文,而StreamReader.Read用于读取请求的其余部分(startine、headers…)。
虽然调用是由同一线程同步进行的,但这可能是我所经历的行为的原因
当将代码更改为仅与套接字一起工作并直接从套接字执行读取时,它已修复了问题。可能是服务器会话时间限制。您在HttpWebRequest类上使用System.io.Stream的任何原因?似乎有点不方便?@kmcc049关键字有“body”-通过Stream
读/写正文是很常见的;最常见的是HttpRequest.InputStream
,或者如果您正在创建一个请求,HttpWebRequest.GetRequestStream()
@Marc ah ok没有注意到正文部分,认为他是在手动使用(var remoteReader=new StreamReader(remoteResponse.GetResponseStream()))result=remoteReader.ReadToEnd();
读取正文似乎更方便……可能是服务器会话时间限制。您在HttpWebRequest类上使用System.io.Stream有什么原因吗?似乎有点不方便?@kmcc049关键字是“body”-通过Stream
读/写正文是很常见的;最常见的是HttpRequest.InputStream
,或者如果您正在创建一个请求,HttpWebRequest.GetRequestStream()
@Marc ah ok没有注意到正文部分,认为他是在手动使用(var remoteReader=newstreamreader(remoteResponse.GetResponseStream()))result=remoteReader.ReadToEnd();
读正文似乎更方便……我不同意;原稿更正确;问题中的代码是填充了一个大小的缓冲区(而不是使用暂存缓冲区在流之间复制,如图所示)