ping method

Future<PingResult> ping({
  1. required String host,
  2. int count = 4,
  3. Duration timeout = const Duration(seconds: 3),
  4. Duration interval = const Duration(milliseconds: 200),
  5. int? port,
  6. PingMode mode = PingMode.tcp,
})

执行 Ping 测试 / Runs a ping test.

count 为探测次数,interval 为两次探测之间的间隔 / count is the number of probes, interval the delay between them. modePingMode.icmp 时仅桌面端可用,其它平台抛出 UnsupportedError / PingMode.icmp is desktop-only and throws UnsupportedError elsewhere.

Implementation

Future<PingResult> ping({
  required String host,
  int count = 4,
  Duration timeout = const Duration(seconds: 3),
  Duration interval = const Duration(milliseconds: 200),
  int? port,
  PingMode mode = PingMode.tcp,
}) async {
  if (count <= 0) {
    return PingResult(
      host: host,
      sent: 0,
      received: 0,
      times: const <double>[],
      timestamp: DateTime.now(),
      port: port ?? defaultPort,
      mode: mode,
    );
  }

  if (mode == PingMode.icmp) {
    return _icmpPing(host: host, count: count, timeout: timeout);
  }
  return _tcpPing(
    host: host,
    count: count,
    timeout: timeout,
    interval: interval,
    port: port ?? defaultPort,
  );
}