public DownListInfo CreatDownLoad(LoadInfo d) { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint endpoit = new IPEndPoint(IPAddress.Any, 6000);//////////实际运行port要设置为0 Port = endpoit.Port; sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); //SocketOptionName.ReuseAddress是关键 sock.Bind(endpoit); sock.Listen(Channel); lls = new SocketModule(d); lls.m = new ModuleFalt(); lls.PointInfo = new Information(); dli = new DownListInfo(d); d.s = sock; int workerThreads, availabeThreads; ThreadPool.GetAvailableThreads(out workerThreads, out availabeThreads); WaitCallback wcb = new WaitCallback(CreatBeginAccept); if (workerThreads > 0)//可用线程数>0 { ThreadPool.QueueUserWorkItem(wcb, d); } dli.filename = RootDirectory + lls.name; dli.Progress_Percentage = lls.Progress_Percentage; return(dli); }
public DownListInfo ContinuousDownLoad(Information infor) { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint endpoit = new IPEndPoint(IPAddress.Any, 6000); sock.Bind(endpoit); sock.Listen(Channel); int workerThreads, availabeThreads; ThreadPool.GetAvailableThreads(out workerThreads, out availabeThreads); WaitCallback wcb = new WaitCallback(p => { while (true) { AllDone.Reset(); if (Stop_DownLoad) { break; } sock.BeginAccept(new AsyncCallback(AcceptCallback_Continuous), sock); AllDone.WaitOne(); } } ); if (workerThreads > 0)//可用线程数>0 { ThreadPool.QueueUserWorkItem(wcb, null); } else { //to do 可以采取一种策略,让这个任务合理地分配给线程 } dli = new DownListInfo(); lls = new SocketModule(infor); dli.name = lls.name; dli.type = lls.type; dli.Size = lls.t_size; dli.filename = lls.filename; dli.Progress_Percentage = lls.Progress_Percentage; return(dli); }