Mark ENOLINK and EMULTIHOP as optional
[iolib.git] / examples / ex4-client.lisp
blob7252292cc45dc6552f9594cd5f9a2a9d043a3194
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.
12 ;; ex-0b
13 (defun run-ex4-client-helper (host port)
15 ;; Create a internet TCP socket under IPV4
16 (with-open-socket
17 (socket :connect :active
18 :address-family :internet
19 :type :stream
20 :external-format '(:utf-8 :eol-style :crlf)
21 :ipv6 nil)
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))
30 (handler-case
31 (ex4-str-cli socket)
33 (socket-connection-reset-error ()
34 (format t "Got connection reset. Server went away!"))
36 (hangup ()
37 (format t "Got hangup. Server closed connection on write!~%"))
39 (end-of-file ()
40 (format t "Got end-of-file. Server closed connection on read!~%")))))
41 ;; ex-0e
43 ;; ex-1b
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)
48 (loop
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)))))
54 ;; ex-1e
56 ;; ex-2b
57 ;; This is the entry point into this example
58 (defun run-ex4-client (&key (host *host*) (port *port*))
59 (unwind-protect
60 (handler-case
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)))
69 ;; Cleanup form
70 (format t "Client Exited.~%")))
71 ;; ex-2e