ZetCode

Dart RawSynchronousSocket

last modified October 18, 2026

The RawSynchronousSocket class in Dart provides synchronous socket operations for low-level network programming. It's part of Dart's dart:io library.

Unlike asynchronous sockets, RawSynchronousSocket blocks until operations complete. This makes it suitable for specific use cases requiring synchronous behavior.

Basic Definition

RawSynchronousSocket represents a synchronous TCP socket connection. It allows blocking read/write operations without callbacks or futures.

Key features include synchronous data transfer, direct byte access, and connection management. It's typically used in specialized scenarios.

Creating a Synchronous Socket

This example shows how to create a synchronous socket connection.

main.dart
import 'dart:io';

void main() {
  var host = 'example.com';
  var socket = RawSynchronousSocket.connectSync(host, 80);

  print('Connected to $host:80');
  socket.closeSync();
}

We create a synchronous TCP connection to a web server. The connectSync method blocks until the connection succeeds, and throws a SocketException on failure. Always close sockets when done.

$ dart main.dart
Connected to example.com:80

Synchronous Read Operation

This example demonstrates synchronous reading from a socket.

main.dart
import 'dart:io';

void main() {
  var socket = RawSynchronousSocket.connectSync('example.com', 80);

  var request = 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n';
  socket.writeFromSync(request.codeUnits);

  var buffer = List.filled(1024, 0);
  var bytesRead = socket.readIntoSync(buffer, 0, buffer.length);

  print('Read $bytesRead bytes:');
  print(String.fromCharCodes(buffer.sublist(0, bytesRead)));

  socket.closeSync();
}

We send an HTTP request and read the response synchronously. The readIntoSync method blocks until data is available. The buffer stores received bytes.

$ dart main.dart
Read 937 bytes:
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
...

Synchronous Write Operation

This example shows synchronous writing to a socket.

main.dart
import 'dart:io';

void main() {
  var socket = RawSynchronousSocket.connectSync('localhost', 8080);

  var message = 'Hello, server!\n';
  socket.writeFromSync(message.codeUnits);

  print('Wrote ${message.length} bytes to socket');

  socket.closeSync();
}

We connect to a local server and write data synchronously. The writeFromSync method blocks until the bytes have been written and does not return a count.

$ dart main.dart
Wrote 15 bytes to socket

Handling Multiple Operations

This example demonstrates multiple synchronous operations on a socket.

main.dart
import 'dart:io';

void main() {
  var socket = RawSynchronousSocket.connectSync('time.nist.gov', 13);

  var buffer = List.filled(256, 0);
  var totalBytes = 0;
  
  while (true) {
    var bytesRead = socket.readIntoSync(buffer, 0, buffer.length);
    if (bytesRead <= 0) break;
    
    totalBytes += bytesRead;
    print(String.fromCharCodes(buffer.sublist(0, bytesRead)));
  }
  
  print('Total bytes received: $totalBytes');
  socket.closeSync();
}

We connect to a daytime server and read data in a loop. The readIntoSync call blocks until data arrives. The loop exits when the connection closes.

$ dart main.dart
59335 23-04-25 12:34:56 00 0 0 123.5 UTC(NIST) 
Total bytes received: 51

Error Handling

This example shows proper error handling with synchronous sockets.

main.dart
import 'dart:io';

void main() {
  try {
    var socket = RawSynchronousSocket.connectSync('invalid.host', 80);

    socket.writeFromSync('GET / HTTP/1.1\r\n\r\n'.codeUnits);

    var buffer = List.filled(1024, 0);
    socket.readIntoSync(buffer, 0, buffer.length);

    socket.closeSync();
  } on SocketException catch (e) {
    print('Socket error: ${e.message}');
  } catch (e) {
    print('Error: $e');
  }
}

We wrap socket operations in try-catch blocks to handle potential errors. SocketException catches network-related errors specifically. Always clean up.

$ dart main.dart
Socket error: Failed host lookup: 'invalid.host'

Best Practices

Source

Dart RawSynchronousSocket Documentation

This tutorial covered Dart's RawSynchronousSocket class with practical examples showing connection, reading, writing, and error handling in synchronous mode.

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.