Dart servers refusing connection in local network

Issue

I have Dart running on Mac Mavericks and Ubuntu Linux. I am just getting started;and, none of the examples will function through my local network. When I install a server on Ubuntu and try to access with the mac- nothing happens. When I try to access from Ubunutu, I get a “java.net Connection Refused error” Same goes for Curl on either machine, (connection refused). All of the samples run fine as client/server from localhost. Neither, Client apps or Browsers are able to connect. All other servers/clients working normally in the home network. Firewall is ok, and I double checked port numbers. There are no conflicting servers on the desired ports. I tried about 5 different scripts from the Dart site and others, each with a CORS function, but nothing connects, including the sample below, SimpleServer
-Github

Sample Code

/* A simple web server that responds to **ALL** GET requests by returning
 * the contents of data.json file, and responds to ALL **POST** requests
 * by overwriting the contents of the data.json file
 * 
 * Browse to it using http://localhost:8080  
 * 
 * Provides CORS headers, so can be accessed from any other page
 */
 import 'dart:io';

final HOST = "127.0.0.1"; // eg: localhost 
final PORT = 8080; 
final DATA_FILE = "data.json";

void main() {
  HttpServer.bind(HOST, PORT).then((server) {
     server.listen((HttpRequest request) {
      switch (request.method) {
        case "GET": 
          handleGet(request);
          break;
        case "POST": 
          handlePost(request);
          break;
        case "OPTIONS": 
          handleOptions(request);
          break;
        default: defaultHandler(request);
      }
    }, 
    onError: printError);

    print("Listening for GET and POST on http://$HOST:$PORT");
  },
  onError: printError);
}

/**
 * Handle GET requests by reading the contents of data.json
 * and returning it to the client
 */
void handleGet(HttpRequest req) {
  HttpResponse res = req.response;
  print("${req.method}: ${req.uri.path}");
  addCorsHeaders(res);

  var file = new File(DATA_FILE);
  if (file.existsSync()) {
    res.headers.add(HttpHeaders.CONTENT_TYPE, "application/json");
    file.readAsBytes().asStream().pipe(res); // automatically close output stream
  }
  else {
    var err = "Could not find file: $DATA_FILE";
    res.write(err);
    res.close();  
  }

}

/**
 * Handle POST requests by overwriting the contents of data.json
 * Return the same set of data back to the client.
 */
void handlePost(HttpRequest req) {
  HttpResponse res = req.response;
  print("${req.method}: ${req.uri.path}");

  addCorsHeaders(res);

  req.listen((List<int> buffer) {
    var file = new File(DATA_FILE);
    var ioSink = file.openWrite(); // save the data to the file
    ioSink.add(buffer);
    ioSink.close();

    // return the same results back to the client
    res.add(buffer);
    res.close();
  },
  onError: printError);
}

/**
 * Add Cross-site headers to enable accessing this server from pages
 * not served by this server
 * 
 * See: http://www.html5rocks.com/en/tutorials/cors/ 
 * and http://enable-cors.org/server.html
 */
void addCorsHeaders(HttpResponse res) {
  res.headers.add("Access-Control-Allow-Origin", "*");
  res.headers.add("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
  res.headers.add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,     Accept");
 }

void handleOptions(HttpRequest req) {
  HttpResponse res = req.response;
  addCorsHeaders(res);
  print("${req.method}: ${req.uri.path}");
  res.statusCode = HttpStatus.NO_CONTENT;
res.close();
}

void defaultHandler(HttpRequest req) {
  HttpResponse res = req.response;
  addCorsHeaders(res);
  res.statusCode = HttpStatus.NOT_FOUND;
  res.write("Not found: ${req.method}, ${req.uri.path}");
  res.close();
}

void printError(error) => print(error);

data.json

{
  "language":"DART",
  "targets":["dartium","javascript","Android?"],
  "website":{
   "homepage":"www.dartlang.org",
   "api":"api.dartlang.org"
  }

}

Solution

I think this is because the Dart server is configured to listen to only the clients on the localhost. You need to make it to listen to any IPv4/v6 client in order to receive any response from the server over the network.

Change the line final HOST = "127.0.0.1"; to final HOST = InternetAddress.ANY_IP_V4;
or final HOST = InternetAddress.ANY_IP_V6;

It should make it work.

Answered By – Rahul De

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

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