85c00e4ebad745f13a2e78fc745e849bbe466f73
[ghc-hetmet.git] / ghc / misc / examples / net003 / Main.hs
1 {- server
2
3 As for net001 but gets the system to allocate the next free port
4 number.  It also prints out the IP number of the peer.
5
6 TESTS:
7     getSocketName
8     inet_ntoa
9
10 -}
11
12 module Main where
13
14 import SocketPrim
15
16
17 main =
18     socket AF_INET Stream 6                     >>= \ s ->
19     bindSocket s (SockAddrInet aNY_PORT iNADDR_ANY)     >>
20     getSocketName s                             >>= \ (SockAddrInet port _) ->
21     putStr ("Allocated Port Number: " ++ show port ++ "\n") >>
22     listen s 5                                  >>
23
24
25     let 
26       loop = 
27         accept s                >>= \ (s',(SockAddrInet _ haddr)) ->
28         putStr ("*** Start of Transfer from: " ++ 
29                 (inet_ntoa haddr) ++ "***\n")   >>
30         let 
31           read_all = 
32             readSocket s' 4                     >>= \ (str, nbytes) ->
33             if nbytes /= 0 then
34                 putStr str                      >>
35                 read_all
36             else
37                 putStr "\n*** End of Transfer ***\n" >>
38                 sClose s'
39         in
40             read_all    
41     in
42         loop
43