ZetCode

Dart HttpConnectionInfo

last modified October 18, 2026

The HttpConnectionInfo class in Dart provides information about HTTP server connections. It's available in server-side Dart applications.

This class exposes the remote address, remote port, and local port of a connection. It's part of Dart's dart:io library.

Basic Definition

HttpConnectionInfo is an interface that describes an HTTP connection. It's typically obtained from an HttpRequest object.

Key properties include the remote address and the remote and local port numbers. This information is useful for logging and access control.

Basic HttpConnectionInfo Usage

This example shows how to access basic connection information.

main.dart
import 'dart:io';

void main() async {
  var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080);
  print('Server running on ${server.port}');

  await for (var request in server) {
    var info = request.connectionInfo;
    if (info == null) {
      request.response.statusCode = HttpStatus.internalServerError;
      request.response.write('Connection information unavailable');
    } else {
      print('Remote address: ${info.remoteAddress.address}');
      print('Remote port: ${info.remotePort}');
      print('Local port: ${info.localPort}');
      request.response.write('Hello from server!');
    }
    await request.response.close();
  }
}

We create an HTTP server and access connection info for each request. The remoteAddress, remotePort and localPort properties describe the two ends of the connection.

$ dart main.dart
Server running on 8080
Remote address: 127.0.0.1
Remote port: 54321
Local port: 8080

Local and Remote Endpoints

This example shows the endpoint properties available for a connection.

main.dart
import 'dart:io';

void main() async {
  var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080);

  await for (var request in server) {
    var info = request.connectionInfo;
    if (info == null) {
      request.response.statusCode = HttpStatus.internalServerError;
    } else {
      print('Connection Info:');
      print('- Remote: ${info.remoteAddress.address}:${info.remotePort}');
      print('- Local port: ${info.localPort}');
      request.response.write('''
      Connection Info:
      - Remote: ${info.remoteAddress.address}:${info.remotePort}
      - Local port: ${info.localPort}
    ''');
    }
    await request.response.close();
  }
}

We print the remote endpoint and the local port. HttpConnectionInfo does not expose TLS protocol or cipher details.

$ dart main.dart
Connection Info:
- Remote: 127.0.0.1:54321
- Local port: 8080

Logging Connection Details

This example shows how to log comprehensive connection information.

main.dart
import 'dart:io';

void logConnection(HttpConnectionInfo info) {
  print('''
  New Connection:
  - Remote: ${info.remoteAddress.address}:${info.remotePort}
  - Local port: ${info.localPort}
  ''');
}

void main() async {
  var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080);

  await for (var request in server) {
    var info = request.connectionInfo;
    if (info != null) {
      logConnection(info);
    }
    request.response.write('Logged your connection!');
    await request.response.close();
  }
}

We create a dedicated logging function for connection information. This pattern is useful for monitoring and debugging server applications. HttpConnectionInfo exposes the remote address and the remote and local ports.

$ dart main.dart
New Connection:
- Remote: 127.0.0.1:54321
- Local port: 8080

Handling Multiple Connections

This example shows tracking multiple connections with unique IDs.

main.dart
import 'dart:io';

String generateConnectionId(HttpConnectionInfo info) {
  return '${info.remoteAddress.address}-${info.remotePort}';
}

void main() async {
  var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 8080);
  var connections = <String, int>{};

  await for (var request in server) {
    var info = request.connectionInfo;
    if (info == null) {
      request.response.statusCode = HttpStatus.internalServerError;
      await request.response.close();
      continue;
    }

    var id = generateConnectionId(info);
    connections[id] = (connections[id] ?? 0) + 1;

    print('Connection ID: $id');
    print('Visit count: ${connections[id]}');
    print('Total connections: ${connections.length}');

    request.response.write('''
      Connection ID: $id
      Visit count: ${connections[id]}
      Total connections: ${connections.length}
    ''');

    await request.response.close();
  }
}

We generate unique IDs for each connection using the remote address and port. This allows tracking individual client interactions with the server.

$ dart main.dart
Connection ID: 127.0.0.1-54321
Visit count: 1
Total connections: 1

Secure Server Example

This example demonstrates HttpConnectionInfo with a secure HTTPS server.

main.dart
import 'dart:io';

Future<SecurityContext> getSecurityContext() async {
  var context = SecurityContext();
  // In production, use real certificates
  context.useCertificateChain('path/to/cert.pem');
  context.usePrivateKey('path/to/key.pem');
  return context;
}

void main() async {
  var context = await getSecurityContext();
  var server = await HttpServer.bindSecure(
    InternetAddress.loopbackIPv4,
    8443,
    context,
  );

  await for (var request in server) {
    var info = request.connectionInfo;
    if (info == null) {
      request.response.statusCode = HttpStatus.internalServerError;
    } else {
      print('Secure Connection Info:');
      print('- Remote: ${info.remoteAddress.address}:${info.remotePort}');
      print('- Local port: ${info.localPort}');
      request.response.write('''
      Secure Connection Info:
      - Remote: ${info.remoteAddress.address}:${info.remotePort}
      - Local port: ${info.localPort}
    ''');
    }
    await request.response.close();
  }
}

With a secure server, HttpConnectionInfo provides the same endpoint information. TLS protocol and cipher details are not exposed by this class.

$ dart main.dart
Secure Connection Info:
- Remote: 127.0.0.1:54321
- Local port: 8443

Best Practices

Source

Dart HttpConnectionInfo Documentation

This tutorial covered Dart's HttpConnectionInfo class with practical examples showing how to access and utilize HTTP connection information in server apps.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.