如何使用一个 TCP 服务器从两个不同的位置发送文件?

How to send files from two different location with one TCP-Server?

如何仅通过一个 TCP 服务器从两个不同的位置发送文件,我设法仅从一个位置发送文件。 这是从一个目录发送的代码...

procedure TForm1.Timer1Timer(Sender: TObject);
var
  fs: TFileStream;
  fn: string;
  sr: TSearchRec;
  I: integer;
begin
  I := 0;
  if FindFirst('C:/*.jpg', faAnyFile, sr) = 0 then
  begin
    with StringGrid1 do
    begin
      ListBox1.Items.Add('C:/' + sr.Name);
      while FindNext(sr) = 0 do
      begin
        ListBox1.Items.Add('C:/' + sr.Name);
        Inc(I);
        if I = 7 then
          Break;
      end;
      FindClose(sr);
      idTCPClient1.Connect;
      for fn in ListBox1.Items do
      begin
        fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyWrite);
        try
          idTCPClient1.IOHandler.WriteLn(ExtractFileName(fn));
          idTCPClient1.IOHandler.Write(fs, 0, True);
          idUDPClient1.Send(lbLatitude.Text + ',' + lbLongitude.Text);
        Finally
          fs.Free;
        end;
      end;
    end;
  end;
end;

您所要做的(无需像 DavidH 建议的那样完全重写您的代码)只需用您想要的所有不同目录中的路径填充您的列表框,例如:

procedure TForm1.Timer1Timer(Sender: TObject);
var
  fs: TFileStream;
  fn: string;
  sr: TSearchRec;
  I : integer;
begin
  I := 0;
  if FindFirst('C:/*.jpg', faAnyFile, sr) = 0 then
  begin
    repeat
      ListBox1.Items.Add('C:/' + sr.Name);
      Inc(I);
      if I = 7 then Break;
    until FindNext(sr) <> 0;
    FindClose(sr);
  end;

  if I < 7 then
  begin
    if FindFirst('C:/Some Other Folder/*.jpg', faAnyFile, sr) = 0 then
    begin
      repeat
        ListBox1.Items.Add('C:/Some Other Folder/' + sr.Name);
        Inc(I);
        if I = 7 then Break;
      until FindNext(sr) <> 0;
      FindClose(sr);
    end;
  end;

  idTCPClient1.Connect;
  for fn in ListBox1.Items do
  begin
    fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyWrite);
    try
      IdTCPClient1.IOHandler.WriteLn(ExtractFileName(fn));
      IdTCPClient1.IOHandler.Write(fs, 0, True);
      ...
    finally
      fs.Free;
    end;
  end;
end;