[Haskell-beginners] Network programming (select(2)) for Haskell?

Johan Tibell johan.tibell at gmail.com
Fri Sep 10 09:08:29 EDT 2010


Hi Patrick,

On Fri, Sep 10, 2010 at 2:02 PM, Patrick LeBoutillier <
patrick.leboutillier at gmail.com> wrote:

> In order to get better at network programming using haskell, I'd like
> to try and port it to Haskell, but I can't seem to locate the
> equivalent to select(2) in Haskell. Perhaps a different idiom is to be
> used?
>

GHC uses select/epoll/kqueue internally to multiplex its lightweight
threads. When you would use a select loop for handle many connections in one
language you would typically launch one thread per connection in Haskell,
using forkIO. With the upcoming version GHC can handle 100k+ threads this
way. Right now the limit is 1024 simultaneous connections due a hard coded
limit in select.

You code would look something like this:

acceptConnections serverSock = do
    sock <- accept severSock
    forkIO $ handleConnection sock
    acceptConnections serverSock

handleConnection sock = do
    msg <- recv
    send ...
    sClose sock

I'd recommend using the network-bytestring library which provides more
efficient versions of e.g. recv and send.

Cheers,
Johan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100910/667bebae/attachment.html


More information about the Beginners mailing list