gpt4 book ai didi

Delphi Mobile App 通过 Tidtcpclient 发送图片(连接正常关闭)

转载 作者:行者123 更新时间:2023-12-03 16:13:11 25 4
gpt4 key购买 nike

我正在使用 Delphi 10.2 并构建一个移动应用程序
我的任务之一是将图像从移动设备发送到服务器
我从 stackoverflow 获得了一个代码来执行此操作并运行它,但它不起作用

服务器端代码执行

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;

和客户代码是
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;

现在在服务器端出现错误 连接正常关闭

任何帮助请

最佳答案

这是正常行为。
TIdTCPServer是一个多线程组件。当客户端连接时,它在自己的工作线程中运行。 TIdTCPServer.OnExecute事件在该线程的上下文中以连续循环的方式在套接字连接的生命周期内触发。

客户端发送图像流后,与服务器断开连接。

服务器读取图像流后,退出OnExecute事件处理程序。处理程序将再次被触发,调用 ReadLn() ,它将检测断开连接并引发 EIdConnClosedGracefully异常(exception)。

这是正常行为。只需让异常逃脱事件处理程序。不要抓到它(或者,如果你抓到了,一定要重新加注)。 TIdTCPServer将为您处理异常,关闭客户端套接字并停止管理套接字的线程。

仅供引用,附带说明,FMX TBitmap.SaveTo...() 的默认图像格式方法是PNG,而不是BMP。

关于Delphi Mobile App 通过 Tidtcpclient 发送图片(连接正常关闭),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46178568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com