是否有可能 MyTcpClient.GetStream().Read() 仅返回 < 128 字节
是的。您不能假设您对 Read() 的调用将返回 128 个字节。
请参阅文档:
The total number of bytes read into
the buffer. This can be less than the
number of bytes requested if that many
bytes are not currently available, or
zero (0) if the end of the stream has
been reached.
See this link on how to properly read from streams
Try something like this instead: (pass in a 128 length byte array)
private static void ReadWholeArray (Stream stream, byte[] data)
{
int offset=0;
int remaining = data.Length;
while (remaining > 0)
{
int read = stream.Read(data, offset, remaining);
if (read <= 0)
throw new EndOfStreamException
(String.Format("End of stream reached with {0} bytes left to read", remaining));
remaining -= read;
offset += read;
}
}