fbc9ff04e02226e0debea9410443e260c80b1130
[ghc-hetmet.git] / ghc / misc / examples / net007 / Main.hs
1 {- server
2
3 As net003 but uses the BSD module for portability.  Also prints the
4 common name of the host rather than its IP number.
5
6 TESTS:
7     getProtocolNumber
8     getSocketName
9     getHostByAddr
10
11 -}
12
13 module Main where
14
15 import BSD
16 import SocketPrim
17
18 main =
19     getProtocolNumber "tcp"                     >>= \ proto ->
20     socket AF_INET Stream proto                 >>= \ s ->
21     bindSocket s (SockAddrInet aNY_PORT iNADDR_ANY)     >>
22     getSocketName s                             >>= \ (SockAddrInet port _) ->
23     putStr ("Allocated Port Number: " ++ show port ++ "\n") >>
24     listen s 5                                  >>
25
26
27     let 
28       loop = 
29         accept s                    >>= \ (s',(SockAddrInet _ haddr)) ->
30         getHostByAddr AF_INET haddr             >>= \ (HostEntry hname _ _ _) ->
31         putStr ("*** Start of Transfer from: " ++ hname ++ "***\n")     >>
32         let 
33           read_all = 
34             readSocket s' 4                     >>= \ (str, nbytes) ->
35             if nbytes /= 0 then
36                 putStr str                      >>
37                 read_all
38             else
39                 putStr "\n*** End of Transfer ***\n" >>
40                 sClose s'
41         in
42             read_all    
43     in
44         loop