2.9 C
New York
Sunday, February 23, 2025

flutter – ICMP ping on iOS works on simulator however not on actual gadgets


So I’m making an app with flutter that performs ping scanning to search out gadgets on my native community.
I’m utilizing this bundle
https://pub.dev/packages/flutter_icmp_ping

I’ve seen that there are some particular ip addresses that my actual system can not ping and returns PingError.requestTimedOut

The issue although occurs solely on the true system take a look at whereas within the simulator (and different comparable apps like Fing) it is ready to ping the identical ip completely.

Right here is the

import 'bundle:flutter/companies.dart';
import 'bundle:flutter_icmp_ping/flutter_icmp_ping.dart';
import 'bundle:ip_scanner_ios_new/sidemenu.dart';
import 'bundle:supplier/supplier.dart';
import 'bundle:shared_preferences/shared_preferences.dart';

class IcmpPingResult {
  last int seq;
  last String response;
  last double time;
  last bool success;

  IcmpPingResult({
    required this.seq,
    required this.response,
    required this.time,
    required this.success,
  });
}

class PingPage2 extends StatefulWidget {
  @override
  _IcmpPingPageState createState() => _IcmpPingPageState();
}

class _IcmpPingPageState extends State {
  String _host="google.com";
  int _count = 5;
  bool _running = false;
  Checklist _results = [];
  double _packetLoss = 0.0;
  last _scrollController = ScrollController();
  Ping? ping;

Future _runPing() async {
  setState(() {
    _running = true;
    _results.clear();
    _packetLoss = 0.0;
  });

  strive {
    print('Trying to ping $_host'); // Debug log
    ping = Ping(
      _host,
      depend: _count,
      timeout: 5, // Elevated timeout
      interval: 1, // Decreased interval
      ipv6: false,
      ttl: 64, // Modified TTL
    );

    ping!.stream.hear((occasion) {
      print('Uncooked ping occasion: $occasion'); // Debug log
      if (_results.size < _count) {
        last time = occasion.response?.time?.inMilliseconds.toDouble() ?? -1;
        last success = occasion.response != null;
        last response = occasion.toString();
        
        print('Ping outcome - Success: $success, Time: $time'); // Debug log

        setState(() {
          _results.add(IcmpPingResult(
            seq: _results.size + 1,
            response: response,
            time: time,
            success: success,
          ));

          _packetLoss = ((_count - _results.the place((r) => r.success).size) / _count) * 100;
        });
      }
    }, onDone: () {
      print('Ping stream accomplished'); // Debug log
      setState(() {
        _running = false;
      });
    }, onError: (e, stack) {
      print('Ping error: $e'); // Debug log
      print('Stack hint: $stack'); // Debug log
      setState(() {
        _running = false;
      });
    });
  } catch (e, stack) {
    print('Setup error: $e'); // Debug log
    print('Stack hint: $stack'); // Debug log
    setState(() {
      _running = false;
    });
  }
}

  @override
  void dispose() {
    ping?.cease();
    tremendous.dispose();
  }

  @override
  Widget construct(BuildContext context) {
    return Scaffold(         drawer: SideMenu(),

      appBar: AppBar(
        title: Textual content('ICMP Ping'),
      ),
      
      physique: SafeArea(
        youngster: Column(
          youngsters: [
            Padding(
              padding: EdgeInsets.all(16.0),
              child: Row(
                children: [
                  Expanded(
                    child: TextField(
                      decoration: InputDecoration(
                        hintText: 'Enter host name or IP address',
                        border: OutlineInputBorder(),
                      ),
                      onChanged: (value) {
                        _host = value;
                      },
                    ),
                  ),
                  SizedBox(width: 16.0),
                  SizedBox(
                    width: 60,
                    child: TextField(
                      keyboardType: TextInputType.number,
                      decoration: InputDecoration(
                        hintText: '#',
                        border: OutlineInputBorder(),
                      ),
                      onChanged: (value) {
                        _count = int.tryParse(value) ?? 5;
                      },
                    ),
                  ),
                  SizedBox(width: 16.0),
                  ElevatedButton(
                    onPressed: _running ? null : _runPing,
                    child: Text(_running ? 'Running...' : 'Start'),
                  ),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.0),
              youngster: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                youngsters: [
                  Text('${_results.length} of $_count'),
                  Text('${_packetLoss.toStringAsFixed(1)}% packet loss'),
                ],
              ),
            ),
            Expanded(
              youngster: ListView.separated(
                controller: _scrollController,
                itemCount: _results.size,
                itemBuilder: (context, index) {
                  last outcome = _results[index];
                  return ListTile(
                    main: Icon(
                      outcome.success ? Icons.check_circle : Icons.error,
                      coloration: outcome.success ? Colours.inexperienced : Colours.pink,
                    ),
                    title: Textual content('Sequence ${outcome.seq}'),
                    subtitle: Textual content(outcome.response),
                    trailing: Textual content('${outcome.time.toStringAsFixed(2)} ms'),
                  );
                },
                separatorBuilder: (context, index) => Divider(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}`

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles