-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathserver.hs
More file actions
35 lines (32 loc) · 1.19 KB
/
server.hs
File metadata and controls
35 lines (32 loc) · 1.19 KB
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 NetworkUtils
import Control.Monad
import Control.Concurrent (forkIO)
import System.IO
import Text.Printf
import Control.Exception
-- <<main
main = withSocketsDo $ do
printf "Listening on port %d\n" port
listenOn port $ \sock -> -- <1>
forever $ -- <2>
accept sock $ \(handle, peer) -> do
printf "Accepted connection from %s\n" (show peer)
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>
-- >>