[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / misc / examples / net001 / Main.hs
1 {- server
2
3 The purpose of this test driver is to test TCP Stream sockets.
4 All values have been hard coded since the BSD library is not used to
5 query the databases for the values.  In therory this code is thus not
6 portable but net007/Main.hs provides a portable version using the BSD
7 module.
8
9 This creates a stream socket bound to port 5000 and waits for incoming
10 messages it then reads all available data before closing the
11 connection to that peer.
12
13 No form of error checking is provided other than that already provided
14 by module SocketPrim.
15
16
17 TESTS:
18     socket
19     bindSocket
20     listen
21     accept
22     readSocket
23     sClose
24
25 -}
26
27
28 module Main where
29
30 import SocketPrim
31
32
33 main =
34     socket AF_INET Stream 6                     >>= \ s ->
35     bindSocket s (SockAddrInet 5000 iNADDR_ANY) >>
36     listen s 5                                  >>
37
38     let 
39       loop = 
40         accept s                                >>= \ (s',peerAddr) ->
41         putStr "*** Start of Transfer ***\n"    >>
42         let 
43           read_all = 
44             readSocket s' 4                     >>= \ (str, nbytes) ->
45             if nbytes /= 0 then
46                 putStr str                      >>
47                 read_all
48             else
49                 putStr "\n*** End of Transfer ***\n" >>
50                 sClose s'
51         in
52             read_all    
53     in
54         loop
55