start-connection-serverPortable Socketswith-open-connectionGoTo Top

with-open-connection   (var host port) declaration* form*[Macro]

Purpose
Open a socket-stream connection to server host, perform a series of operations on the connection, and then close the connection.

Package   :portable-sockets

Module   :portable-sockets

Arguments and values

var     A variable symbol
host     A 32-bit internet address or a string specifying the remote host
port     An integer or a string specifying the service port
declaration     A declare expression (not evaluated)
forms     An implicit progn of forms to be evaluated

Description
This macro ensures that the opened connection is closed when control leaves the body of the macro.

A host string can be either a host name or a “dotted” IP address, such as "127.0.0.1".

String values available for specifying port are found in the operating system's services file and labeled as being tcp services. On Unix systems, the services file is /etc/services. On Windows, it is the file services in the Windows directory.

See also
    open-connection
    shutdown-socket-stream

Example
Open a socket connection to the GBBopen Project web server:

  > (with-open-connection (connection "GBBopen.org" 80)
       (flet ((write-crlf (stream)
                ;; HTTP requires CR/LF line termination: 
                (write-char #\return stream)
                (write-char #\linefeed stream)))
          (format connection "GET / HTTP/1.1")
          (write-crlf connection)
          (format connection "Host: ~a:~a" host port)
          (write-crlf connection)
          (write-crlf connection)
          (force-output connection)
          (let ((line (read-line connection)))
            (format t "~&;; Received: ~a~%" line))))
  ;; Received: HTTP/1.1 200 OK
  > 


The GBBopen Project


start-connection-serverPortable Socketswith-open-connectionGoTo Top