0

This is my server's coding

procedure TForm1.IdTCPServerExecute(AThread: TIdPeerThread);
var
  InputString: string;
  ACommand: string[1];
  AFileName: string;
  ATempFileName: string;
  AFileStream: TFileStream;
begin
  InputString := UpperCase(AThread.Connection.ReadLn);
  ACommand := Copy(InputString, 1, 1);
  AFileName := FPicFilePath + Copy(InputString, 2, 5) + '.jpg';

  if ACommand = 'R' then begin
    AFileStream := TFileStream.Create(AFileName, fmOpenRead + fmShareDenyNone);

    try
      AThread.Connection.WriteStream(AFileStream, true, true);
    finally
      AFileStream.Free;
    end;
  end else if ACommand = 'S' then begin
    ATempFileName := FPicFilePath + 'TEMP.jpg';

    if FileExists(ATempFileName) then
      DeleteFile(ATempFileName);

    AFileStream := TFileStream.Create(ATempFileName, fmCreate);

    try
      AThread.Connection.ReadStream(AFileStream, -1, false);
      //RenameFile(ATempFileName, AFileName);
    finally
      AFileStream.Free;
    end;
  end;

  AThread.Connection.Disconnect;
end;

And this is my client's coding

procedure TForm1.SendImageToServer(ASendCmd: string);
var
  AFileStream: TFileStream;
begin
  MessageDlg('Sending ' + ASendCmd + ' :' + FSendFileName, mtInformation, [mbOK], 0);
  Screen.Cursor := crHourGlass;

  with IdTCPClient do begin
    if Connected then Disconnect;
    Host := '127.0.0.1';
    Port := 2108;

    AFileStream := TFileStream.Create(FSendFileName, fmOpenRead);

    try
      try
        Connect;
        try
          WriteLn(ASendCmd);
          WriteStream(AFileStream, true, false);
        finally
          Disconnect;
        end;
      finally
        AFileStream.Free;
      end;
    except
    end;
  end;

  Screen.Cursor := crDefault;
end;

I can successfully get images from server, but when I had to send a new image back to server, I just had an empty TEMP.jpg.

Please help. Thanks.

Delphi 5, Indy 9

Arioch 'The
  • 15,799
  • 35
  • 62
Artisan
  • 4,042
  • 13
  • 37
  • 61
  • i suspect you should send file length in advance. Please quote your indy9 help file about `AThread.Connection.ReadStream` – Arioch 'The Jun 28 '13 at 13:54
  • I think that only the client should invoke the `Disconnect` method. Why use `Disconnect` both server and client side? – AndreaBoc Jun 28 '13 at 16:42
  • @Arioch'The: the OP is setting the `AWriteByteCount` parameter of WriteStream() to true on the server side, so it is sending the file length in advance, but he is not doing the same thing on the client side, though the server is expecting it. – Remy Lebeau Jun 28 '13 at 20:51
  • @RemyLebeau So it is the case of "read help to me" issues ? pity there is no Indy9 help online – Arioch 'The Jun 29 '13 at 14:33

1 Answers1

0

When sending a file from the client to the server, the client is not telling WriteStream() to send the stream size, but the server is telling ReadStream() to expect the stream size to arrive, so you have a mismatch.

When sending a file from the server to the client, the server is telling WriteStream() to send the stream size, and the client is telling ReadStream() to expect the stream size to arrive, so there is no mismatch.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770