echoClient.icl 1.75 KB
Newer Older
Peter Achten's avatar
Peter Achten committed
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
59
60
61
62
module echoClient

//	**************************************************************************************************
//  Clean tutorial example program.
//
//	A program to use together with echoServer
//
//	**************************************************************************************************

import	StdEnv, StdTCP, StdMaybe
from	StdSystem import ticksPerSecond

echoPort	:== 7

sendMessage	:==	"HELLO"

Start world
	#	(console, world)			= stdio world

  // get the IP address of the server
		console						= fwrites "enter address of echo server:" console
		(line, console)				= freadline console
		server						= line % (0, (size line)-2)
		(mbIPAddr, world)			= lookupIPAddress server world
	|	isNothing mbIPAddr
		#	console					= fwrites (server+++" not found\n") console
		= end console world

  // connect

	#	(tReport, mbDuplexChan, world)
									= connectTCP_MT (Just (15*ticksPerSecond))
												    (fromJust mbIPAddr, echoPort) world
	|	tReport<>TR_Success
		#!	console 				= fwrites (   server+++" does not respond on port "
											   +++toString echoPort+++"\n") console
		= end console world
	#!	console 					= fwrites	(server+++" responded on port "+++toString echoPort)
												console
		{ sChannel=sc, rChannel=rc }= fromJust mbDuplexChan

  // send something

		(sc, world)					= send (toByteSeq sendMessage) sc world

  // receive answer

		(bs, rc, world)				= receive rc world
		console						= fwrites (" with \""+++toString bs+++"\".\n") console

  // close

		world						= closeRChannel rc world
		world						= closeChannel sc world
	= end console world

end :: !*File !*World -> *World
end console world
	#!	console		= fwrites "press return to exit program" console
		(_, console)= freadline console
		(_,world)	= fclose console world
	= world