日期:2014-05-17 浏览次数:21132 次
private void Send()
{
FileStream fs = new FileStream(Path.Combine(_rootfilepath, filename), FileMode.Open);
byte[] readByte = new byte[fs.Length];
fs.Read(readByte, 0, (int)fs.Length);
long SendNumbers = fs.Length / _buffersize;//_buffersize我设的为64K
long temp = fs.Length % _buffersize;
if (temp > 0)
{
SendNumbers += 1;//计算指定每个包大小的情况下需要发送几次
}
long leftbuffersize = fs.Length;//每次发送完一个数据包后,剩余待发送的字节数
for (int i = 0; i < SendNumbers; i++)
{
byte[] SendBytes = null;
if (leftbuffersize <= _buffersize)//是最后一个包
{
SendBytes = new byte[leftbuffersize];
Array.Copy(readByte, i * _buffersize, SendBytes, 0, leftbuffersize);
}
else//不是最后一个包
{
SendBytes = new byte[_buffersize];
Array.Copy(readByte, i * _buffersize, SendBytes, 0, _buffersize);
}
leftbuffersize = fs.Length - (i + 1) * _buffersize;//记录当前剩下的字节数
clientsocket.BeginSend(SendBytes, 0, SendBytes.Length, SocketFlags.None, new AsyncCallback(SendCallBack), clientsocket);
Thread.Sleep(10);
}
fs.Close();
}
byte [] _recvDataBuffer = new byte[64K的缓冲区];
private void RecvCallBack(IAsyncResult ar)//BeginReceive的回调函数
{
//略部分代码
FileInfo file = new FileInfo(待保存的文件路径+文件名);
using (FileStream fs = file.Open(file.Exists ? FileMode.Append : FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
if (fs.Length < clientList[id].FileLen)//clientList是我保存的客户端队列,FileLen是这个文