Stream.Read 没有 return 收到数据
Stream.Read doesn't return received data
这是我喜欢使用的方法。我相信,这段代码没有什么新东西。
public static byte[] ReadFully(Stream stream, int initialLength)
{
// If we've been passed an unhelpful initial length, just
// use 1K.
if (initialLength < 1)
{
initialLength = 1024;
}
byte[] buffer = new byte[initialLength];
int read = 0;
int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;
// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
// End of stream? If so, we're done
if (nextByte == -1)
{
return buffer;
}
// Nope. Resize the buffer, put in the byte we've just
// read, and continue
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
// Buffer is now too big. Shrink it.
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
我的目标是读取从 TCP 客户端发送的数据,例如box{"id":1,"aid":1}
这是一个在我的应用程序中用类似 Jason 的文本解释的命令。
而且这段文字不一定每次都是一样的大小。
下次可以有run{"id":1,"aid":1,"opt":1}
.
这一行调用的方法;
var serializedMessageBytes = ReadFully(_receiveMemoryStream, 1024);
Please click to see; Received data in receiveMemoryStream
虽然我们可以看到流中的数据,
在 ReadFully
方法中,"chunck" 总是 return 0
和方法 returns {byte[0]}
.
非常感谢任何帮助。
在 Watch window 中查看您的流,流的位置 (19) 位于数据末尾,因此没有任何内容可读。这可能是因为您刚刚将数据写入流并且随后没有重置位置。
如果您乐于始终从流的开头读取,或检查填充流的代码,请在函数的开头添加 stream.Position = 0;
或 stream.Seek(0, System.IO.SeekOrigin.Begin);
语句。请注意,尽管某些流实现不支持搜索。
这是我喜欢使用的方法。我相信,这段代码没有什么新东西。
public static byte[] ReadFully(Stream stream, int initialLength)
{
// If we've been passed an unhelpful initial length, just
// use 1K.
if (initialLength < 1)
{
initialLength = 1024;
}
byte[] buffer = new byte[initialLength];
int read = 0;
int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;
// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
// End of stream? If so, we're done
if (nextByte == -1)
{
return buffer;
}
// Nope. Resize the buffer, put in the byte we've just
// read, and continue
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
// Buffer is now too big. Shrink it.
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
我的目标是读取从 TCP 客户端发送的数据,例如box{"id":1,"aid":1}
这是一个在我的应用程序中用类似 Jason 的文本解释的命令。
而且这段文字不一定每次都是一样的大小。
下次可以有run{"id":1,"aid":1,"opt":1}
.
这一行调用的方法;
var serializedMessageBytes = ReadFully(_receiveMemoryStream, 1024);
Please click to see; Received data in receiveMemoryStream
虽然我们可以看到流中的数据,
在 ReadFully
方法中,"chunck" 总是 return 0
和方法 returns {byte[0]}
.
非常感谢任何帮助。
在 Watch window 中查看您的流,流的位置 (19) 位于数据末尾,因此没有任何内容可读。这可能是因为您刚刚将数据写入流并且随后没有重置位置。
如果您乐于始终从流的开头读取,或检查填充流的代码,请在函数的开头添加 stream.Position = 0;
或 stream.Seek(0, System.IO.SeekOrigin.Begin);
语句。请注意,尽管某些流实现不支持搜索。