NetStream.Read in C#

调用NetStream.Read(buffer, offset, len)有一个坑,那就是它一次读取并不一定会真正读完你指定的len长度,所以最好这么写:

    for (int alreadyRead = 0; alreadyRead < len; )
        alreadyRead += stream.Read(buffer, alreadyRead, len - alreadyRead);

为此浪费了不少时间·

发表评论

电子邮件地址不会被公开。 必填项已用*标注