[Haskell-cafe] Multicast receiver thread fails to receive last few messages

Ben Gunton ben.gunton at gmail.com
Tue Sep 23 15:14:09 UTC 2014


I am trying to send multicast messages from one thread, and receive them on
another. The sending thread reads lines from a file and sends them.
However, the receiving thread does not receive all the messages - it seems
to miss the last 10 message or so. If I run other programs to listen for
the multicast message, they receive them fine. So I think the issue is
something to do with receiving, not sending. I keep the program alive with
a threadDelay, so it shouldn't be halting prematurely. If I add a small
delay after each send, it works fine. And if I make the input file smaller,
it works fine.

On the provided data file, on my system (64-bit Ubuntu, GHC 7.8.2), the
receiver fails to receive lines 125-140. Compiled with no optimizations.

Thanks for any help!

The relevant code snippets are below, and the sample data file and full
program are attached.

    -- Loop to receive all the packets
    let receiveMulticast = do
            (msg, _) <- recvFrom recvSock 32628
            putStrLn . BS.unpack $ BS.take 100 msg
            receiveMulticast
    _ <- forkIO receiveMulticast

    -- Send every line from a file as multicast message
    inputFile <- openFile "data" ReadMode
    fileLines <- BS.lines <$> BS.hGetContents inputFile
    let sendMulticast msg = do
            sendTo sendSock msg addr
            -- Receiver FAILS to receive last few messages unless this
            -- thread delay exists... why?!
            -- threadDelay (1)
    mapM_ sendMulticast fileLines
    hClose inputFile

    threadDelay (1000*1000*1000) -- Delay for 1000 seconds
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140923/c220dbf7/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: data
Type: application/octet-stream
Size: 72591 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140923/c220dbf7/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Main.hs
Type: text/x-haskell
Size: 2237 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140923/c220dbf7/attachment-0001.hs>


More information about the Haskell-Cafe mailing list