Created
April 19, 2016 14:14
-
-
Save notogawa/99a7e79e17b3fdfcbc4eab262137665d to your computer and use it in GitHub Desktop.
Hello,World!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Network ( withSocketsDo, listenOn, PortID(PortNumber), connectTo ) | |
import Network.Socket ( accept, send, close, recvLen ) | |
import Control.Concurrent ( forkIO, threadDelay ) | |
import System.IO ( hClose ) | |
import Control.Monad ( void ) | |
import Control.Exception ( bracket ) | |
main :: IO () | |
main = withSocketsDo $ do | |
let port = PortNumber $ toEnum 3000 | |
let wait = void . forkIO . bracket (connectTo "localhost" port) hClose . const . threadDelay | |
bracket (listenOn port) close $ \lis -> do | |
wait 3000000 | |
bracket (accept lis) (close . fst) $ \(sock, _) -> | |
void $ forkIO $ do | |
threadDelay 1000000 | |
void $ send sock "Hello,World!" | |
wait 2000000 | |
bracket (accept lis) (close . fst) $ \(sock, _) -> | |
recvLen sock 1024 >>= putStrLn . fst |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment