summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib/socket/socket001.hs
blob: 0af7afa0d7d1df6fe66ad876ce7003dc2bad67c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{- server

The purpose of this test driver is to test TCP Stream sockets.
All values have been hard coded since the BSD library is not used to
query the databases for the values.  In therory this code is thus not
portable but net007/Main.hs provides a portable version using the BSD
module.

This creates a stream socket bound to port 5000 and waits for incoming
messages it then reads all available data before closing the
connection to that peer.

No form of error checking is provided other than that already provided
by module SocketPrim.


TESTS:
    socket
    bindSocket
    listen
    accept
    readSocket
    sClose

-}


module Main where

import SocketPrim

main = do
    s <- socket AF_INET Stream 6
    bindSocket s (SockAddrInet (fromIntegral 5000) iNADDR_ANY)
    listen s 5

    let 
      loop = 
	accept s				>>= \ (s',peerAddr) ->
	putStr "*** Start of Transfer ***\n"	>>
	h <- socketToHandle s'
	let 
	  read_all = 
	    b <- hEOF h
	    c <- hGetChar h
	    putChar c

	    if nbytes /= 0 then
		putStr str			>>
		read_all
	    else
		putStr "\n*** End of Transfer ***\n" >>
		sClose s'
	in
	    read_all    

    loop