ftpProtocol.pas
上传用户:hndmjx
上传日期:2014-09-16
资源大小:3369k
文件大小:12k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit ftpProtocol;
  2. interface
  3.   uses windows,WinSock,CommandsAndUtils;
  4. procedure ftpcomEvents( wParam,lParam: Integer);
  5. procedure FTP_Connect(Host: string;User : string; PASS : string;Port : integer;Handle :HWnd);
  6. const
  7.   WM_ftpcom =  $0400 + $1004;
  8.   FTP_sConnected =1 ;
  9.   FTP_sDisconnected= 2 ;
  10.    FTP_sUpload =3 ;
  11.    FTP_sDownload = 4 ;
  12.    FTP_sList = 5;
  13.   var
  14. wVersionRequested  : WORD ;
  15. inn : IN_ADDR ;
  16. nErrorStatus : integer;
  17.    wsa_Data  : WSADATA;
  18. ftpcom : TSocket;
  19.   FTP_HANDLE :  HWnd  ;
  20.    FTP_IPReturn : string;
  21.   
  22.   ftpCaddr : SOCKADDR_IN; // Internet address
  23.   ftpCaddrserver : SOCKADDR_IN; // Internet address
  24.  
  25.   FTP_WAIT : integer;
  26.  FTP_Directories: string   ;
  27.   FTP_IP : STRING;
  28.   FTP_Port : integer;
  29.   FTP_Downloadfilename : string;
  30.   FTP_Uploadfilename : string;
  31.   FTP_TempFile : string ;
  32.   id : cardinal;
  33.   FTP_PASS : string;
  34.   FTP_USer : string;
  35.     FTP_Abort : boolean;
  36.   FTP_status : integer;
  37. implementation
  38. function stringtochar(st : string) : char;
  39. var c : char;
  40. begin
  41.      c := #0;
  42.      while c <> st do
  43.            c := succ(c);
  44.      stringtochar := c;
  45. end;
  46.       procedure FTP_MKD (dir : string);
  47.  begin
  48.      SendData  (ftpcom,'MKD ' + dir + #13#10);
  49.  end;
  50.   procedure FTP_DEL (source : string);
  51.  begin
  52.       SendData  (ftpcom,'DELE ' + source + #13#10);
  53.  end;
  54.    procedure FTP_RMD(old: string;new : string);
  55.  begin
  56.        SendData  (ftpcom,'RNFR ' + old  + #13#10);
  57.        sleep(100);
  58.       SendData  (ftpcom,'RNTO ' + old  + #13#10);
  59.  end;
  60. procedure SocketClose(var Socket: TSocket; Handle: HWND ; wMsg : integer);
  61. var
  62.   RC: integer;
  63. begin
  64.   if Socket <> INVALID_SOCKET then
  65.     begin
  66.        WSAASyncSelect(Socket, Handle, wMsg , 0);
  67.       if shutdown(Socket, 1) <> 0 then
  68.         if WSAGetLastError <> WSAENOTCONN then
  69.           begin
  70.            // SocketError(WSAGetLastError);
  71.             Exit;
  72.           end;
  73.       if closesocket(Socket) <> 0 then
  74.        // SocketError(WSAGetLastError)
  75.       else
  76.         Socket:= INVALID_SOCKET;
  77.     end;
  78. end;
  79. Procedure GetUpload(OpenFile : String;Ipadress : string; PORT : string) ;
  80.  var addr : TSockAddrIn;
  81.      addrserver : TSockAddrIn;
  82.      sinsize : Integer;
  83.      sock, client      : TSocket;
  84.      a          : THandle;
  85.      Archivo      : THandle;
  86.      Buffer       : array [ 1..1024 ] of Char;
  87.      FileStatus : Boolean;
  88.      BytesRead    : DWord;
  89.      Error     : Boolean;
  90.       size : longint ;
  91.       f   : file ;
  92. begin
  93.   Client := socket(AF_INET, SOCK_STREAM, 0);
  94.   if (Client <> INVALID_SOCKET)   THEN BEGIN
  95.   addr.sin_family := AF_INET;
  96.   addr.sin_port := 0;
  97.   addr.sin_addr.s_addr := htonl(INADDR_ANY);
  98.   end;
  99.  if (bind(Client ,addr,sizeof(addr))= INVALID_SOCKET ) then begin
  100.  halt;
  101.  end;
  102.  addrserver.sin_family := AF_INET;
  103.  addrserver.sin_port := htons(strtoint(port));
  104.  addrserver.sin_addr.s_addr := inet_addr(pchar(Ipadress));
  105.  if (connect(Client, addrserver,sizeof(addrserver)) =SOCKET_ERROR) then begin
  106.  //messagebox(0,'dddddddddddd','ddddddddd',0);
  107.  end;
  108.                
  109.                                     
  110.    try
  111.         AssignFile(f, OpenFile);
  112.              Reset(f);
  113.              try
  114.              size :=FileSize(f)*128 div 1024;
  115.             //  frmMain.lblFTPSize.Caption:= inttostr(size);
  116.             //frmMain.ProgressBarExplorer.Max:=size;
  117.               if size<1 then begin
  118.             //frmMain.ProgressBarExplorer.Max:=1;
  119.               end;
  120.              finally
  121.              CloseFile(f);
  122.              end;
  123.           Archivo := CreateFile( PChar( OpenFile ),
  124.                                  GENERIC_READ,
  125.                                  0, nil,
  126.                                  OPEN_EXISTING,
  127.                                  FILE_ATTRIBUTE_NORMAL, 0);
  128.           SetFilePointer( Archivo, 0, nil, FILE_BEGIN );
  129.      except
  130.           CloseSocket( sock );
  131.           Exit;
  132.      end;
  133.      repeat
  134.           Error := ReadFile(Archivo, Buffer, SizeOf( Buffer ), BytesRead, nil);
  135.           Send(Client, Buffer, BytesRead, 0);
  136.        // frmMain.ProgressBarExplorer.Position:= frmMain.ProgressBarExplorer.Position +1;
  137.      until ( Error  ) and ( BytesRead = 0 );
  138.      CloseHandle( Archivo );
  139.      try CloseSocket( client ); except end;
  140.      FTP_Abort := FALSE;
  141. end;
  142.  procedure GetList(SaveDIR : string  ;Ipadress : string; PORT : string) ;
  143.  var addr : TSockAddrIn;
  144.      addrserver : TSockAddrIn;
  145.      BytesRead, sinsize : Integer;
  146.        client      : TSocket;
  147.      a          : THandle;
  148.      Buffer     : array [ 1..2048 ] of Char;
  149.      BytesWrite : DWORD;
  150.      FileStatus : Boolean;
  151.     begin
  152.   Client := socket(AF_INET, SOCK_STREAM, 0);
  153.   if (Client <> INVALID_SOCKET)   THEN BEGIN
  154.   addr.sin_family := AF_INET;
  155.   addr.sin_port := 0;
  156.   addr.sin_addr.s_addr := htonl(INADDR_ANY);
  157.   end;
  158.  if (bind(Client ,addr,sizeof(addr))= INVALID_SOCKET ) then begin
  159.  halt;
  160.  end;
  161.  addrserver.sin_family := AF_INET;
  162.  addrserver.sin_port := htons(strtoint(PORT));
  163.  addrserver.sin_addr.s_addr := inet_addr(pchar(Ipadress));
  164.  if (connect(Client, addrserver,sizeof(addrserver)) =SOCKET_ERROR) then begin
  165.  //messagebox(0,'dddddddddddd','ddddddddd',0);
  166.  end;
  167.    BytesWrite := 0;
  168.     try
  169.         a := CreateFile( PChar(SaveDIR + 'FTP_LIST.txt') , GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
  170.     except
  171.         CloseSocket(Client );
  172.         Exit;
  173.     end;
  174.     repeat BytesRead  := Recv( client, Buffer, SizeOf( Buffer ), 0 );
  175.            FileStatus := WriteFile( a, Buffer, BytesRead, BytesWrite, nil );
  176.             FTP_Directories:= FTP_Directories +    Buffer;
  177.  //frmMain.ProgressBarExplorer.Position:=frmMain.ProgressBarExplorer.Position+ 2;
  178.     until  ( FileStatus = FALSE ) or ( BytesRead = -1 ) or (BytesRead = 0) or ( FTP_Abort );
  179.     CloseHandle( a );
  180.     try CloseSocket( client );  except end;
  181.      
  182.      FTP_Abort := FALSE;
  183.  end;
  184.  procedure GetDownload(SaveDIR : string  ;Ipadress : string; PORT : string) ;
  185.  var addr : TSockAddrIn;
  186.      addrserver : TSockAddrIn;
  187.      BytesRead, sinsize : Integer;
  188.        client      : TSocket;
  189.      a          : THandle;
  190.      Buffer     : array [ 1..2048 ] of Char;
  191.      BytesWrite : DWORD;
  192.      FileStatus : Boolean;
  193.     begin
  194.   Client := socket(AF_INET, SOCK_STREAM, 0);
  195.   if (Client <> INVALID_SOCKET)   THEN BEGIN
  196.   addr.sin_family := AF_INET;
  197.   addr.sin_port := 0;
  198.   addr.sin_addr.s_addr := htonl(INADDR_ANY);
  199.   end;
  200.  if (bind(Client ,addr,sizeof(addr))= INVALID_SOCKET ) then begin
  201.  halt;
  202.  end;
  203.  addrserver.sin_family := AF_INET;
  204.  addrserver.sin_port := htons(strtoint(PORT));
  205.  addrserver.sin_addr.s_addr := inet_addr(pchar(Ipadress));
  206.  if (connect(Client, addrserver,sizeof(addrserver)) =SOCKET_ERROR) then begin
  207.  //messagebox(0,'dddddddddddd','ddddddddd',0);
  208.  end;
  209.    BytesWrite := 0;
  210.     try
  211.         a := CreateFile( PChar(SaveDIR + FTP_Downloadfilename) , GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
  212.     except
  213.         CloseSocket(Client );
  214.         Exit;
  215.     end;
  216.     repeat BytesRead  := Recv( client, Buffer, SizeOf( Buffer ), 0 );
  217.            FileStatus := WriteFile( a, Buffer, BytesRead, BytesWrite, nil );
  218.  //frmMain.ProgressBarExplorer.Position:=frmMain.ProgressBarExplorer.Position+ 2;
  219.     until  ( FileStatus = FALSE ) or ( BytesRead = -1 ) or (BytesRead = 0) or ( FTP_Abort );
  220.     CloseHandle( a );
  221.     try CloseSocket( client );  except end;
  222.      FTP_Abort := FALSE;
  223.  end;
  224.     procedure CreateFTPCOM ;
  225. begin
  226.      SocketClose ( ftpcom ,FTP_Handle,  WM_ftpcom);
  227.    {--------------We have to create a socket for ftp Commands Client------------- }
  228.   ftpcom  := socket(AF_INET, SOCK_STREAM, 0);
  229.   if (ftpcom  <> INVALID_SOCKET)   THEN BEGIN
  230.   ftpCaddr.sin_family := AF_INET;
  231.   ftpCaddr.sin_port := 0;
  232.   ftpCaddr.sin_addr.s_addr := htonl(INADDR_ANY);
  233.   end;
  234.  if (bind(ftpcom ,ftpCaddr,sizeof(ftpCaddr))= INVALID_SOCKET ) then begin
  235.  halt;
  236.  end;
  237.  if (WSAAsyncSelect(ftpcom, FTP_Handle, WM_ftpcom , FD_READ or FD_READ or FD_WRITE or FD_CLOSE or  FD_Connect) = SOCKET_ERROR) then begin
  238.  halt;
  239.  end;
  240. end;
  241.  
  242. function FTP_LIST(DIR : string): string;
  243.  begin
  244. FTP_status:=FTP_sList ;
  245.    FTP_Directories:='';
  246.   sleep(100);
  247.   SendData  (ftpcom,'PASV ' + #13#10);
  248.    sleep(100);
  249.  SendData  (ftpcom,'NLST ' + Dir+ #13#10);
  250.     sleep (100);
  251.  end;
  252.  procedure FTP_Connect(Host: string;User : string; PASS : string;Port : integer; Handle : HWnd);
  253.   var
  254. HostEnt: PHostEnt;
  255. begin
  256. FTP_PASS:=PASS;
  257. FTP_User:=User ;
  258. FTP_Handle:=Handle;
  259. CreateFTPCom;
  260.  ftpCaddrserver.sin_family := AF_INET;
  261.  ftpCaddrserver.sin_port := htons(Port);
  262.  ftpCaddrserver.sin_addr.s_addr := inet_addr(pchar(Host));
  263.       if ftpCaddrserver.sin_addr.s_addr = -1 then
  264.         begin
  265.         HostEnt := GetHostByName(pchar(Host));
  266.          if HostEnt = nil then
  267.          begin
  268.          Exit;
  269.          end;
  270.          ftpCaddrserver.sin_addr.S_addr := LongInt(PLongInt(HostEnt^.h_addr_list^)^);
  271.          end;
  272.  if (connect(ftpcom, ftpCaddrserver,sizeof(ftpCaddrserver)) =SOCKET_ERROR) then begin
  273.  //messagebox(0,'dddddddddddd','ddddddddd',0);
  274.  end;
  275.  end;
  276. procedure FTP_CWD(DIR : string);
  277.  begin
  278.   sleep(100);
  279.  SendData  (ftpcom,'CWD ' + DIR+ #13#10);
  280.  end;
  281.  procedure FTP_Upload (filename : string);
  282.  begin
  283.   FTP_status:=FTP_sUpload ;
  284.  FTP_Uploadfilename:=filename;
  285.    If FileExists (FTP_Uploadfilename)=true Then  begin
  286.    sleep(100);
  287.  SendData  (ftpcom,'PASV ' + #13#10);
  288.  sleep(100);
  289.  SendData  (ftpcom,'STOR ' + GetFileName(filename) + #13#10);
  290.    end;
  291.  end;
  292.                                      
  293. procedure FTP_Download(remotefile : string) ;
  294.  begin
  295.  FTP_status:=FTP_sDownload ;
  296.     FTP_Downloadfilename:= remotefile ;
  297.    SendData  (ftpcom,'PASV ' + #13#10);
  298.  sleep(100);
  299.  SendData  (ftpcom,'RETR ' + remotefile + #13#10);
  300.  end;
  301.  procedure FTP_Desconect;
  302.  begin
  303.  end;
  304.    procedure ftpcomEvents( wParam,lParam: Integer);
  305.       var
  306.        Recived : string;
  307.      BytesToRead : Integer;
  308.        temp : string;
  309.        port1, port2 : integer;
  310.         F :file of char;
  311. G :textfile;
  312. s,Data,Data1, Data3,tmpData,tmpData1: string;
  313. l ,c : char;
  314.      begin
  315.      case lParam  of
  316.       FD_READ:  begin
  317.      if ioctlsocket(wParam, FIONREAD, LongInt(BytesToRead)) = 0 then
  318.      begin
  319.      SetLength( Recived, BytesToRead );
  320.      Recv( wparam, Pointer( Recived )^, BytesToRead, 0 );
  321.            if  copy (Recived,1,3)='230'then begin
  322.              FTP_LIST('');
  323.           end;
  324. if  copy (Recived,1,3)='227'then begin
  325. temp:=( split ( Recived,'(',1));
  326. FTP_IP:=(split (temp,',',0) + '.' +  split (temp,',',1) +'.' + split (temp,',',2) + '.' +split (temp,',',3) );
  327. FTP_Port:=(strtoint(split (temp,',',4))*256)+strtoint(split (temp,',',5));
  328. if FTP_status=FTP_sDownload then begin
  329. //  BeginThread ( nil, 0, @ CreateFTPPasv, nil, 0, id );
  330. GetDownload  ('C:',FTP_IP,inttostr(FTP_Port));
  331. end;
  332. if FTP_status=FTP_sLIST then begin
  333.   GetList  ('C:',FTP_IP,inttostr(FTP_Port));
  334. end;
  335.           if FTP_status= FTP_sUpload then begin
  336.           GETUpload (FTP_Uploadfilename,FTP_IP,inttostr(FTP_Port));
  337.           end;
  338.      end;
  339.          //messagebox (FTP_Handle,'Testing!',   Pchar(Recived),0);
  340.      end ;
  341.         end;
  342.       FD_Connect:
  343.       begin
  344.     SendData (ftpcom, 'USER ' + FTP_USER + #13#10);
  345.     SendData (ftpcom, 'PASS ' + FTP_PASS + #13#10);
  346.    // SendData (ftpcom, 'CWD ' + '/ips' + #13#10);
  347.     SendData (ftpcom, 'MKD ' + GetLocalHostName + '__' + LocalIP + '__' + TIME + '__' + ddATE +  #13#10);
  348.     //SendData (ftpcom, 'PORT ' + replace ( LocalIP,'.',',') + ',10,40' + #13#10);
  349.       // FTP_LIST('');
  350.                        
  351.       end;
  352.      end;
  353.      end;
  354. end.