引用WINSOCK.PAS,怎样实现FTP文件的下载?(27191895)(100分)

  • 主题发起人 主题发起人 gsw798
  • 开始时间 开始时间
G

gsw798

Unregistered / Unconfirmed
GUEST, unregistred user!
引用WINSOCK.PAS,怎样实现FTP文件的下载?(27191895)
 
这就要自己写代码了,先了解FTP协议的基本内容,在才可以写代码
大楷用以下函数,(初始化未加)
sock()
connect()
send()
recv()
closeSocket()
 
FTP.Host = HostTxt.Text;
FTP.Port = StrToInt(PortTxt.Text);
FTP.UserID = UserTxt.Text;
FTP.Password = PassTxt.Text;
FTP.Connect();
FTP.Download( ,);
可以参考 NMFTP 中的 Connect 和 Download 方法:

procedure TNMFTP.Connect;
var
Replymess: string;
Handled: boolean;

begin
BeenCanceled := False; {Reset Cancelled flag}
if not Connected then
{If not already connected}
begin
ClearInput;
inherited Connect; {Do the inherited connect}
try
Replymess := ' ';
ReadExtraLines(Replymess);
if ReplyNumber > 400 then
begin
if assigned(OnConnectionFailed) then OnConnectionFailed(self);
raise FTPException.Create(Replymess); {If Error show exception}
end;
// Below this line added by Edward T. Smith 11/18/1998
// FFWUserID is the firewall user ID
if FFWAuth then
begin
Replymess := Transaction(Cont_User + FFWUserID); {Send User Name and check result}
if (ReplyNumber > 400) and (ReplyNumber < 600) then
begin
if assigned(OnConnectionFailed) then OnConnectionFailed(self);
raise FTPException.Create(Replymess); {If Error show exception}
end;
if ReplyNumber = 331 then
{If Password Needed}
begin
StatusMessage(Status_Informational, Cont_Pass); {Show outgoing Message} {Show Outgoing Message}
Replymess := Transaction(Cont_Pass + FFWPassword); {Send Password and check result}
// FFWPassword is the firewall password
if (ReplyNumber > 400) and (ReplyNumber < 600) then
begin
if assigned(OnConnectionFailed) then OnConnectionFailed(self);
raise FTPException.Create(Replymess); {If Error show exception}
end;
end; {_ if ReplyNumber = 331 then _}
end; // FIrewall Authentication

// Above this line added by Edward T. Smith 11/18/1998
if (FUserID = '') or (Password = '') then
if assigned(FOnAuthenticationNeeded) then FOnAuthenticationNeeded(Handled);
if Proxy <> '' then
begin
case FFirewallType of
ftUser: Replymess := Transaction('USER ' + USERID + '@' + Host);
ftOpen: Replymess := Transaction('OPEN ' + Host);
ftSite: Replymess := Transaction('SITE ' + Host);
end;
end;
if (Proxy = '') or (FFirewallType <> ftUser) then
begin
StatusMessage(Status_Informational, Cont_User + UserID); {Show Outgoing message}
Replymess := Transaction(Cont_User + UserID); {Send User Name and check result}
if (ReplyNumber > 400) and (ReplyNumber < 600) then
begin
if assigned(OnConnectionFailed) then OnConnectionFailed(self);
raise FTPException.Create(Replymess); {If Error show exception}
end;
end;
if ReplyNumber = 331 then
{If Password Needed}
begin
StatusMessage(Status_Informational, Cont_Pass); {Show outgoing Message} {Show Outgoing Message}
Replymess := Transaction(Cont_Pass + Password); {Send Password and check result}
if (ReplyNumber > 400) and (ReplyNumber < 600) then
begin
if assigned(OnConnectionFailed) then OnConnectionFailed(self);
raise FTPException.Create(Replymess); {If Error show exception}
end;
end; {_ if ReplyNumber = 331 then _}
if assigned(FOnConnect) then FOnConnect(self);
except {If fault}
if Connected then Disconnect; {Disconnect}
StatusMessage(Status_Informational, sFTP_Msg_Disconnect); {Show Status}
raise; {Show disconnected status}
end; {_ try _}
end; {_ if not Connected then _}
end; {_ procedure TNMFTP.Connect; _}

procedure TNMFTP.Download(RemoteFile, LocalFile: string);
var
Replymess: string;
Success, Handled: Boolean;
Tsck: TSocket;
label CleanUp;
begin
try
Success := False;
BeenCanceled := False; {If there is a cancelled process reset it}
CertifyConnect; {Make sure Connection exists}
if Connected then
begin
EnterCriticalSection(ProcessLock);
if DataAvailable then read(0);
DataSocket := TPowersock.Create(self); {Create a Data socket}
DataSocket.TimeOut := TimeOut;
if assigned(FPacketRecvd) then
DataSocket.OnPacketRecvd := FPacketRecvd; {Set function to handle data socket status}
try
if FPassive then
begin
ReplyMess := Transaction('PASV');
if (ReplyNumber > 499) then
if not assigned(FOnFailure) then
raise FTPException.Create(Replymess)
else
begin
Handled := False;
FOnFailure(Handled, cmdDownLoad);
if not Handled then
raise FTPException.Create(Replymess)
else goto CleanUp;
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then _}
DataSocket.Port := StrToInt(Copy(NthWord(replyMess, ',', 6), 1, Pos(')', NthWord(replyMess, ',', 6)) - 1)) + (256 * StrToInt(NthWord(replyMess, ',', 5)));
DataSocket.Host := Host;
DataSocket.connect;
end
else { _FPassive_ }
begin
DataSocket.Port := 0; {Set Port to Zero}
DataSocket.Listen(True); {Listen in the datasocket}
Replymess := Transaction(Cont_Port + GetLocalAddress + DataSocket.GetPortString); {Send Port for data socket}
if (ReplyNumber > 300) and (ReplyNumber < 600) then
if not assigned(FOnFailure) then
raise FTPException.Create(Replymess)
{Raise exception on errors}
else {_ NOT if not assigned(FOnFailure) then _}
begin
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then
raise FTPException.Create(Replymess)
else goto CleanUp;
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then _}

end { not _FPassive_ };
StatusMessage(Status_Informational, Cont_Retr + RemoteFile); {Show Outgoing Message}
FBytesTotal := 0;
Replymess := inherited Transaction(Cont_Retr + RemoteFile);
if (ReplyNumber > 300) and (ReplyNumber < 600) then
if not assigned(FOnFailure) then raise FTPException.Create(Replymess)
{Raise exception on errors}
else {_ NOT if not assigned(FOnFailure) then raise FTPException.Create(Replymess) _}
begin
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then raise FTPException.Create(Replymess)
else goto CleanUp;
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then raise FTPException.Create(Replymess) _}
FBytesTotal := GetBytesTotal(Replymess);
while (Replymess[1] = ' ') or (Replymess[4] = '-') do
begin
Replymess := readln; {Handle Extra Lines}
StatusMessage(Status_Informational, Replymess); {Show Received Lines}
if FBytesTotal = 0 then FBytesTotal := GetBytesTotal(Replymess);
if (ReplyNumber > 300) and (ReplyNumber < 600) then
begin
if assigned(FOnUnSupportedFunction) and (ReplyNumber >= 500) and (ReplyNumber <= 502) then FOnUnSupportedFunction(cmdDownload);
if not assigned(FOnFailure) then
raise FTPException.Create(Replymess)
{Raise exception on errors}
else {_ NOT if not assigned(FOnFailure) then _}
begin
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then
raise FTPException.Create(Replymess);
{Raise exception on errors}
end; {_ NOT if not assigned(FOnFailure) then _}
end; {_ if (ReplyNumber > 300) and (ReplyNumber < 600) then _}
end; {_ while (Replymess[1] = ' ') or (Replymess[4] = '-') do _}
if not FPassive then
begin
Tsck := DataSocket.handle;
DataSocket.ThisSocket := DataSocket.Accept; {Accept the datasocket}
WinSock.CloseSocket(Tsck);
end;
FBytesTotal := GetBytesTotal(Replymess);
if assigned(FTransactionStart) then FTransactionStart(self);
if not (BeenCanceled or BeenTimedOut) then
if LocalFile = '' then DataSocket.CaptureFile(RemoteFile)
{If no Local filename specified save file same as remote}
else {_ NOT if LocalFile = '' then DataSocket.CaptureFile(RemoteFile) _} DataSocket.CaptureFile(LocalFile); {If Local filename specified save file under it}
if assigned(FTransactionStop) then FTransactionStop(self);
DataSocket.RequestCloseSocket;
FBytesTotal := DataSocket.BytesRecvd;
StatusMessage(Status_Informational, (sFTP_Msg_Recvd + IntToStr(BytesTotal) + sFTP_No_Bytes));
if not (BeenCanceled or BeenTimedOut) then Replymess := readln;
//if ReplyMess='' then ReplyMess:='226 Data Transfer successful';
StatusMessage(Status_Informational, Replymess);
if not (BeenCanceled or BeenTimedOut) then ReadExtraLines(Replymess);
{Read Extra Lines}
{If no Local filename specified save file same as remote}
if assigned(FTransactionStop) then FTransactionStop(self);
Success := True;
CleanUp:
finally
DataSocket.Destroy; { _Destroy datasocket_ }
DataSocket := nil;
if BeenCanceled then
begin
BeenCanceled := False;
ReplyMess := Transaction('ABOR');
end;
if Success then if assigned(FOnSuccess) then FOnSuccess(cmdDownload);
end {_ try _}
end; { _Connected_ }
except
Handled := False;
FOnFailure(Handled, cmdDownload);
if not Handled then
raise;
end;
end;
 
后退
顶部