Playing with sockets in Haskell

This is another one of those posts that I make mostly for myself, you know for organising and help my memory :-)

There are as far as I can see three ways to deal with sockets in Haskell. There’s the type Socket which is used throughout Network.Socket. From that it’s possible to get to the underlying filedescriptor, and it in turn can be converted to a Handle.

When coupled with fork+exec it’s crucial to make sure the child process can find the socket Leaving it in a predictable place seems to be the easiest way to do that, and as far as I can see that requires using dupTo from System.Posix.IO. So, on the child-side it’s necessary to find a way to turn an integer (CInt) into something that can be treated as a socket (i.e. a Socket, a Handle, or a filedescriptor).

A basic parent-child which obviously won’t work since the child’s socket is represented as a Socket:

Let the child take a CInt and turn it into a filedescriptor:

Let the child take a CInt and turn it into a Handle:

Let the child take a CInt and turn it into a Socket:1

  1. It seems the socket is in the Connected state after socketPair succeeds.


I’m curious what situation is requiring you to use forkProcess instead of the more flexible forkIO and letting the RTS figure things out. Are there some TLS issues or is this just hypothetical?


@TomMD, it’s simple, I need a new process not a new thread.

Since I’m only playing around with this at the moment I didn’t bother putting childFunc in a separate executable, and hence also left out the call to executeFile.

Leave a comment