TCP Server in Dart

Video tutorial here!

With the advent of Node.js and HHVM, more and more web developers are becoming interested in using interpreted languages to power their applications. Google Dart is a relatively new language. It was originally intended to compile to client-side Javascript, and it still does. However, its real strength is on the server. The Dart VM allows us to write powerful servers as command-line apps for less cost than a Windows server would, and with a strong language with similar features to C# or Java.

#TCP Server This brings us to today’s objective, which is implementing perhaps the simplest server of them all: a TCP server. We’ll write a simple script Dart that:

  • Listens on a port for incoming connections
  • Prints incoming messages to the screen

So, let’s get started.

#Setup For this tutorial, you will need to install the Dart SDK.

I also recommend using JetBrains WebStorm as your IDE, but that’s not a requirement. You can write your code in even Notepad.

Visual Studio Code and Brackets are also quite good.

Now that we have our editor of choice, we need to set up our package structure.

mkdir dart-tcp-server
cd dart-tcp-server
mkdir bin

The bin/ directory will hold our “executable” script.

#The Code So, let’s get started.

##Listening on a Port

Create a new main.dart file within bin/.

Add the following code.

import 'dart:core';
import 'dart:async';
import 'dart:io';

main() {
  Future<ServerSocket> fServer = ServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 3406);
}

What did we just do?

  1. Each import call exposes code for us to use within our script. dart:async allows us to perform asynchronous actions, and dart:io allows us to work with sockets and I/O.
  2. We bound a socket to port 3406. This socket will listen for incoming connections.
  3. Our socket was implemented as a Future<>. Futures in Dart are similar to Promises in Javascript. They allow us to receive the results of an asynchronous action through a callback.

##Server Socket Callback Now, in your main function, just after your call to ServerSocket.bind, add this code:

fServer.then((ServerSocket server) {
	server.listen(handleIncoming);
});
  1. Once our socket is bound to localhost:3406, we can run a callback on it that accepts a ServerSocket as a parameter.
  2. We tell our server to listen for incoming connections, and then handle them with a callback called handleIncoming, which we are about to write.

##Handling Incoming Connections

Now, add the following code to your file:

handleIncoming(Socket socket) {
  socket.listen((List<int> data) {
    String message = new String.fromCharCodes(data);
    print(message);
  });
}
  1. Our socket listens for incoming data.
  2. When this data is received, you can run a callback that accepts a List. Each of these ints represents a byte.
  3. Now, we just have to convert the bytes to a string, and print to the console.

#Testing Now, we’ve written our functional TCP server. We just need to see it in action.

To start our server, simply:

cd bin
dart main.dart

Windows users can look into PacketSender for a GUI-based TCP client.

On Unix-based systems, you can use netcat to easily connect to our server through TCP.

nc 127.0.0.1 3406
Hello Dart

Our server will print Hello Dart to the screen!

#What we’ve Done Good job! You’ve written a Dart server that:

  • Listens on a TCP port
  • Receives incoming connections
  • Prints messages to the console

Stay tuned for more tutorials!

Here is a link to the video version of this tutorial.

Follow me on Twitter: http://twitter.com/thosakwe

Comment below!