Created
August 22, 2015 22:18
-
-
Save rhwlo/9c908d5410efd439e17f to your computer and use it in GitHub Desktop.
simple echo server in Haskell
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 GHC.IO.Handle (Handle, hGetLine) | |
import GHC.IO.Handle.FD (stdout) | |
import Network | |
import Text.Printf | |
main :: IO () | |
main = withSocketsDo $ do | |
listenSock <- listenOn $ PortNumber 9999 | |
(listenHandle, clientHost, clientPort) <- accept listenSock | |
hPrintf stdout "Accepted connection from %s:%s\n" clientHost (show clientPort) | |
hPrintf listenHandle "Hello %s:%s!\n" clientHost (show clientPort) | |
loopOn listenHandle | |
where | |
loopOn :: Handle -> IO () | |
loopOn someHandle = do | |
line <- hGetLine someHandle | |
hPrintf stdout "Received %s\n" line | |
hPrintf someHandle "backwards: %s\n" (reverse line) | |
loopOn someHandle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment