- 我使用 TIdTCPServer 和 TIdTCPClient,客户端和服务器都在同一个本地网络上,速度为 100 mbps
- 需要发送最多 2 MB 的文件
- 我对 Indy 和 Socket 编程没有经验
*客户端向服务器发送请求后,循环直到与服务器断开连接,文件由客户端自己创建,但大小为0字节
IdTCPServer.OnExecute ...
begin
CS.Enter;
try
InputString := AThread.Connection.ReadLn;
if InputString = 'PIC' then begin
AFullFileName := FFilePath + 'PIC01.jpg';
if FileExists(AFullFileName) then begin
AFileStream := TFileStream.Create(AFullFileName, fmOpenRead + fmShareDenyNone);
AThread.Connection.OpenWriteBuffer;
AThread.Connection.WriteStream(AFileStream);
AThread.Connection.CloseWriteBuffer;
FreeAndNil(AFileStream);
end;
AThread.Connection.Disconnect;
end;
except
on E : Exception do
ShowMessage(E.Message);
end;
CS.Leave;
end;
Client.OnButtonClick ...
begin
with IdTCPClient do begin
if Connected then Disconnect;
Host := '127.0.0.1';
Port := 2018;
Connect;
WriteLn('PIC');
//different folder with server's folder
if FileExists(FFilePath + 'PIC01.jpg') then
DeleteFile(FFilePath + 'PIC01.jpg');
AFileStream := TFileStream.Create(FFilePath + 'PIC01.jpg', fmCreate);
while Connected do
ReadStream(AFileStream, -1, true);
FreeAndNil(AFileStream);
Disconnect;
end;
end;
谢谢大家。