Итак, я делаю приложение с Flutter, которое выполняет сканирование Ping, чтобы найти устройства в моей локальной сети.
Я использую этот пакет
https://pub.dev/packages/flutter_icmp_ping
Я заметил, что есть некоторые конкретные IP -адреса, которые мое реальное устройство не может пинг и возвращает pingerror.requesttimedout < /p>
Проблема, хотя происходит только на реальном тесте устройства В то время как в симуляторе (и других подобных приложениях, таких как ног), он может отлично пинговать один и тот же IP. -none PrettyPrint-Override ">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 {
String _host = 'google.com';
int _count = 5;
bool _running = false;
List _results = [];
double _packetLoss = 0.0;
final _scrollController = ScrollController();
Ping? ping;
Future _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(),
),
),
],
),
),
);
}
}`
Подробнее здесь: https://stackoverflow.com/questions/794 ... al-devices
ICMP Ping на iOS работает на симуляторе, но не на реальных устройствах ⇐ IOS
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение