-
-
Save AlexeyRaga/8437c47dc459cb40c9d5e05668c49296 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