ZetCode

Dart SocketDirection

last modified October 18, 2026

The SocketDirection class in Dart represents the direction of socket communication. It's used with socket shutdown operations to specify whether to close reading, writing, or both directions.

SocketDirection is a final class with static constants in Dart's dart:io library. It's essential for proper socket resource management in network applications.

Basic Definition

SocketDirection defines three constants: receive, send, and both. These represent the possible communication directions for a socket.

The class is used with RawSocket.shutdown and RawSynchronousSocket.shutdown to control which direction of communication should be terminated on a socket.

Basic SocketDirection Usage

This example shows basic usage of SocketDirection with a simple TCP socket.

main.dart
import 'dart:io';

void main() async {
  var server = await RawServerSocket.bind('127.0.0.1', 0);
  var client = await RawSocket.connect('127.0.0.1', server.port);

  // Shutdown only the send direction
  client.shutdown(SocketDirection.send);

  print('Send direction shutdown');
  await client.close();
  await server.close();
}

We create a server and client socket, then shutdown only the send direction on the client. The server can still send data to the client after this call.

$ dart main.dart
Send direction shutdown

Shutting Down Receive Direction

This example demonstrates shutting down the receive direction of a socket.

main.dart
import 'dart:io';

void main() async {
  var server = await RawServerSocket.bind('127.0.0.1', 0);
  var client = await RawSocket.connect('127.0.0.1', server.port);

  // Shutdown only the receive direction
  client.shutdown(SocketDirection.receive);

  print('Receive direction shutdown');
  await client.close();
  await server.close();
}

After shutting down the receive direction, the client can still send data but won't receive any incoming data. This is useful for one-way communication.

$ dart main.dart
Receive direction shutdown

Shutting Down Both Directions

This example shows how to completely shutdown a socket in both directions.

main.dart
import 'dart:io';

void main() async {
  var server = await RawServerSocket.bind('127.0.0.1', 0);
  var client = await RawSocket.connect('127.0.0.1', server.port);

  // Shutdown both directions
  client.shutdown(SocketDirection.both);

  print('Both directions shutdown');
  await client.close();
  await server.close();
}

Using SocketDirection.both shuts down both directions on the socket. Call RawSocket.close() afterwards to release the socket.

$ dart main.dart
Both directions shutdown

Graceful Socket Termination

This example demonstrates graceful socket termination using SocketDirection.

main.dart
import 'dart:io';

void main() async {
  var server = await RawServerSocket.bind('127.0.0.1', 0);
  var client = await RawSocket.connect('127.0.0.1', server.port);

  // First shutdown sending to indicate we're done writing
  client.shutdown(SocketDirection.send);

  // Accept and close the pending server-side connection
  var serverClient = await server.first;
  await serverClient.close();

  // Finally shutdown completely
  client.shutdown(SocketDirection.both);
  await client.close();
  await server.close();

  print('Graceful termination complete');
}

This pattern stops sending first, accepts the pending server-side connection, and then closes both directions. It gives the peer a chance to finish before the socket is released.

$ dart main.dart
Graceful termination complete

Error Handling with SocketDirection

This example shows proper error handling when using SocketDirection.

main.dart
import 'dart:io';

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

    // Attempt to shutdown a direction
    socket.shutdown(SocketDirection.send);

    print('Shutdown successful');
    await socket.close();
  } on SocketException catch (e) {
    print('Socket error: ${e.message}');
  } catch (e) {
    print('Unexpected error: $e');
  }
}

We wrap socket operations in try-catch blocks to handle potential errors. shutdown itself never throws, but the connection attempt can fail with a SocketException.

$ dart main.dart
Shutdown successful

Best Practices

Source

Dart SocketDirection Documentation

This tutorial covered Dart's SocketDirection class with practical examples showing how to control socket communication directions for proper network programming.

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.