How to set limit for max connection for single client in Dart http_server?


I have created a simple flutter app to serve my phone directory (Index of /) to share my files with the PC.
(I used IDM (Internet Download Manager) to download file)
it works fine on small files, but the app got closed automatically when I am downloading the large file to PC. (here I tried to download 691MB File)
as it creates 8 connections to download.

How do I set a limit for max Connection for a single client in this code? (like downloading the file by max. 2 connections)

(Implemented using HttpServer & VirtualDirectory.)

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),

HttpServer server;

startServer() async {

// Created VirtualDirectory Variable named staticFiles
  VirtualDirectory staticFiles = VirtualDirectory('/storage/emulated/0')
    ..allowDirectoryListing = true
    ..jailRoot = false;

// Started the server
  server = await HttpServer.bind(InternetAddress.anyIPv4, 7766);

// Serve the directory (Index of/)
  server.listen((req) {

stopServer() {
//Stop the Server

class HomePage extends StatelessWidget {
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Center(
          child: Text('Server Test'),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
            child: Text('Start Server'),
            onPressed: startServer,
            child: Text('Stop Server'),
            onPressed: stopServer,

Dependencies Installed:

  http_server: ^0.9.8+3

Make sure you have READ_EXTERNAL_STORAGE permission enabled.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

App work on Local Network

URL=> gatewayIP:7766 (most probably if you create a wifi hotspot from your phone and connect it to PC)

please help me to fix this.

ps: File download gets resumed when I open the app again and click on startServer.

edit: I was using IDM (Internet Download Manager) to download the file which was causing the crash, but when I used to download files from the browser it works fine.


I found the connectionsInfo() API in dart:io library

from connectionsInfo() API we can access four types of values/properties: Total,Active,Idle,Closeing see HttpConnectionsInfo Class documentation

To limit the Max Connection we have to use if-else statement when the server is listening to the client request:

//_server is an httpServer

    int maxConnection = 4;
    //if active connections are more than maxConnection than server will not response to any request
    if(_server.connectionsInfo().active <= maxConnection){
        // Or Your Logic for request response...

Answered By – SKJ

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *