Skip to content

Instantly share code, notes, and snippets.

@rhwlo
Created August 22, 2015 22:18
Show Gist options
  • Save rhwlo/9c908d5410efd439e17f to your computer and use it in GitHub Desktop.
Save rhwlo/9c908d5410efd439e17f to your computer and use it in GitHub Desktop.
simple echo server in Haskell
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