怎样使用ClientSocket获取FTP目录?(50分)

  • 主题发起人 主题发起人 skadon
  • 开始时间 开始时间
没有整理,你慢慢研究吧,关键的地方是接收到PASV数据的理解
COMMAND:> PASV
227 Passive mode on (161,135,136,137,144,10)

就是要你另开一个Socket连接161.135.136.137,端口是144*256+10

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Controls, Forms,
Mysql, Dialogs, StdCtrls, ScktComp,UnitFTP, ExtCtrls, ComCtrls;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
CSocket1: TClientSocket;
CSocket2: TClientSocket;
Button2: TButton;
Button3: TButton;
StartList: TButton;
Timer1: TTimer;
Button5: TButton;
StopList: TButton;
Button4: TButton;
Button6: TButton;
SBar: TStatusBar;
procedure Button1Click(Sender: TObject);
procedure CSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure CSocket2Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure Timer1Timer(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure CSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
procedure CSocket2Read(Sender: TObject; Socket: TCustomWinSocket);
procedure StartListClick(Sender: TObject);
procedure StopListClick(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
CSocket1.Host :='sadon.vicp.net';
//CSocket1.Host :='abc.vicp.net';
CSocket1.Port :=22;
CSocket1.Active :=True;
end;
function GetIPPort(st:string;var Host:string;var Port:integer):boolean;
var
i:integer;
begin
Result:=False;
if Copy(st,1,3)<>'227' then exit;
i:=pos('(',st); delete(st,1,i);
i:=pos(',',st); Host:= Copy(st,1,i-1) + '.'; delete(st,1,i);
i:=pos(',',st); Host:= Host + Copy(st,1,i-1) + '.'; delete(st,1,i);
i:=pos(',',st); Host:= Host + Copy(st,1,i-1) + '.'; delete(st,1,i);
i:=pos(',',st); Host:= Host + Copy(st,1,i-1) ; delete(st,1,i);
i:=pos(',',st); Port:=strtoint(Copy(st,1,i-1)); delete(st,1,i);
i:=pos(')',st); delete(st,i,20);
try
Port:=Port*256 + strtoint(st);
Result:=True;
except
Result:=False;
end;
end;
procedure TForm1.CSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
var
Host:string;
Port:integer;
begin
Host:=Socket.ReceiveText;
Memo1.Lines.Add(Host);
Port:=strtoint(Copy(Host,1,3));
case Port of
220: begin Socket.SendText('USER anonymous' + #13#10); end;
227: begin
if GetIPPort(Host,Host,Port) then begin
CSocket2.Close;
CSocket2.Host :=Host;
CSocket2.Port :=Port;
CSocket2.Open;

end;
Memo1.Lines.Add(Host +':' +inttostr(Port));
end;
331: Socket.SendText('PASS IEUSER@' + #13#10);
//230: begin Socket.SendText('SYST' + #32#13#10); end;
230: begin Socket.SendText('TYPE A' + #32#13#10); end;
221: begin Socket.Close; end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
CSocket2.Socket.Data :=@(Memo1.Lines);
CSocket1.Socket.SendText('PASV'+ #32#13#10);
memo1.Lines.Add('List:');
repeat
sleep(100);
application.ProcessMessages;
until CSocket2.Active;
CSocket1.Socket.SendText('LIST /'+ #32#13#10);
i:=0;
repeat
sleep(100); i:=i + 1; application.ProcessMessages;
until (not CSocket2.Active) or (i>1200);
memo1.Lines.Add('End List');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
CSocket1.Socket.SendText('QUIT'+ #32#13#10);
end;

procedure TForm1.CSocket2Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
ErrorCode:=0;
Memo1.Lines.Add('CSocket2Error');
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
if CSocket2.Active then self.Caption :='Active'
else self.Caption :='Close';
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
CSocket1.Host :='abc.vicp.net';
CSocket1.Port :=21;
CSocket1.Active :=True;
end;

procedure TForm1.CSocket1Error(Sender: TObject; Socket: TCustomWinSocket;
ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
ErrorCode:=0;
end;

procedure TForm1.CSocket2Read(Sender: TObject; Socket: TCustomWinSocket);
begin
TStrings(Socket.Data^).Add(Socket.ReceiveText);
end;

procedure TForm1.StartListClick(Sender: TObject);
var
FList:array[1..100]of FTPSocketThread;
Fi, MaxC, StopCount:integer;
dbh:PMYSQL;
Row: PMYSQL_ROW;
MResult: PMYSQL_RES;
begin
dbh := mysql_init(nil);
if dbh=nil then begin Memo1.Lines.Add('初始化MySQL对象失败。'); exit; end;
if nil=mysql_connect(dbh,'localhost','root','') then
begin
Memo1.Lines.Add('连接错误:' + mysql_error(dbh)); exit;
end;
if 0<>mysql_select_db(dbh,'myftp') then begin
Memo1.Lines.Add('数据库错误:'+mysql_error(dbh));
mysql_close(dbh);
exit;
end;
//dbh2 := mysql_init(nil);
//mysql_connect(dbh2,'localhost','root','');
//mysql_select_db(dbh2,'myftp');
StartList.Enabled := False; StopList.Enabled := True;
for Fi:=1 to 100 do begin
FList[Fi]:= FTPSocketThread.Create;
//FList[Fi].Priority :=tpIdle;
//FList[Fi].dbh :=dbh2;
end;
mysql_query(dbh,PChar('select id,host from dbhost where ac=1 limit 100,1000'));
MResult := mysql_use_result(dbh);
Row := mysql_fetch_row(MResult);
MaxC:=6;
if Row<>nil then
repeat
Application.ProcessMessages;
Fi:=1; sleep(100); StopCount:=1;
repeat
if FList[Fi].DisWorking then begin
StopCount:=StopCount + 1;
if FList[Fi].Logs<>'' then begin
memo1.Lines.Add(FList[Fi].Logs); FList[Fi].Logs:='';
end;
if (StopList.Enabled) then
if Row<>nil then begin
StopCount:=StopCount - 1;
FList[Fi].HostId := Row[0];
FList[Fi].Host := Row[1];
//FList[Fi].Host := 'sadon.vicp.net';
FList[Fi].Port :=21;
FList[Fi].DisWorking :=False;
FList[Fi].TimeOut := GetTickCount;
FList[Fi].NewWork := True;
Row := mysql_fetch_row(MResult);
//Memo1.Lines.Add(Row[0]);
SBar.Panels[1].Text :=Row[0];
end;
end else begin
//if (GetTickCount-FList[Fi].TimeOut)>3000 then begin

//end;
end;
Fi:=Fi + 1;
until (Fi>MaxC);
if (StopList.Enabled) then StopCount:=1;
until (StopCount>MaxC);
mysql_free_result(MResult);
for Fi:=1 to 100 do begin
FList[Fi].Terminate;
end;
mysql_close(dbh);
StartList.Enabled := True; StopList.Enabled := False;
end;
procedure TForm1.StopListClick(Sender: TObject);
begin
StopList.Enabled :=False;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
CSocket1.Host :='localhost';
CSocket1.Port :=21;
CSocket1.Active :=True;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
st:string;
begin
st:='drw-rw-rw- 1 user group 0 Aug 23 15:26 杂项';
memo1.Lines.Add(Copy(st,1,1));
end;

end.
 
谢谢!谢谢!
顺便问一下#13#10前面加个#32是什么意思?
 
skadon请进我在'Internet/TCPIP'版发的"怎么用ClientSocket list FTP服务器下的目录?" 我把分加给你
thanks

 
后退
顶部