Ticket #42: socket001.hs
| File socket001.hs, 1.1 kB (added by igloo, 18 months ago) |
|---|
| Line | |
|---|---|
| 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 | main = 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 |
