| 1 | module Main where |
|---|
| 2 | import Control.Monad |
|---|
| 3 | import Control.Concurrent |
|---|
| 4 | import System.IO |
|---|
| 5 | import Control.Exception (handle) |
|---|
| 6 | import System.Posix.Signals |
|---|
| 7 | import Network(listenOn, PortID(..),accept, Socket) |
|---|
| 8 | import System.IO |
|---|
| 9 | |
|---|
| 10 | listenSock :: Socket -> IO ThreadId |
|---|
| 11 | listenSock sk = forkIO . forever $ handle (\e -> print e) $ do |
|---|
| 12 | (h,_,_) <- accept sk |
|---|
| 13 | hSetBuffering h NoBuffering |
|---|
| 14 | forkIO . forever $ hGetLine h >>= putStrLn |
|---|
| 15 | return () |
|---|
| 16 | |
|---|
| 17 | main :: IO () |
|---|
| 18 | main = do |
|---|
| 19 | installHandler sigPIPE Ignore Nothing |
|---|
| 20 | sk <- listenOn $ PortNumber 9020 |
|---|
| 21 | listenSock sk |
|---|
| 22 | forever $ threadDelay 1000000 |
|---|