Skip to content

Commit

Permalink
Add udpflow sample application
Browse files Browse the repository at this point in the history
This is a very simple application which forks 2 threads, one sending
messages to a port, another one receiving (and discarding) them.

The application should be built using the threaded RTS. Consider using
2 capabilities upon execution (+RTS -N2).
  • Loading branch information
NicolasT committed Aug 28, 2012
1 parent 7ec7921 commit 1c16960
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions network-conduit/udpflow.hs
@@ -0,0 +1,34 @@
{-# LANGUAGE OverloadedStrings #-}

-- Build using threaded RTS

import Data.Conduit
import Data.Conduit.Network.UDP
import qualified Data.Conduit.List as CL

import Control.Concurrent (forkIO, threadDelay)
import Control.Monad (forever)
import Data.ByteString.Char8 ()
import Network.Socket (addrAddress)

localhost :: String
localhost = "127.0.0.1"
port :: Int
port = 4000

receiver :: IO ()
receiver = do
sock <- bindPort port (Host localhost)
sourceSocket sock 4096 $$ CL.mapM_ (\_ -> return ())

sender :: IO ()
sender = do
(sock, addr) <- getSocket localhost port
CL.sourceList (repeat ("abc", addrAddress addr)) $$ sinkSocket sock

main :: IO ()
main = do
forkIO receiver
forkIO sender

forever $ threadDelay $ 1000 * 1000 * 10

0 comments on commit 1c16960

Please sign in to comment.