{- Compile this file with ghc -o talk1 -package concurrent -package network Talk1.hs -} module Main where import Concurrent import Network import IO import System (getArgs) import Char(isControl,ord) main :: IO () main = do portNum <- getArgs >>= return . read . head s <- listenOn (PortNumber (fromInteger portNum)) ch<- newChan loop s ch loop :: Socket -> Chan String -> IO () loop s ch = do (handle, wh, p) <- accept s hSetBuffering handle NoBuffering putStrLn ("New connection from "++ wh++ " on "++ show p) ch2 <- dupChan ch forkIO (newUser handle ch2) loop s ch newUser :: Handle-> Chan String -> IO () newUser socket msgch = forkIO read >> write where read :: IO () read = hGetLine socket >>= writeChan msgch >> read write :: IO () write = readChan msgch >>= hPutStrLn socket >> write