Created
August 6, 2012 17:45
-
-
Save billdozr/3277051 to your computer and use it in GitHub Desktop.
Ping server
This file contains 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
import System.Environment (getArgs, getProgName) | |
import Control.Concurrent (forkIO) | |
import Network (Socket, PortID(PortNumber), withSocketsDo, | |
listenOn, accept) | |
import System.IO (hPutStrLn, hGetLine, hFlush) | |
import Control.Monad (forever) | |
main = withSocketsDo $ do | |
args <- getArgs | |
prog <- getProgName | |
case args of | |
[port] -> do | |
socket <- listenOn $ PortNumber (fromIntegral (read port :: Int)) | |
putStrLn $ "Listening for pings on " ++ port | |
handleRequest socket | |
_ -> | |
putStrLn $ "usage: " ++ prog ++ " <port>" | |
handleRequest s = do | |
(handle, _, _) <- accept s | |
forkIO $ | |
forever $ do | |
input <- hGetLine handle | |
processPing (hPutStrLn handle) (init input) | |
hFlush handle | |
handleRequest s | |
processPing f msg = do | |
case msg of | |
"ping" -> f "pong!" | |
_ -> f "Did you mean `ping'?" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment