summaryrefslogtreecommitdiff
path: root/ghc/misc/examples/net001/Main.hs
blob: 121e51d66431465132f3e6dad44f7cb448b62f1a (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
{- 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 =
    socket AF_INET Stream 6			>>= \ s ->
    bindSocket s (SockAddrInet 5000 iNADDR_ANY)	>>
    listen s 5					>>

    let 
      loop = 
	accept s				>>= \ (s',peerAddr) ->
	putStr "*** Start of Transfer ***\n"	>>
	let 
	  read_all = 
	    readSocket s' 4			>>= \ (str, nbytes) ->
	    if nbytes /= 0 then
		putStr str			>>
		read_all
	    else
		putStr "\n*** End of Transfer ***\n" >>
		sClose s'
	in
	    read_all    
    in
	loop