Skip to content

Instantly share code, notes, and snippets.

@MgaMPKAy
Created March 27, 2012 15:20
Show Gist options
  • Save MgaMPKAy/2216866 to your computer and use it in GitHub Desktop.
Save MgaMPKAy/2216866 to your computer and use it in GitHub Desktop.
Implement simple echo server and client with pipes-network
{-# LANGUAGE PackageImports #-}
module Main where
import "pipes-core" Control.Pipe
import Control.Pipe.Network
import Control.Monad
import Control.Monad.Trans
import Control.Concurrent (forkIO)
import qualified Data.ByteString.Char8 as B
clientSettings :: ClientSettings
clientSettings = ClientSettings 8080 "0.0.0.0"
client :: Application IO ()
client reader writer = do
forkIO $ runPipe $ prompt >+> writer
runPipe $ reader >+> printer'
printer' :: Consumer B.ByteString IO ()
printer' = forever $ do
x <- await
lift $ B.putStrLn x
prompt :: Producer B.ByteString IO ()
prompt = forever $ do
x <- lift B.getLine
yield x
main :: IO ()
main = runTCPClient clientSettings client
{-# LANGUAGE PackageImports #-}
module Main where
import "pipes-core" Control.Pipe
import Control.Pipe.Network
serverSettings :: ServerSettings
serverSettings = ServerSettings 8080 Nothing
app :: Application IO ()
app reader writer = runPipe $ reader >+> writer
main :: IO ()
main = runTCPServer serverSettings app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment