简体   繁体   中英

How can I get devices name after gathering all the IP addresses connected in a network? Flutter

I am using lan_scanner flutter package to gather IP Addresses of a.network.I want to also get the hostname of each particular address. Any ideas on how can I achieve that?

I tried using some other packages but to no avail.

you can connect to that device and ask for it's hostname

first add a server socket to listen for clients (devices)

const int port  = 7575;
try {
  ServerSocket server = await ServerSocket.bind(deviceIp, port);
  server.listen((client) {
    client.listen((data) {
      final ip = client.remoteAddress.address;
      final hostname = String.fromCharCodes(data);
      print("hostname of $ip is $hostname");
    });
  });
} on SocketException catch(_) {
  print('failed to create a server socket');
}

then for each device connect to the server and send the device hostname

try {
  Socket client = await Socket.connect(targetIp, port);
  client.write(Platform.localHostname);
  client.destroy();
} on SocketException catch(_) {
  print('failed to connect to the server');
}

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM