All pastes #388166 Raw Edit

Something

public text v1 · immutable
·published 2007-03-09 21:44 UTC
rendered paste body
module Main where

import Network
import System.IO
import Control.Concurrent

main :: IO ()
main = do putStrLn "started the server"
          sock <- listenOn (PortNumber 7654)
          mainLoop sock

mainLoop :: Socket -> IO ()
mainLoop sock =
    do socketSet <- accept sock
       forkIO (wrapHandler socketSet)
       mainLoop sock

wrapHandler :: (Handle, HostName, PortNumber) -> IO ()
wrapHandler (h, host, port) =
    do let showed = (show host) ++ ":" ++ (show port)
       putStrLn ("add conn: " ++ showed)
       hSetBuffering h NoBuffering
       handleNewSocket h
       putStrLn ("rm conn: " ++ showed)

handleNewSocket :: Handle -> IO ()
handleNewSocket sock =
    do hPutStrLn sock "What is your name?"
       name <- hGetLine sock
       hPutStrLn sock ("Hello, " ++ name ++ "!")
       hClose sock