/// <summary> /// 向用户发送下载的文件 /// </summary> /// <param name="sSocket"></param> /// <param name="Parameter"></param> /// <param name="si"></param> public void ProcessRetreiveCommand(Socket sSocket, string Parameter, SessionInfo si) { try { if (File.Exists(si.szFtpRoot + @"\" + Parameter)) { //文件 using (FileStream pStream = new FileStream(si.szFtpRoot + @"\" + Parameter, FileMode.Open)) { if (si.fBinary) { //Reply(sSocket, 150, "Opening BINARY mode data connection for '" + Parameter + "'"); using (BinaryReader br = new BinaryReader(pStream)) { Socket sBinarySocket; if (si.fPassive) { sBinarySocket = si.pi.tcpListener.AcceptSocket(); } else { sBinarySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sBinarySocket.Bind(localEP); sBinarySocket.Connect(remoteEP); } while (br.BaseStream.Position < br.BaseStream.Length) { sBinarySocket.Send(br.ReadBytes(1024 * 1024 * 5)); } sBinarySocket.Close(); } } else { //Reply(sSocket, 150, "Opening ASCII mode data connection for '" + Parameter + "'"); using (StreamReader sr = new StreamReader(pStream)) { if (si.fPassive) { SendOverPassiveDataConnection(GetBytes(sr.ReadToEnd()), si); } else { SendOverDataConnection(GetBytes(sr.ReadToEnd())); } } } Reply(sSocket, 200, "Transfer Complete"); } } }
为什么 sBinarySocket.Close();有时不被执行,直接跳出了。
br.ReadBytes(1024 * 1024 * 5)是读多大的内容吧,sBinarySocket.Close()你自己去调试下吧