Is there a way to ping a local IP address using flutter?
Asked Answered
G

2

7

I want to check if a specific device is connected to my network or not. I have the ip address of that device. I am unable to find a way to ping that device using flutter app. The goal here is to check if a particular device is connected to local network or not by pinging the device. Can you help?

Gosney answered 3/1, 2020 at 7:39 Comment(0)
M
9

There is a package available called dart_ping which allows you to ping IPs, but it hasn't been updated in some time.

Alternatively there is a package called ping_discover_network that allows you to explore that network that your device is in. I haven't personally experimented with this one.

I've done some experiments with other packages to try to reproduce pinging, but the best result has been with the `dart_ping´ package:

import 'package:dart_ping/dart_ping.dart';

var pings = await ping('google.pl');

pings.listen((ping) {
  print(ping.time.inMilliseconds);
});
Mcdade answered 3/1, 2020 at 8:55 Comment(2)
Thanks for the insight. I have tried this package along with some others. This one doesn't even get inside ping.listenGosney
It's possible that the package no longer complies with the current version of the Flutter and Dart SDKs.Mcdade
H
9

Use this,

import 'dart:io';
Socket.connect(IPADDR, PORT, timeout: Duration(seconds: 5)).then((socket){
       print("Success");
       socket.destroy();
     }).catchError((error){
       print("Exception on Socket "+error.toString());
     });
Hanshansard answered 1/3, 2021 at 10:8 Comment(2)
Flutter Socket uses TCP. Ping is based on ICMP. This is incorrect.Mixologist
But this serves the main purpose of the OP : "I want to check if a specific device is connected to my network or not."Dobla

© 2022 - 2024 — McMap. All rights reserved.