Skip to content

Instantly share code, notes, and snippets.

@sebnow
Created July 23, 2011 03:31
Show Gist options
  • Save sebnow/1100960 to your computer and use it in GitHub Desktop.
Save sebnow/1100960 to your computer and use it in GitHub Desktop.
Haskell ZeroMQ - Hello World!
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Monad (forever)
import Control.Concurrent (forkIO)
import qualified Data.ByteString.Char8 as B
import System.ZMQ
connect' :: String -> (Socket Req -> IO ()) -> Context -> IO ()
connect' endpoint f ctx = withSocket ctx Req (\s -> connect s endpoint >> f s)
bind' :: String -> (Socket Rep -> IO ()) -> Context -> IO ()
bind' endpoint f ctx = withSocket ctx Rep (\s -> bind s endpoint >> f s)
requester :: Socket a -> IO ()
requester s = forever $ do
B.putStrLn $ B.concat [" Sending: ", request]
send s request []
reply <- receive s []
B.putStrLn $ B.concat ["Received: ", reply]
where request = "Hello world!" :: B.ByteString
replier :: Socket a -> IO ()
replier s = forever $ do
msg <- receive s []
B.putStrLn $ B.concat ["Received: ", msg]
B.putStrLn $ B.concat ["Replying: ", reply]
send s reply []
where reply = "Hello!" :: B.ByteString
main = withContext 1 $ \ctx -> do
forkIO $ bind' "tcp://*:5555" replier ctx
connect' "tcp://localhost:5555" requester ctx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment