Flutter app unable to resolve hostname for ping operation

52 Views Asked by At

I have developed a Flutter application that allows users to enter an IP address or hostname and performs a ping operation to determine its status. However, I encountered an issue where the app consistently reports "Error: Hostname not found" even when entering valid hostnames.

import 'dart:io';

import 'package:flutter/material.dart';

class SimplePingScreen extends StatefulWidget {
  @override
  _SimplePingScreenState createState() => _SimplePingScreenState();
}

class _SimplePingScreenState extends State<SimplePingScreen> {
  String hostname = '';
  String status = '-- --';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Simple Ping'),
      ),
      body: Padding(
        padding: EdgeInsets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            TextField(
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                labelText: 'Enter IP or Hostname',
              ),
              onChanged: (value) {
                setState(() {
                  hostname = value;
                });
              },
            ),
            SizedBox(height: 20.0),
            ElevatedButton(
              onPressed: () async {
                await pingHostname(hostname);
              },
              child: Text('Ping'),
            ),
            SizedBox(height: 20.0),
            Text(
              'Status:',
              style: TextStyle(fontSize: 18.0),
            ),
            SizedBox(height: 10.0),
            Text(
              status,
              style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold),
            ),
          ],
        ),
      ),
    );
  }

  Future<void> pingHostname(String hostname) async {
    print('Pinging $hostname');
    try {
      final result = await InternetAddress.lookup(hostname);
      final validAddresses = result.where((addr) => addr.rawAddress.isNotEmpty);
      if (validAddresses.isNotEmpty) {
        final response = await Process.run('ping', ['-c', '1', hostname]);
        if (response.exitCode == 0) {
          setState(() {
            status = '$hostname is Alive';
          });
        } else {
          setState(() {
            status = '$hostname Not Responding';
          });
        }
      } else {
        setState(() {
          status = 'Invalid hostname';
        });
      }
    } catch (e) {
      setState(() {
        if (e is SocketException && e.osError?.errorCode == 7) {
          status = 'Error: Hostname not found';
        } else {
          status = 'Error: $e';
        }
      });
    }
  }
}

What I've Tried:

  1. Verified the correctness of the entered hostname.
  2. Ensured network connectivity on the device.
  3. Checked DNS configuration and settings.
  4. Attempted to ping the same hostname from the terminal/command prompt, which was successful.
  5. Reviewed error logs and debugged the code, but couldn't identify the root cause.

Expected Outcome: I expect the Flutter app to successfully resolve and ping the entered hostname without encountering "Error: Hostname not found".

0

There are 0 best solutions below