Home Delphi Mobile App Send Image Through Tidtcpclient (Connection Closed Gracefully)
Reply: 1

Delphi Mobile App Send Image Through Tidtcpclient (Connection Closed Gracefully)

aoemam
1#
aoemam Published in 2017-09-12 14:05:05Z

I am using Delphi 10.2 and build a mobile app one of my tasks to send images from mobile to a server I get a code from stackoverflow to do this and run it but it is not working

server side code on execute

type
  TSendRec = record
    SONo: string;
    Text: string;
    Bitmap: TBitmap;
  end;


procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
var
  MIRec: TSendRec;
  ms: TMemoryStream;
begin
  try
  MIRec.SONo := AContext.Connection.IOHandler.ReadLn;
  MIRec.Text := AContext.Connection.IOHandler.ReadLn;
  MIRec.Bitmap := TBitmap.Create;

    ms := TMemoryStream.Create;
    try
      AContext.Connection.IOHandler.LargeStream := True;
      AContext.Connection.IOHandler.ReadStream(ms, -1, False);
      ms.Position := 0;
      MIRec.Bitmap.LoadFromStream(ms);
    finally
      ms.Free;
    end;
    TThread.Synchronize(nil,
      procedure
      begin
        Memo1.Lines.Add(MIRec.SONo);
        Memo1.Lines.Add(MIRec.Text);
        MIRec.Bitmap.SaveToFile('C:\To Receive\test.bmp');
      end
      );
  finally
    MIRec.Bitmap.Free;
  end;
end;

and Client code is

procedure TForm2.Button2Click(Sender: TObject);
var
  MIRec: TSendRec;
  ms: TMemoryStream;
begin
  MIRec.SONo := IntToStr(Ticket_ID);
  MIRec.Text := 'Pic_'+IntToStr(Ticket_ID);
  MIRec.Bitmap := TBitmap.Create;
  MIRec.Bitmap.Assign(imgPhotoLibraryImage.Bitmap);
  try
    IdTCPClient1.Connect;
    try
      IdTCPClient1.IOHandler.WriteLn(MIRec.SONo);
      IdTCPClient1.IOHandler.WriteLn(MIRec.Text);
      ms := TMemoryStream.Create;
      try
        MIRec.Bitmap.SaveToStream(ms);
        IdTCPClient1.IOHandler.LargeStream := True;
        IdTCPClient1.IOHandler.Write(ms, 0, True);
        ShowMessage('Image Uploaded');
        imgPhotoLibraryImage.Bitmap.Assign(nil);
      finally
        ms.Free;
      end;
    finally
      IdTCPClient1.Disconnect;
    end;
  finally
    MIRec.Bitmap.Free;
  end;
end;

now in the server side I get an error Connection closed gracefully

any help plz

Remy Lebeau
2#
Remy Lebeau Reply to 2017-09-12 15:38:48Z

This is normal behavior.

TIdTCPServer is a multi-threaded component. When a client connects, it runs in its own worker thread. The TIdTCPServer.OnExecute event is fired in the context of that thread in a continuous loop for the lifetime of the socket connection.

After the client sends the image stream, it disconnects from the server.

After the server reads the image stream, it exits the OnExecute event handler. The handler will be fired again, calling ReadLn(), which will detect the disconnect and raise the EIdConnClosedGracefully exception.

This is normal behavior. Just let the exception escape the event handler. Don't catch it (or, if you do, be sure to re-raise it). TIdTCPServer will handle the exception for you, closing the client socket and stopping the thread that manages the socket.


FYI, on a side note, the default image format of FMX's TBitmap.SaveTo...() methods is PNG, not BMP.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.310814 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO