从此流中读取时是否需要将偏移量指定为4,或者是否有办法自动推进NetworkStream以使我的偏移量始终为0?
服务器
NetworkStream theStream = theClIEnt.getStream();//...//Calculate file size with fileInfo and put into byte[] size//...theStream.Write(size,size.Length);theStream.Flush();
客户
NetworkStream theStream = theClIEnt.getStream();//read sizebyte[] size = new byte[4];int bytesRead = theStream.Read(size,4);...//read contentbyte[] content = new byte[4096];bytesRead = theStream.Read(content,4096);Console.Writeline(bytesRead); // <-- Prints filesize + 4解决方法 对;找到了; fileInfo.Length很长;你的电话:
binWrite.Write(fileInfo.Length);
写8个字节,little-endian.然后你通过以下方式阅读:
filesize = binRead.ReadInt32();
哪个little-endian会给你相同的值(至少32位).但是,在流中没有使用4 00字节(从长字节的高字节开始) – 因此4字节不匹配.
使用以下之一:
> binWrite.Write((int)fileInfo.Length);> filesize = binRead.ReadInt64();
总结以上是内存溢出为你收集整理的c# – 读取NetworkStream不会前进流全部内容,希望文章能够帮你解决c# – 读取NetworkStream不会前进流所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)