Next Previous Up Top Contents Index

3 The COMM package

open-tcp-stream

Function

Summary

Attempts to connect to a socket on another machine and returns a stream object for the connection.
Package

comm

Signature

open-tcp-stream hostname service &key direction element-type buffered => stream-object

Arguments

hostname

An integer or string.

service

A string or a fixnum.

direction

One of:input,:output or:io.

element-type

One ofbase-char,unsigned-byte orsigned-byte.

errorp

A boolean.

Values

stream-object

A socket stream.

Description

Theopen-tcp-stream function attempts to connect to a socket on another machine and returns stream-object for the connection if successful. The server machine to connect to is given by hostname, which can be one of the following:
  • A string naming the host, for example"www.nowhere.com"
  • A string providing the IP address, for example"204.71.177.75"
  • An integer IP address in network order, for example#xCC47B14B
The name of the service to provide is given by service. If service is a string, the location of the file specifying the names of the services available varies, but typically on Windows 95 it is calledSERVICES and is stored in theWindows directory., and on Windows NT it is the file
%SystemRoot%\system32\drivers\etc\SERVICES

The service can also be a fixnum representing the port number of the desired connection.
The direction of the connection is given by connection. Its default value is:io. The element type of the connection is given by element-type, and isbase-char by default.
If errorp isnil, failure to connect returnsnil, otherwise an error is signaled.
Example

The following example opens an HTTP connection to a given host, and retrieves the root page:
(with-open-stream (http (comm:open-tcp-stream 
                         "webhost" 80))
  (format http "GET / HTTP/1.0~C~C~C~C"
               (code-char 13) (code-char 10)
               (code-char 13) (code-char 10))
  (force-output http)
  (write-string "Waiting to reply...")
  (loop for ch = (read-char-no-hang http nil :eof)
        until ch
        do (write-char #\.)
           (sleep 0.25)
        finally (unless (eq ch :eof)
                  (unread-char ch http))) 
  (terpri)
  (loop for line = (read-line http nil nil)
        while line
        do (write-line line)))

See also

start-up-server

LispWorks Reference Manual - 15 Oct 1998

Next Previous Up Top Contents Index

Generated with Harlequin WebMaker