Skip to content

Instantly share code, notes, and snippets.

@AlexeyRaga
Forked from rhwlo/SimpleEchoServer.hs
Created May 30, 2017 10:52
Show Gist options
  • Save AlexeyRaga/8437c47dc459cb40c9d5e05668c49296 to your computer and use it in GitHub Desktop.
Save AlexeyRaga/8437c47dc459cb40c9d5e05668c49296 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