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


i using delphi 10.2 , build mobile app 1 of tasks send images mobile server code stackoverflow , run 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);           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       );       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);               ms.free;       end;           idtcpclient1.disconnect;     end;       mirec.bitmap.free;   end; end; 

now in server side error connection closed gracefully

any plz

this normal behavior.

tidtcpserver multi-threaded component. when client connects, runs in own worker thread. tidtcpserver.onexecute event fired in context of thread in continuous loop lifetime of socket connection.

after client sends image stream, disconnects server.

after server reads image stream, exits onexecute event handler. handler fired again, calling readln(), detect disconnect , raise eidconnclosedgracefully exception.

this normal behavior. let exception escape event handler. don't catch (or, if do, sure re-raise it). tidtcpserver handle exception you, closing client socket , stopping thread manages socket.


fyi, on side note, default image format of fmx's tbitmap.saveto...() methods png, not bmp.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -