9.3 C
Canberra
Tuesday, July 1, 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 seek out gadgets on my native community.
I’m utilizing this package deal
https://pub.dev/packages/flutter_icmp_ping

I’ve observed that there are some particular ip addresses that my actual gadget can’t ping and returns PingError.requestTimedOut

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

Right here is the

import 'package deal:flutter/providers.dart';
import 'package deal:flutter_icmp_ping/flutter_icmp_ping.dart';
import 'package deal:ip_scanner_ios_new/sidemenu.dart';
import 'package deal:supplier/supplier.dart';
import 'package deal:shared_preferences/shared_preferences.dart';

class IcmpPingResult {
  closing int seq;
  closing String response;
  closing double time;
  closing 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;
  Record _results = [];
  double _packetLoss = 0.0;
  closing _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.pay attention((occasion) {
      print('Uncooked ping occasion: $occasion'); // Debug log
      if (_results.size < _count) {
        closing time = occasion.response?.time?.inMilliseconds.toDouble() ?? -1;
        closing success = occasion.response != null;
        closing response = occasion.toString();
        
        print('Ping consequence - 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(
        baby: Column(
          kids: [
            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),
              baby: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                kids: [
                  Text('${_results.length} of $_count'),
                  Text('${_packetLoss.toStringAsFixed(1)}% packet loss'),
                ],
              ),
            ),
            Expanded(
              baby: ListView.separated(
                controller: _scrollController,
                itemCount: _results.size,
                itemBuilder: (context, index) {
                  closing consequence = _results[index];
                  return ListTile(
                    main: Icon(
                      consequence.success ? Icons.check_circle : Icons.error,
                      colour: consequence.success ? Colours.inexperienced : Colours.purple,
                    ),
                    title: Textual content('Sequence ${consequence.seq}'),
                    subtitle: Textual content(consequence.response),
                    trailing: Textual content('${consequence.time.toStringAsFixed(2)} ms'),
                  );
                },
                separatorBuilder: (context, index) => Divider(),
              ),
            ),
          ],
        ),
      ),
    );
  }
}`

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles