Skip to content
This repository
tag: resourcet/0.4.4
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 46 lines (36 sloc) 1.07 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
{-# 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, killThread, threadDelay)
import Control.Monad.IO.Class (liftIO)
import Data.ByteString.Char8 ()
import Network.Socket (addrAddress, connect, sClose)

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

receiver :: IO ()
receiver = runResourceT $ src $$ CL.mapM_ (\_ -> return ())
  where
    src = bracketP
            (bindPort port (Host localhost))
            sClose
            (\sock -> sourceSocket sock 4096)

sender :: IO ()
sender = runResourceT $ CL.sourceList (repeat "abc") $$ sink
  where
    sink = bracketP
            (getSocket localhost port)
            (sClose . fst)
            (\(sock, addr) -> do
                liftIO $ connect sock (addrAddress addr)
                sinkSocket sock)

main :: IO ()
main = do
    rt <- forkIO receiver
    st <- forkIO sender

    threadDelay $ 1000 * 1000 * 5

    killThread st
    killThread rt
Something went wrong with that request. Please try again.