Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"flutter (os error: no address associated with hostname, errno = 7)" only for android

I created a DNS server in esp32 wifi module (arduino framework) to communicate with flutter app. It working correctly in dart and flutter ios app but its not working with flutter andoid app. I am getting the following error in flutter android "flutter (os error: no address associated with hostname, errno = 7)". Since it is working perfectly with dart and ios flutter app I hope its not the esp32 wifi program error. I think its problem with flutter andoid. I tried the methods posted in "https://github.com/flutter/flutter/issues/27883" and is also not working in my case. So please help me to solve this issue.

https://github.com/flutter/flutter/issues/27883

ESP32 code


#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>

WiFiServer wifiServer(80);

String hostname="esp32";

const char* ssid = "ssid";
const char* password =  "password";

void setup() {
  Serial.begin(115200);

  WiFi.mode(WIFI_AP);
  WiFi.begin(ssid, password);
  WiFi.setHostname("toyama");
  Serial.printf("Connecting to ssid '%s'", ssid);
  Serial.println();
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.printf("\nWiFi connected to '%s'", WiFi.SSID().c_str());
  Serial.println();
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (!MDNS.begin(hostname.c_str())) {
    Serial.println("Error setting up MDNS responder!");
    while (1) {
      delay(1000);
    }
  }
  Serial.printf("mDNS responder started at %s.local:80\n",hostname.c_str());

  // Start TCP (HTTP) server
  wifiServer.begin();

  // Add service to MDNS-SD
  MDNS.addService("http", "tcp", 80);
  Serial.print("Server started at ");
  Serial.print(WiFi.localIP());
  Serial.println(":80");

}

void loop()
{
  WiFiClient client = wifiServer.available();

  if (client) {
    Serial.println("Client connected");
    while (client.connected()) {

      if (client.available() > 0) {
        String c = client.readString();
        Serial.print(client.remoteIP().toString());
        Serial.print(":");
        Serial.print(c);
      }

      if (Serial.available() > 0)
      {
        String ch = Serial.readString();
        client.println(ch);
        Serial.print(WiFi.localIP());
        Serial.print(":");
        Serial.println(ch);
      }
    }

    client.stop();
    Serial.println("Client disconnected");

  }
}

Flutter code


import 'package:flutter/foundation.dart';
import 'dart:io';
import 'package:flutter/material.dart';

void main() async {
  // modify with your true address/port
  Socket sock = await Socket.connect("esp32.local", 80);
  runApp(MyApp(sock));
}

class MyApp extends StatelessWidget {
  Socket socket;

  MyApp(Socket s) {
    this.socket = s;
  }

  @override
  Widget build(BuildContext context) {
    final title = 'TcpSocket Demo';
    return MaterialApp(
      title: title,
      home: MyHomePage(
        title: title,
        channel: socket,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  final String title;
  final Socket channel;

  MyHomePage({Key key, @required this.title, @required this.channel})
      : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController _controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Form(
              child: TextFormField(
                controller: _controller,
                decoration: InputDecoration(labelText: 'Send a message'),
              ),
            ),
            StreamBuilder(
              stream: widget.channel,
              builder: (context, snapshot) {
                return Padding(
                  padding: const EdgeInsets.symmetric(vertical: 24.0),
                  child: Text(snapshot.hasData
                      ? '${String.fromCharCodes(snapshot.data)}'
                      : ''),
                );
              },
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _sendMessage,
        tooltip: 'Send message',
        child: Icon(Icons.send),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void _sendMessage() {
    if (_controller.text.isNotEmpty) {
      widget.channel.writeln(_controller.text);
    }
    // widget.channel.write('\n');
  }

  @override
  void dispose() {
    widget.channel.close();
    super.dispose();
  }
}
like image 545
Yatheesha S Avatar asked May 10 '19 15:05

Yatheesha S


2 Answers

Add < uses-permission android:name="android.permission.INTERNET"/> to your Manifest.xml file. In your AVD Manager select the device and wipe out data by going on options (Arrow down button). Restart your emulator. Rerun the application.

like image 73
Hitendra Singh Shekhawat Avatar answered Sep 28 '22 08:09

Hitendra Singh Shekhawat


if < uses-permission android:name="android.permission.INTERNET"/> is added to your Manifest.xml file and you still get this error,

then be assured that this error is thrown when there is a slow network on the laptop you are using to run the AVD

like image 42
Okonkwo Clovis Saintiano Avatar answered Sep 28 '22 08:09

Okonkwo Clovis Saintiano