Artisan
Artisan

Reputation: 4112

Please help how to send images from client to server?

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

Upvotes: 0

Views: 843

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598424

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.

Upvotes: 0

Related Questions