Socket 客户端大于2个接收阻塞
由于公司需要开发一个Ftp断点续传 上传、下载的Active工具,现上传已做好,就是多线程批量下载的时候,只要下载文件多过两个且批量下载,创建的Socket就会接收数据阻塞。功能开发的是一个线程下载一个文件,写了一个线程类,里面包含了下载方法用的委托实现的,麻烦各位大侠瞧瞧的。下面是部分代码:
/// <summary>
/// 处理过程
/// </summary>
private void Procedure()
{
downloadcustomEventArgs.FileGuid = FileGuid;
if (IS_RunOver == 1)
{
FileStream output = null;
Socket socketData = null;
bool IsException = false;
long FileLength = 0;//文件大小
int iBytes = 0;
long currBytes = 0;//已下载
try
{
if (!bConnected)
{
Connect();
}
SetTransferType(TransferType.Binary);
//获取Ftp文件大小
FileLength = GetFileSize(_strRemoteFileName);
if (_strLocalFileName == null || _strLocalFileName.Equals(""))
{
_strLocalFileName = _strRemoteFileName;
}
if (!File.Exists(_strFolder + "\\" + _strLocalFileName))
{
Stream st = File.Create(_strFolder + "\\" + _strLocalFileName);
st.Close();
}
output = new FileStream(_strFolder + "\\" + _strLocalFileName, FileMode.Open);
//建立进行数据连接的socket
socketData = CreateDataSocket();
long offset = 0;
if (_DownLoadMode)
{
offset = output.Length;
if (offset > 0)
{
SendCommand("REST " + offset);
if (iReplyCode != 350)
{
offset = 0;
}
}
if (offset > 0)
{
long npos = output.Seek(offset, SeekOrigin.Begin);
currBytes += offset;
}
}
SendCommand("RETR " + strRemoteFileName);
if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226 || iReplyCode == 250))
{
throw new IOException(strReply.Substring(4));
}
int Progress = 0;//下载进度
socketData.Blocking = true;
while (true)
{
Application.DoEvents();
if (backgroundWorker.CancellationPending)
{
downloadcustomEventArgs.Status = "取消";
IsException = true;
break;
}
#region 当需要暂停操作时,阻止后台线程继续执行
if (isSuspend)
{
downloadcustomEventArgs.Status = "暂停";
onProgress(downloadcustomEventArgs);//下载进度
onSetText(downloadcustomEventArgs);
}
while (isSuspend)
{
Application.DoEvents();
if (backgroundWorker.CancellationPending)
{
downloadcustomEventArgs.Status = "取消";
IsException = true;
break;
}
System.Threading.Thread.Sleep(100);
}
if (IsException)
{
break;
}
#endregion
if (_Astrict > 0 && _Speed >= _Astrict)
{
ExecuteWrite = false;
}
if (ExecuteWrite)
{
if (socketData.Available > 0)
{
//通过Socket从服务器读取字节流
iBytes = socketData.Receive(buffer, buffer.Length, SocketFlags.None);
if (iBytes <= 0)
{
break;
}
//写入文件流
output.Write(buffer, 0, iBytes);
currBytes += iBytes;
_Speed += iBytes;
//下载数据
downloadcustomEventArgs.Status = "开始";
downloadcustomEventArgs.FileLength = FileLength;
//已下载
downloadcustomEventArgs.AlreadyDownloaded = currBytes;
Progress = Convert.ToInt32(Math.Round(double.Parse(currBytes.ToString()) / FileLength, 2) * 100);
}
}
if (ExecuteFun || Progress == 100)
{
downloadcustomEventArgs.Progress = Progress;
if (_Speed >= 1048576)
{
downloadcustomEventArgs.Speed = Math.Round((double.Parse(_Speed.ToString()) / 1048576), 2) + "MB";
}
else if (_Speed >= 1024)
{
downloadcustomEventArgs.Speed = Math.Round((double.Parse(_Speed.ToString()) / 1024), 2) + "KB";
}
else
{
downloadcustomEventArgs.Speed = Math.Round(double.Parse(_Speed.ToString()), 2) + "B";
}
downloadcustomEventArgs.SurplusTime = "00:00:00";
if (_Speed > 0)
{
//剩余秒数
long Seconds = (long)Math.Round(double.Parse((downloadcustomEventArgs.FileLength - downloadcustomEventArgs.AlreadyDownloaded).ToString()) / _Speed, 0);
if (Seconds > 0)
{
long hour = 0, minute = 0, second = 0;
hour = Seconds / 3600;
minute = (Seconds % 3600) / 60;
second = (Seconds % 3600) % 60;
downloadcustomEventArgs.SurplusTime = (hour >= 10 ? hour.ToString() : "0" + hour.ToString()) + ":" + (minute >= 10 ? minute.ToString() : "0" + minute.ToString()) + ":" + (second >= 10 ? second.ToString() : "0" + second.ToString());
}
}
_Speed = 0;
ExecuteFun = false;
ExecuteWrite = true;
if (Progress == 100)
{
timer.Stop();
timer.Dispose();
}
//执行事件
//backgroundWorker.ReportProgress(Progress);
onProgress(downloadcustomEventArgs);
onSetText(downloadcustomEventArgs);
}
}
if (!IsException)//正常结束
{
downloadcustomEventArgs.Status = "完成";
}
}
catch
{
IsException = true;
downloadcustomEventArgs.Status = "异常!";
}
finally
{
//关闭Scoket请求流
if (socketData != null)
{
socketData.Close();
}
//关闭文件输出流
if (output != null)
{
output.Close();
}
if (!IsException && !(iReplyCode == 226 || iReplyCode == 250))
{
ReadReply();
if (!(iReplyCode == 226 || iReplyCode == 250))
{
throw new IOException(strReply.Substring(4));
}
DisConnect();//关闭Ftp连接
}
else
{
//关闭控制连接的Socket
if (socketControl != null)
{
socketControl.Close();
}
}
}
onProgress(downloadcustomEventArgs);//下载进度
onSetText(downloadcustomEventArgs);
}
} Socket Tcp
补充:.NET技术 , C#