Newsgroups : Borland : borland.public.delphi.internet.winsock : 2005 Feb : Download problem using TIdCmdTCPServer & TIdCmdTCPClient

www.cryer.info
Managed Newsgroup Archive

Download problem using TIdCmdTCPServer & TIdCmdTCPClient

Subject:Download problem using TIdCmdTCPServer & TIdCmdTCPClient
Posted by:"yc" (ema..@test.com)
Date:Sat, 5 Feb 2005 18:17:42

Hi,
I hope I get an answer.
I have been trying on this subject nearly a week and could get any
breakthrough.
I have a client and a server side, all I try to do is to send a file name to
the server and download it from client.
If you tell me something is wrong with code segment.
Delphi7 and Indy10

Thanks in advance
yc

Client side
----------
var
  AStream : TIdStreamVCL;
  mem    : TMemoryStream;
  s : string;
  aa : int64;


  client.Connect;  //a TIdCmdTCPClient instance
  client.Socket.WriteLn('a'); //User id
  client.Socket.WriteLn('a'); //Password
  client.Socket.WriteLn('LOAD:FILE.EXE'); //Download f'ile from server
  aa := strtoint64(client.Socket.Readln());
  mem := TMemoryStream.Create();
  AStream := TIdStreamVCL.Create(mem);
  try
    client.Socket.ReadStream(AStream, aa, True);
    mem.SaveToFile(ExtractFilePath(Application.ExeName)+'a.aaa');
  finally
    AStream.Free;
    mem.Free;
  end;


Server side
-----------
procedure TfrmMain.IdCmdTCPServer1Connect(AContext: TIdContext);
var
  user, pass : string;
begin
  with AContext.Connection.Socket do
  begin
    user := ReadLn;
    if user = '' then
    begin
      AContext.Connection.Disconnect;
      Exit;
    end;
    pass := ReadLn;
    if (user = 'a') and (pass = 'a') then Exit;
    AContext.Connection.Disconnect;
  end;
end;

procedure TfrmMain.IdCmdTCPServer1Execute(AContext: TIdContext);
var
  cmd: string;
  fn : string;
  str : string;
  i: Integer;
  strm : TIdStreamVCL;
  mem  : TMemoryStream;
begin
    str := AnsiUpperCase(AContext.Connection.Socket.ReadLn); //LOAD:x.txt
    i := Pos(':', str);
    if i <= 0 then
    begin
      if str = 'BYE' then
      begin
        AContext.Connection.Disconnect;
        Exit;
      end;
    end;
    cmd := Copy(str, 1, i-1); //LOAD
    fn  := Copy(str, i+1, 255);

    if cmd = 'LOAD' then
    begin
      with AContext.Connection.Socket do
      begin
        mem  := TMemoryStream.Create();
        mem.LoadFromFile('c:\'+fn);
        strm := TIdStreamVCL.Create(mem);
        try
//          WriteBufferOpen(-1);
          WriteLn(IntToStr(strm.Size));
          Write(strm, strm.Size, True);
//          WriteBufferClose;
        finally
          FreeAndNil(strm);
          mem.Free;
        end;
      end;
      AContext.Connection.Disconnect;
    end;

Replies:

www.cryer.info
Managed Newsgroup Archive