-
Notifications
You must be signed in to change notification settings - Fork 104
/
server.hs
35 lines (32 loc) · 1.2 KB
/
server.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import ConcurrentUtils
import Network
import Control.Monad
import Control.Concurrent (forkIO)
import System.IO
import Text.Printf
import Control.Exception
-- <<main
main = withSocketsDo $ do
sock <- listenOn (PortNumber (fromIntegral port)) -- <1>
printf "Listening on port %d\n" port
forever $ do -- <2>
(handle, host, port) <- accept sock -- <3>
printf "Accepted connection from %s: %s\n" host (show port)
forkFinally (talk handle) (\_ -> hClose handle) -- <4>
port :: Int
port = 44444
-- >>
-- <<talk
talk :: Handle -> IO ()
talk h = do
hSetBuffering h LineBuffering -- <1>
loop -- <2>
where
loop = do
line <- hGetLine h -- <3>
if line == "end" -- <4>
then hPutStrLn h ("Thank you for using the " ++ -- <5>
"Haskell doubling service.")
else do hPutStrLn h (show (2 * (read line :: Integer))) -- <6>
loop -- <7>
-- >>