1 (in-package :iolib.examples
)
3 ;;;; This file was originally written by Peter Keller (psilord@cs.wisc.edu)
4 ;;;; and this code is released under the same license as IOLib.
6 ;;;; This program is a very simple echo client. After connecting to
7 ;;;; the server it reads a line from the console, echos it to the
8 ;;;; server, reads the response back, then echos it to
9 ;;;; *standard-output*. We handle common conditions. Type "quit" on a
10 ;;;; line by itself to exit the client.
13 (defun run-ex4-client-helper (host port
)
15 ;; Create a internet TCP socket under IPV4
17 (socket :connect
:active
18 :address-family
:internet
20 :external-format
'(:utf-8
:eol-style
:crlf
)
23 ;; do a blocking connect to the daytime server on the port.
24 (connect socket
(lookup-hostname host
) :port port
:wait t
)
26 (format t
"Connected to server ~A:~A from my local connection at ~A:~A!~%"
27 (remote-host socket
) (remote-port socket
)
28 (local-host socket
) (local-port socket
))
33 (socket-connection-reset-error ()
34 (format t
"Got connection reset. Server went away!"))
37 (format t
"Got hangup. Server closed connection on write!~%"))
40 (format t
"Got end-of-file. Server closed connection on read!~%")))))
44 ;; read a line from stdin, write it to the server, read the response, write
45 ;; it to stdout. If we read 'quit' then echo it to the server which will
46 ;; echo it back to us and then close its connection to us.
47 (defun ex4-str-cli (socket)
49 (let ((line (read-line)))
50 ;; send it to the server, get the response.
51 (format socket
"~A~%" line
)
52 (finish-output socket
)
53 (format t
"~A~%" (read-line socket
)))))
57 ;; This is the entry point into this example
58 (defun run-ex4-client (&key
(host *host
*) (port *port
*))
62 (run-ex4-client-helper host port
)
64 ;; handle a commonly signaled error...
65 (socket-connection-refused-error ()
66 (format t
"Connection refused to ~A:~A. Maybe the server isn't running?~%"
67 (lookup-hostname host
) port
)))
70 (format t
"Client Exited.~%")))