ZetCode

Dart RawSocket

last modified October 18, 2026

The RawSocket class in Dart provides low-level network socket communication capabilities. It allows direct TCP/IP socket operations.

RawSocket is part of Dart's dart:io library and offers more control than higher-level alternatives like HttpClient or WebSocket.

Basic Definition

RawSocket represents a raw network socket connection. It provides direct access to socket events and data streams without protocol handling.

Key features include event-based communication, direct byte stream access, and fine-grained connection control. It's useful for custom protocols.

Basic RawSocket Client

This example shows a basic RawSocket client connecting to a server.

main.dart
import 'dart:io';

void main() async {
  try {
    var host = 'example.com';
    var socket = await RawSocket.connect(host, 80);
    print('Connected to $host');

    socket.listen((event) {
      if (event == RawSocketEvent.read) {
        var data = socket.read();
        if (data != null) {
          print('Received: ${String.fromCharCodes(data)}');
        }
      } else if (event == RawSocketEvent.readClosed) {
        socket.close();
      }
    });

    socket.write('GET / HTTP/1.1\r\nHost: example.com\r\n'
        'Connection: close\r\n\r\n'.codeUnits);
  } catch (e) {
    print('Error: $e');
  }
}

We create a TCP connection to example.com on port 80. The listen method handles socket events. We send an HTTP request and print responses.

$ dart main.dart
Connected to example.com
Received: HTTP/1.1 200 OK
...

Handling Multiple Socket Events

This example demonstrates handling different socket events.

main.dart
import 'dart:io';

void main() async {
  var socket = await RawSocket.connect('localhost', 8080);

  socket.listen((event) {
    switch (event) {
      case RawSocketEvent.read:
        var data = socket.read();
        if (data != null) {
          print('Data: ${String.fromCharCodes(data)}');
        }
        break;
      case RawSocketEvent.write:
        print('Ready to write');
        break;
      case RawSocketEvent.readClosed:
        print('Read closed');
        break;
      case RawSocketEvent.closed:
        print('Connection closed');
        break;
    }
  });

  socket.write('Hello server'.codeUnits);
  await Future.delayed(Duration(seconds: 1));
  await socket.close();
}

We handle read, write and closed events separately. The switch statement routes different socket events to appropriate handlers for processing.

$ dart main.dart
Ready to write
Data: Hello client
Read closed
Connection closed

Creating a Simple Echo Server

This example shows a basic echo server using RawSocket.

server.dart
import 'dart:io';

void main() async {
  var server = await RawServerSocket.bind('127.0.0.1', 8080);
  print('Listening on ${server.address.address}:${server.port}');

  server.listen((client) {
    client.listen((event) {
      if (event == RawSocketEvent.read) {
        var data = client.read();
        if (data != null) {
          print('Received: ${String.fromCharCodes(data)}');
          client.write(data);
        }
      } else if (event == RawSocketEvent.readClosed ||
          event == RawSocketEvent.closed) {
        client.close();
      }
    });
  });
}

The server binds to localhost port 8080. It echoes back any received data. Each client connection gets its own event listener for handling messages.

$ dart server.dart
Listening on 127.0.0.1:8080
Received: Test message

Socket Timeout Handling

This example demonstrates implementing a connection timeout.

main.dart
import 'dart:io';
import 'dart:async';

void main() async {
  try {
    var socket = await RawSocket.connect('example.com', 80)
      .timeout(Duration(seconds: 3));
    
    socket.listen((event) {
      if (event == RawSocketEvent.read) {
        print('Data received');
      }
    });

    print('Connection successful');
    await socket.close();
  } on TimeoutException {
    print('Connection timed out');
  } on SocketException catch (e) {
    print('Socket error: $e');
  }
}

We add a 3-second timeout to the connection attempt. The timeout method throws TimeoutException if connection isn't established in time.

$ dart main.dart
Connection successful

Binary Data Transmission

This example shows sending and receiving binary data.

main.dart
import 'dart:io';
import 'dart:typed_data';

void main() async {
  var socket = await RawSocket.connect('localhost', 9000);

  // Send binary data
  var bytes = Uint8List.fromList([0, 1, 2, 3, 255]);
  socket.write(bytes);

  socket.listen((event) {
    if (event == RawSocketEvent.read) {
      var response = socket.read();
      if (response != null) {
        print('Received bytes: $response');
      }
    }
  });

  await Future.delayed(Duration(seconds: 1));
  await socket.close();
}

We create a Uint8List containing binary values and send it through the socket. The socket handles binary data transparently without any conversion needed.

$ dart main.dart
Received bytes: [0, 1, 2, 3, 255]

Best Practices

Source

Dart RawSocket Documentation

This tutorial covered Dart's RawSocket class with practical examples showing basic usage, event handling, binary data, and server implementation.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Dart tutorials.