Ticket #42: socket001.hs

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