admin管理员组文章数量:1296920
So I am making an app with flutter that performs ping scanning to find devices on my local network. I am using this package /packages/flutter_icmp_ping
I have noticed that there are some specific ip addresses that my real device cannot ping and returns PingError.requestTimedOut
The problem though happens only on the real device test while in the simulator (and other similar apps like Fing) it is able to ping the same ip perfectly.
Here is the
import 'package:flutter/services.dart';
import 'package:flutter_icmp_ping/flutter_icmp_ping.dart';
import 'package:ip_scanner_ios_new/sidemenu.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
class IcmpPingResult {
final int seq;
final String response;
final double time;
final 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<PingPage2> {
String _host = 'google';
int _count = 5;
bool _running = false;
List<IcmpPingResult> _results = [];
double _packetLoss = 0.0;
final _scrollController = ScrollController();
Ping? ping;
Future<void> _runPing() async {
setState(() {
_running = true;
_results.clear();
_packetLoss = 0.0;
});
try {
print('Attempting to ping $_host'); // Debug log
ping = Ping(
_host,
count: _count,
timeout: 5, // Increased timeout
interval: 1, // Decreased interval
ipv6: false,
ttl: 64, // Changed TTL
);
ping!.stream.listen((event) {
print('Raw ping event: $event'); // Debug log
if (_results.length < _count) {
final time = event.response?.time?.inMilliseconds.toDouble() ?? -1;
final success = event.response != null;
final response = event.toString();
print('Ping result - Success: $success, Time: $time'); // Debug log
setState(() {
_results.add(IcmpPingResult(
seq: _results.length + 1,
response: response,
time: time,
success: success,
));
_packetLoss = ((_count - _results.where((r) => r.success).length) / _count) * 100;
});
}
}, onDone: () {
print('Ping stream completed'); // Debug log
setState(() {
_running = false;
});
}, onError: (e, stack) {
print('Ping error: $e'); // Debug log
print('Stack trace: $stack'); // Debug log
setState(() {
_running = false;
});
});
} catch (e, stack) {
print('Setup error: $e'); // Debug log
print('Stack trace: $stack'); // Debug log
setState(() {
_running = false;
});
}
}
@override
void dispose() {
ping?.stop();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold( drawer: SideMenu(),
appBar: AppBar(
title: Text('ICMP Ping'),
),
body: SafeArea(
child: Column(
children: [
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),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('${_results.length} of $_count'),
Text('${_packetLoss.toStringAsFixed(1)}% packet loss'),
],
),
),
Expanded(
child: ListView.separated(
controller: _scrollController,
itemCount: _results.length,
itemBuilder: (context, index) {
final result = _results[index];
return ListTile(
leading: Icon(
result.success ? Icons.check_circle : Icons.error,
color: result.success ? Colors.green : Colors.red,
),
title: Text('Sequence ${result.seq}'),
subtitle: Text(result.response),
trailing: Text('${result.time.toStringAsFixed(2)} ms'),
);
},
separatorBuilder: (context, index) => Divider(),
),
),
],
),
),
);
}
}`
本文标签: flutterICMP ping on iOS works on simulator but not on real devicesStack Overflow
版权声明:本文标题:flutter - ICMP ping on iOS works on simulator but not on real devices - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741636287a2389657.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论