这个是接收端的,发送端每次发送1024字节过来。
buffersize=1024;
byte [] receive=new byte[size];//size为接收文件的大小
while (total < size)
{
read = stream.Read(receive, total, buffersize);
total += read;
}
我的问题是,在最后一次,发送端传来了658个字节,但是接收端的read仍然是读出1024哥字节,导致receive大小不足,引发异常。按道理最后一次的read也是为658才对的呀~为什么的呢?我调试过,发送端最后一次确实只是发送了658个字节
附上发送端
byte [] send =new byte [1024];
while((read = fs.Read(send, 0, send.Length))!=0)
{
stream.Write(send, 0, read);
total += read; }
buffersize=1024;
byte [] receive=new byte[size];//size为接收文件的大小
while (total < size)
{
read = stream.Read(receive, total, buffersize);
total += read;
}
我的问题是,在最后一次,发送端传来了658个字节,但是接收端的read仍然是读出1024哥字节,导致receive大小不足,引发异常。按道理最后一次的read也是为658才对的呀~为什么的呢?我调试过,发送端最后一次确实只是发送了658个字节
附上发送端
byte [] send =new byte [1024];
while((read = fs.Read(send, 0, send.Length))!=0)
{
stream.Write(send, 0, read);
total += read; }
解决方案 »
- 好像把vs2005绑定了vss之后,vs2005就打不开了,怎么办?
- 关于DataTable..rows.count的问题,为何得到的值会变?每一次都会增加?
- 有个数据库脚本文件1.sql,里面有很多条语句组成,如何在c#程序中来执行它
- 怎么判断txt文件中有多少行,并分别把每行的数据读出来?
- 如何获取局域网中拨号计算机的IP
- dataGrid的交替行背景色属性AlternatingBackColor设置后不起作用,是何原因啊?
- Public Report(string strSQL)
- C#里有没有类似Delphi中with的语法?
- 初学者的一个简单问题,在线等
- 怎样编程产生 Nero项目文件(*.nri)(好的程序可以付费)
- GridView.RowCommand的问题。。。。。。。。。。。。。。。。
- 關於Excel取值
read = stream.Read(receive, total, (总长度%1024));
read = stream.Read(receive, total, 总长度%1024);
read = stream.Read(receive, total, 总长度%1024);
while (size > buffersize)
{
read = stream.Read(receive, receive.Length - size, buffersize);
size -= read;
}
read = stream.Read(receive, receive.Length - size, size);没有必要一定要在循环中接收全部 有时候可以简单一点!还有最好不要在循环里添加判断之类 因为每循环一次就需要判断一次 这是有损耗的!