Port groveler ASDF fix from CFFI
[iolib.git] / examples / ex3-client.lisp
blob189b4c30ccd16d347a99f740595edccbab07ce6f
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 example is similar to ex2-client.lisp, except we've added in
7 ;;;; catching of various conditions which may be signaled during the
8 ;;;; network communication.
10 ;; ex-0b
11 (defun run-ex3-client-helper (host port)
13 ;; Create a internet TCP socket under IPV4
14 (with-open-socket
15 (socket :connect :active
16 :address-family :internet
17 :type :stream
18 :external-format '(:utf-8 :eol-style :crlf)
19 :ipv6 nil)
21 ;; do a blocking connect to the daytime server on the port.
22 (connect socket (lookup-hostname host) :port port :wait t)
23 (format t "Connected to server ~A:~A from my local connection at ~A:~A!~%"
24 (remote-name socket) (remote-port socket)
25 (local-name socket) (local-port socket))
27 (handler-case
28 ;; read the one line of information I need from the daytime
29 ;; server. I can use read-line here because this is a TCP
30 ;; socket. It will block until the whole line is read.
31 (let ((line (read-line socket)))
32 (format t "~A" line)
35 ;; However, let's notice the signaled condition if the server
36 ;; went away prematurely...
37 (end-of-file ()
38 (format t "Got end-of-file. Server closed connection!")))))
39 ;; ex-0e
41 ;; ex-1b
42 ;; The main entry point into ex3-client
43 (defun run-ex3-client (&key (host *host*) (port *port*))
44 (handler-case
46 (run-ex3-client-helper host port)
48 ;; handle a commonly signaled error...
49 (socket-connection-refused-error ()
50 (format t "Connection refused to ~A:~A. Maybe the server isn't running?~%"
51 (lookup-hostname host) port))))
52 ;; ex-1e