Ticket #42: socket004.hs

File socket004.hs, 0.8 kB (added by igloo, 3 years ago)
Line 
1{- client
2
3As for net002 but reads port number and message as arguments.
4It also prints out the IP number of the peer machine.
5
6
7
8TESTS:
9    getPeerName
10-}
11
12
13module Main(main) where
14
15import SocketPrim
16import System
17
18
19starbuck    = "130.209.240.81"
20marcus      = "130.209.247.2"
21
22nauru       = "130.209.247.5"           -- Linux 2.0.30 (RH-4.2) x86
23easter      = "130.209.247.6"           -- Linux 2.0.30 (RH-4.2) x86
24
25
26main =
27    getArgs                                     >>= \ [port, message] ->
28    socket AF_INET Stream 6                     >>= \ s ->
29    inet_addr easter                            >>= \ i_addr ->
30    connect s (SockAddrInet (mkPortNumber (read port)) i_addr)  >>
31
32    getPeerName s                       >>= \ (SockAddrInet p haddr) ->   
33    inet_ntoa haddr                     >>= \ a ->
34    putStr ("Connected to : " ++ a ++ "\n") >>
35    writeSocket s message                       >>
36    shutdown s ShutdownBoth                     >>
37    sClose s