[Haskell-cafe] non-blocking recv from UDP socket

Vo Minh Thu noteed at gmail.com
Thu Aug 26 05:13:44 EDT 2010


Hi,

Is is possible to get Network.Socket.ByteString.recv to be
non-blocking (i.e. return directly even if no data is available) ?

I have tried ti use

  setSocketOption sock NoDelay 1

but then I get the following error:

  setSocketOption: unsupported operation (Protocol not available)

Here is the code if it may help (I'm on linux btw).

  sock <- socket AF_INET Datagram 17 -- 17 is IPPROTO_UDP
  addr <- inet_addr "127.0.0.1"
  bindSocket sock (SockAddrInet 44004 addr)
  setSocketOption sock NoDelay 1 -- <-- doesn't seem to work

  addr <- inet_addr "127.0.0.1"
  forever $ do
    sent <- sendTo sock "hello" (SockAddrInet 44005 addr)
    dat <- recv sock 5  -- <-- I'd like it to be non-blocking
    B.putStrLn dat
    threadDelay 1000000 -- 1 second

Thanks,
Thu


More information about the Haskell-Cafe mailing list