Search code examples
androidflutterdartsocket.ioport

I/flutter (20418): Error: SocketException: No route to host (OS Error: No route to host, errno = 113), address = 10.2.19.104, port = 33064


I'm trying to have a android flutter app that I'm developing in Android Studio connect to a linux server. It connects fine using an android emulator, but not when testing on my physical device.


Socket? _socket;

  Future<void> connect({
    required String ip,
    required int port,
  }) async {

    try {
      _socket = await Socket.connect(ip, 3000);
      stream = _socket!.asBroadcastStream();
    } on Exception catch(e){
      print("Error: $e");
    }

    print(ip);
    print(port);
    //
  }

When testing with a physical device, it always produces the following error, except with varying port addresses: I/flutter (20418): Error: SocketException: No route to host (OS Error: No route to host, errno = 113), address = 10.2.19.104, port = 33064

I've tried with a variety of physical phones. Only on one has it worked. The others give the same error message. Changing network settings does not appear to have any effect.


Solution

  • That's not a Flutter error, it's an error because the error comes from the OS. Basically, what happens is that when the OS asks the network for the IP, nobody responds, which is why it tells you that it can't find a host to route the IP. Download an application like pingtool for android and try to ping that way, and it's most likely that it will timeout.

    • Check that the IP is correct.

    try these two things, the first one is to connect to a Google IP on port 80, send 'html1.1\r\n\r\n', and also try to connect to the router if your router has a web interface. In the logs, you should see something like:

    Restarted application in 1,537ms. I/flutter ( 8643): 142.250.64.206 I/flutter ( 8643): 80 I/flutter ( 8643): HTTP/1.0 400 Bad Request...

    If the mobile has internet this should work.

    import 'dart:io';
    
    import 'package:flutter/material.dart';
    
    void main() => runApp(const MyApp());
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        Socket? socket;
    
        Future<void> connect({
          required String ip,
          required int port,
        }) async {
          try {
            Socket.connect(ip, port).then((Socket sock) {
              socket = sock;
              socket!.write('html1.1\r\n\r\n');
              socket!.listen((data) {
                print(String.fromCharCodes(data).trim());
              });
            });
          } on Exception catch (e) {
            print("Error: $e");
          }
    
          print(ip);
          print(port);
          //
        }
    
        return MaterialApp(
          title: 'Material App',
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Material App Bar'),
            ),
            body: Center(
              child: Column(
                children: [
                  TextButton(
                    onPressed: () {
                      connect(ip: '142.250.64.206', port: 80);
                    },
                    child: const Text('Connect http gogole.com'),
                  ),
                  TextButton(
                    onPressed: () {
                      connect(ip: '10.2.19.1', port: 80);
                    },
                    child: const Text('Connect http router'),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }