Ticket #2703: BigSock.hs

File BigSock.hs, 0.8 KB (added by sclv, 5 years ago)

Successful case

Line 
1module Main where
2import Control.Monad
3import Control.Concurrent
4import System.IO
5import Control.Applicative
6import Control.Exception (handle)
7import System.Posix.Signals
8import Network(listenOn, PortID(..))
9import Network.Socket
10import System.IO
11import System.IO.Error (isEOFError)
12
13sGetLine :: Socket -> IO String
14sGetLine s = reverse <$> go []
15    where go xs = do
16            (x,n) <- recvLen s 1
17            if x == "\n" || n <= 0
18               then return xs
19               else go $ x++xs
20
21listenSock :: Socket -> IO ThreadId
22listenSock sk = forkIO . forever $ handle (\e -> print e) $ do
23           (resp,_) <- accept sk
24           forkIO . forever $ sGetLine resp >>= putStrLn
25           return ()
26
27main :: IO ()
28main = do
29  installHandler sigPIPE Ignore Nothing
30  sk <- listenOn $ PortNumber 9020
31  listenSock sk
32  forever $ threadDelay 1000000