multithreading - Delphi (XE2) Indy (10) 多线程 Ping

标签 multithreading delphi ping indy tthread

我的房间里有 60 台计算机/设备(40 台计算机和 20 台基于 Windows CE 的示波器),我想使用 ping 来了解哪些计算机/设备处于事件状态。首先,我编写了一个标准 ping(请参见此处 Delphi Indy Ping Error 10040 ),它现在工作正常,但在大多数计算机离线时需要很长时间。

所以我想做的是编写一个多线程 Ping,但我对此非常挣扎。我在互联网上只看到了很少的例子,没有人符合我的需求,这就是为什么我尝试自己写它。

我使用XE2和Indy 10,表单仅由备忘录和按钮组成。

unit Main;

interface

uses
  Winapi.Windows, System.SysUtils, System.Classes, Vcl.Forms,
  IdIcmpClient, IdGlobal, Vcl.StdCtrls, Vcl.Controls;

type
  TMainForm = class(TForm)
    Memo1: TMemo;
    ButtonStartPing: TButton;
    procedure ButtonStartPingClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TMyPingThread = class(TThread)
  private
    fIndex : integer;
    fIdIcmpClient: TIdIcmpClient;
    procedure doOnPingReply;
  protected
    procedure Execute; override;
  public
    constructor Create(index: integer);
  end;

var
  MainForm: TMainForm;
  ThreadCOunt : integer;

implementation

{$R *.dfm}

constructor TMyPingThread.Create(index: integer);
begin
  inherited Create(false);

  fIndex := index;
  fIdIcmpClient := TIdIcmpClient.Create(nil);
  fIdIcmpClient.ReceiveTimeout := 200;
  fIdIcmpClient.PacketSize := 24;
  fIdIcmpClient.Protocol := 1;
  fIdIcmpClient.IPVersion := Id_IPv4;

  //first computer is at adresse 211
  fIdIcmpClient.Host := '128.178.26.'+inttostr(211+index-1);

  self.FreeOnTerminate := true;
end;

procedure TMyPingThread.doOnPingReply;
begin
  MainForm.Memo1.lines.add(inttostr(findex)+' '+fIdIcmpClient.ReplyStatus.Msg);
  dec(ThreadCount);

  if ThreadCount = 0 then
    MainForm.Memo1.lines.add('--- End ---');
end;

procedure TMyPingThread.Execute;
begin
  inherited;

  try
    fIdIcmpClient.Ping('',findex);
  except
  end;

  while not Terminated do
  begin
    if fIdIcmpClient.ReplyStatus.SequenceId = findex then Terminate;
  end;

  Synchronize(doOnPingReply);
  fIdIcmpClient.Free;
end;

procedure TMainForm.ButtonStartPingClick(Sender: TObject);
var
  i: integer;
  myPing : TMyPingThread;
begin
  Memo1.Lines.Clear;

  ThreadCount := 0;
  for i := 1 to 40 do
  begin
    inc(ThreadCount);
    myPing := TMyPingThread.Create(i);
    //sleep(10);
  end;
end;

end.

我的问题是,当我取消注释“sleep(10)”时,它“似乎”可以工作,而没有它,“似乎”就无法工作。这肯定意味着我在编写的线程中遗漏了一点。

换句话说。当代码中存在 Sleep(10) 时。每次我单击按钮来检查连接时,结果都是正确的。

如果没有 sleep (10),它“大部分”时间都在工作,但有时结果是错误的,在离线计算机上给我一个 ping 回显,而在在线计算机上没有 ping 回显,因为未分配 ping 回复到正确的线程。

欢迎任何评论或帮助。

-----编辑/重要-----

作为此问题的一般跟进,@Darian Miller 启动了 Google Code project here https://code.google.com/p/delphi-stackoverflow/这是一个工作基础。我将他的答案标记为“已接受的答案”,但用户应该引用这个开源项目(所有功劳都属于他),因为它将来肯定会扩展和更新。

最佳答案

根本问题在于 ping 是无连接流量。如果您有多个 TIdIcmpClient 对象同时 ping 网络,则一个 TIdIcmpClient 实例可以收到实际上属于另一个 TIdIcmpClient 实例的回复。您试图通过检查 SequenceId 值在线程循环中考虑这一点,但您没有考虑到 TIdIcmpClient 已经在内部进行了相同的检查。它循环读取网络回复,直到收到期望的回复,或者直到发生 ReceiveTimeout 。如果它收到了它不期望的答复,它就会简单地丢弃该答复。因此,如果一个 TIdIcmpClient 实例丢弃另一个 TIdIcmpClient 实例期望的回复,则您的代码将不会处理该回复,而其他 TIdIcmpClient 也不会处理该回复。 > 可能会收到另一个 TIdIcmpClient 的回复,依此类推。通过添加 Sleep(),您可以减少(但不能消除) ping 相互重叠的机会。

对于您尝试执行的操作,您将无法按原样使用 TIdIcmpClient 来并行运行多个 ping,抱歉。它根本不是为此而设计的。它无法按照您需要的方式区分回复数据。您必须序列化线程,以便一次只有一个线程可以调用 TIdIcmpClient.Ping()

如果序列化 ping 不适合您,您可以尝试将 TIdIcmpClient 的部分源代码复制到您自己的代码中。有 41 个线程正在运行 - 40 个设备线程和 1 个响应线程。创建所有线程共享的单个套接字。让每个设备线程准备并使用该套接字将其单独的 ping 请求发送到网络。然后让响应线程不断从同一套接字读取回复,并将它们路由回适当的设备线程进行处理。这需要更多的工作,但它将为您提供所需的多 ping 并行性。

如果您不想遇到这些麻烦,另一种方法是使用已经支持同时 ping 多台计算机的第三方应用程序,例如 FREEPing .

关于multithreading - Delphi (XE2) Indy (10) 多线程 Ping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12858551/

相关文章:

database - 在 Delphi7 TListView 中显示 (Synopse) SQLite3 表列

delphi - TPNGImage.LoadFromFile EPNGInvalidFileHeader

python - ftplib python : NOOP command works in ASCII not Binary

c++ - 使用局部全局变量在单独的线程中运行 C 函数

delphi - 如何将 Indy UTF-8 响应转换为 native Delphi (Unicode) 字符串?

php - cURL connect() 超时

powershell - 将测试连接转换为 bool 值

linux - 带绝对时间戳的 ping

java - Executors.newSingleThreadExecutor 将始终在同一系统核心上运行?

c# - C# 中的 Web 进度条