英文:
Is there a way to ping a local IP address using flutter?
问题
我想检查特定设备是否连接到我的网络。我有该设备的IP地址。我无法找到使用Flutter应用程序ping该设备的方法。
这里的目标是通过ping设备来检查特定设备是否连接到本地网络。你能帮忙吗?
英文:
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?
答案1
得分: 9
使用以下代码:
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());
});
英文:
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());
});
答案2
得分: 6
有一个名为 dart_ping 的包可用,允许您ping IP地址,但它已经有一段时间没有更新了。
或者还有一个名为 ping_discover_network 的包,允许您探索您的设备所在的网络。我个人没有使用过这个包。
我已经尝试过使用其他包进行ping的实验,但最好的结果是使用 dart_ping
包:
import 'package:dart_ping/dart_ping.dart';
var pings = await ping('google.pl');
pings.listen((ping) {
print(ping.time.inMilliseconds);
});
英文:
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);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论