打印本文 打印本文 关闭窗口 关闭窗口
TCP/IP (五)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2726  更新时间:2009/4/23 18:43:56  文章录入:mintao  责任编辑:mintao
nbsp; raise EProtocolError.Create(''''FTP'''',f_status_txt,f_status_nr)
        else begin
          self.response;
          f_busy:=false;
          end;
        end;
      end;
    end;
  end;
(*@\\\0000000301*)
(*@/// procedure t_ftp.noop;                                 // NOOP command *)
procedure t_ftp.noop;
begin
  if f_busy then  raise(EProtocolBusy.create);
  if not f_logged_in then login;
  self.SendCommand(''''NOOP'''');
  self.response;
  end;
(*@\\\0000000501*)
(*@/// procedure t_ftp.changedir(const f_dir:string);        // CWD command *)
procedure t_ftp.changedir(const f_dir:string);
begin
  if f_busy then  raise(EProtocolBusy.create);
  if not f_logged_in then login;
  self.SendCommand(''''CWD ''''+f_dir);
  self.response;
  end;
(*@\\\*)
(*@/// procedure t_ftp.removefile(const filename:string);    // DELE command *)
procedure t_ftp.removefile(const filename:string);
begin
  if f_busy then  raise(EProtocolBusy.create);
  if not f_logged_in then login;
  self.SendCommand(''''DELE ''''+filename);
  self.response;
  end;
(*@\\\*)
(*@/// procedure t_ftp.removedir(const dirname:string);      // RMD command *)
procedure t_ftp.removedir(const dirname:string);
begin
  if f_busy then  raise(EProtocolBusy.create);
  if not f_logged_in then login;
  self.SendCommand(''''RMD ''''+dirname);
  self.response;
  end;
(*@\\\*)
(*@/// procedure t_ftp.makedir(const dirname:string);        // MKD command *)
procedure t_ftp.makedir(const dirname:string);
begin
  if f_busy then  raise(EProtocolBusy.create);
  if not f_logged_in then login;
  self.SendCommand(''''MKD ''''+dirname);
  self.response;
  end;
(*@\\\*)
(*@/// procedure t_ftp.renamefile(const prior,after:string); // RNFR and RNTO commands *)
procedure t_ftp.renamefile(const prior,after:string);
begin
  if f_busy then  raise(EProtocolBusy.create);
  if not f_logged_in then login;
  self.SendCommand(''''RNFR ''''+prior);
  self.response;
  self.SendCommand(''''RNTO ''''+after);
  self.response;
  end;
(*@\\\*)

(*@/// function t_ftp.do_write:boolean; *)
function t_ftp.do_write:boolean;
var
  ok:integer;
begin
  result:=false;
  if f_socket=invalid_socket then EXIT;

  ok:=f_stream.read(f_buffer^,buf_size);
  if ok>0 then
    write_buf(f_socket,f_buffer^,ok);
  result:=ok>0;
  end;
(*@\\\0000000501*)
(*@/// function t_ftp.do_read:boolean; *)
function t_ftp.do_read:boolean;
var
  ok,ok2:integer;
  h:integer;
  p: pointer;
begin
  result:=false;
  if f_socket=invalid_socket then EXIT;
  read_var(f_socket,f_buffer^,buf_size,ok);
  p:=f_buffer;
  h:=ok;
  while ok>0 do begin   (* just to be sure everything goes into the stream *)
    ok2:=0;             (* Delphi 2 shut up! *)
    case f_mode_intern of
      tftp_download: ok2:=f_stream.write(p^,ok);
      tftp_getdir:   ok2:=f_dir_stream.write(p^,ok);
      end;
    dec(ok,ok2);
    p:=pointer(longint(p)+ok2);
    end;
  result:=h>0;
  if assigned(f_ondata_got) then
    f_ondata_got(self,f_mode_intern,h);
  end;
(*@\\\0000000901*)

(*@/// procedure t_ftp.finish_upload; *)
procedure t_ftp.finish_upload;
begin
  while do_write do ;
  f_eof:=false;
  shutdown(f_socket,1);
  closesocket(f_socket);
  f_async:=true;
  self.response;
  f_async:=false;
  if assigned(f_onaction) then
    f_onaction(self,f_mode_intern);
  f_busy:=false;
  end;
(*@\\\0000000901*)
(*@/// procedure t_ftp.finish_download; *)
procedure t_ftp.finish_download;
begin
  while do_read do ;
  f_eof:=false;
  shutdown(f_socket,1);
  closesocket(f_socket);
  f_stream.seek(0,0);  (* set the stream back to start *)
  if assigned(f_onaction) then
    f_onaction(self,f_mode_intern);
  f_busy:=false;
  end;
(*@\\\0000000701*)
(*@/// procedure t_ftp.finish_getdir; *)
procedure t_ftp.finish_getdir;
begin
  f_eof:=false;
  while do_read do ;
  f_eof:=false;
  shutdown(f_socket,1);
  closesocket(f_socket);
  self.SendCommand(''''TYPE I'''');  (* always use binary *)
  self.response;
  f_dir_stream.seek(0,0);  (* set the stream back to start *)
  f_cur_dir.clear;
  f_cur_dir.LoadFromStream(f_dir_stream);
  f_dir_stream.clear;
  f_cur_dir_index:=0;
  if assigned(f_onaction) then
    f_onaction(self,f_mode_intern);
  f_busy:=false;
  end;
(*@\\\0000000901*)

(*@/// procedure t_ftp.get_datasocket; *)
procedure t_ftp.get_datasocket;
var
  po: integer;
  ip: longint;
  s,t: string;
  temp_socket: TSocket;
  SockInfo:TSockAddr;
  f_data_socket_number: smallint;
begin
  f_socket:=INVALID_SOCKET;
(*@///   if self.passive then  ask for the port and open the socket active *)
if self.passive then begin
  self.SendCommand(''''PASV'''');
  self.response;
  if f_status_nr<>227 then
    raise EProtocolError.Create(''''FTP'''',f_status_txt,f_status_nr)
  else begin
    s:=copy(f_status_txt,pos(''''('''',f_status_txt)+1,length(f_status_txt));
    s:=copy(s,1,pos('''')'''',s)-1);

    po:=posn('''','''',s,4);
    t:=copy(s,1,po-1);
    while pos('''','''',t)<>0 do
      t[pos('''','''',t)]:=''''.'''';

(*@///     ip_address:=Winsock.Inet_Addr(PChar(t));  { try a xxx.xxx.xxx.xx } *)
(*$ifdef ver80 *)
  t:=t+#0;
  ip_address:=Winsock.Inet_Addr(PChar(@t[1]));  (* try a xxx.xxx.xxx.xx first *)
(*$else *)
 (*$ifopt h- *)
  t:=t+#0;
  ip_address:=Winsock.Inet_Addr(PChar(@t[1]));  (* try a xxx.xxx.xxx.xx first *)
 (*$else *)
  ip_address:=Winsock.Inet_Addr(PChar(t));  (* try a xxx.xxx.xxx.xx first *)
 (*$endif *)
(*$endif *)
(*@\\\0000000801*)
    s:=copy(s,po+1,length(s));
    try
      f_data_socket_number:=strtoint(copy(s,1,pos('''','''',s)-1))*256
                           +strtoint(copy(s,pos('''','''',s)+1,length(s)));
      f_socket:=self.create_socket;
      if f_async_data then
        winsock.WSAAsyncSelect(f_socket,f_handle,uwm_socketevent+1,
          fd_connect or fd_read or fd_write or fd_accept);
      self.connect_socket(f_socket, f_data_socket_number, ip_address);
    except
      f_socket:=INVALID_SOCKET;
      end;
    end;
  end
(*@\\\0000000F01*)
(*@///   else                  send the port and open the socket passive *)
else begin
  ip:=my_ip_address;
  self.SendCommand(''''PORT ''''+inttostr(ip and $000000ff       )+'''',''''+
                            inttostr(ip and $0000ff00 shr 8)+'''',''''+
                            inttostr(ip and $00ff0000 shr 16)+'''',''''+
                            inttostr(ip and $ff000000 shr 24)+'''',''''+
                            inttostr(f_port and $ff00 shr 8   )+'''',''''+
                            inttostr(f_port and $00ff         ));
  self.response;
&n

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口