skip to Main Content

I am using flutter to open an http server on Android

import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:jaguar/serve/server.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Http Server',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late Jaguar _server;

  @override
  void initState() {
    super.initState();
    init();
  }

  @override
  void dispose() {
    super.dispose();
    _server.close();
  }

  void init() async {
    try {
      _server = Jaguar(port: 7777);
      _server.get('/', (ctx) => 'Hello world!');
      await _server.serve();
    } catch (e) {
      print("Something went wrong while creating a server...");
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          var url = Uri.parse("http://localhost:7777");
          var response = await http.get(url);
          print('Response status: ${response.statusCode}');
          print('Response body: ${response.body}');
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

PC and Android real machine connected to the same WIFI

Test the app on the real machine and click add to get the expected results, but I cannot access this http service on the PC

$ curl http://192.168.9.102:7777
curl: (7) Failed to connect to 192.168.9.102 port 7777 after 2048 ms: Connection refused

I’m not sure what’s causing it, my guess is that Android blocks all port connections in order to protect the phone’s security, if so how do I expose port 7777 to the LAN

2

Answers


  1. Chosen as BEST ANSWER

    I found the problem, although the PC and android are connected to the same WIFI, but they are different ip

    When I check the WIFI connection details of Android, I found that the ip is 192.168.9.101, which is completely different from the PC

    $ curl http://192.168.9.101:7777
    Hello world!
    

    I am curious to use another Android phone to connect to the same WIFI and found that the ip is 192.168.9.100


  2. This is a routing problem.

    Most WiFi routers have two relevant settings (its manual should suffice as reputable source):

    • When "connected to the same Wifi": enable the setting, which permits devices to communicate with each other. Else the devices cannot see each other.
    • For wired traffic: enable to bridge device on the WiFi router. Unless these two subnets are being bridged, the wired LAN traffic cannot be routed to the wireless network segment.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search