Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 878
Muhammad Usama Avatar asked Jan 03 '20 07:01

Muhammad Usama


People also ask

How do I find my local IP in flutter?

Add this code to _incrementCounter() : The code below works for any condition and returns local IP whether it comes from phone HotSpot or another router: import 'package:flutter/services. dart'; MethodChannel _channel = const MethodChannel('get_ip'); String ip = await _channel. invokeMethod('getIpAdress');


2 Answers

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());
     });
like image 171
Prabhu Avatar answered Nov 15 '22 09:11

Prabhu


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);
});
like image 24
João Soares Avatar answered Nov 15 '22 09:11

João Soares