here is the procedure i'm using to download a file from a remote host.
the procedure downloads fine, except that it doesn't disconnect. after
reading all the data it hangs. i can't figure out how to fix this.

procedure DownloadFile(strHost, strLocalFileName: string;
 ClientSocket: TIdTcpClient);
var
  intReturnCode: Integer;
  s: string;
  szBuffer: array[0..256] of Char;
  FileOut: TFileStream;
begin

  FileOut := TFileStream.Create(strLocalFileName, fmCreate);
  try
    with ClientSocket do
    begin
      Host := strHost;
      ReadTimeout := 500;
      Port := 731;

      try
        Connect(500);
        {send query}
        s := 'screenshot' + #13#10;
        intReturnCode := Socket.Send(Pointer(s)^, Length(s));
        if intReturnCode > 0 then
        begin
          {receive the answer}
          { iterate until no more data }
          while (intReturnCode > 0) do
          begin
            { clear buffer before each iteration }
            FillChar(szBuffer, SizeOf(szBuffer), 0);

            { try to receive some data }
            intReturnCode := Socket.Recv(szBuffer, SizeOf(szBuffer));

            { if received a some data, then add this data to the result
string }
            if intReturnCode > 0 then
              FileOut.Write(szBuffer, intReturnCode);
            //if FileOut.Size = 1228945 then
              //DisconnectSocket;
          end
        end
        else
          MessageDlg('No answer from server', mtError, [mbOk], 0);

        DisconnectSocket;
      except
        MessageDlg('No connection', mtError, [mbOk], 0);
      end;
    end;
  finally
    FileOut.Free
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  nsock: TIdTcpClient;
begin
  nsock := TIdTCPClient.Create(nil);
  DownloadFile('192.168.0.58', 'onlyatest.dds', nsock);
  nsock.Free;
end;


-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/delphi-en/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to