File par/2-clockd/server.d from the latest check-in


import std.concurrency : spawn;
import std.datetime : Clock, seconds;
import std.socket;
import core.thread : Thread;

void main() {
    import std.stdio : writeln;

    auto listener = new TcpSocket;
    listener.setOption(SocketOptionLevel.SOCKET, SocketOption.REUSEADDR, 1);
    listener.bind(new InternetAddress(8000));
    listener.listen(10);
    writeln("Listening on port 8000");

    while (true) {
        auto client = listener.accept;
        spawn(&handleConn, cast(shared(Socket)) client);
    }
}

void handleConn(shared(Socket) socket) {
    import std.format : format;

    auto client = cast(Socket) socket;
    scope (exit)
        client.close;
    while (true) {
        if (0 >= client.send(format!"%s\n"(Clock.currTime)))
            break;
        Thread.sleep(1.seconds);
    }
}