Categories
Dart

HTTP Server/Client in Dart Language

Serves “A Basic HTTP Server” text response on http://127.0.0.1:2222

import 'dart:io';

main() {
  HttpServer
      .bind("127.0.0.1", 2222)
      .then((server) {
        server.listen((HttpRequest request) {
          request.response.write('A Basic HTTP Server');
          request.response.close();
        });
      });
}

Client

import 'dart:io';
import 'dart:convert';

main() {
new HttpClient().get('127.0.0.1', 2222, '/')
     .then((HttpClientRequest request) => request.close())
     .then((HttpClientResponse response) {
       response.transform(utf8.decoder).listen((contents) {
         print(contents);
       });
     });
}