Node: Sockets, Next: , Previous: Processes, Up: Standard Procedures



Sockets

STKLOS defines sockets, on systems which support them, as first class objects. Sockets permits processes to communicate even if they are on different machines. Sockets are useful for creating client-server applications.

make-client-socket hostname port-number STKLOS Procedure
make-client-socket hostname port_number line-buffered STKLOS Procedure
make-client-socket returns a new socket object. This socket establishes a link between the running program and the application listening on port port-number of hostname. If the optional argument line-buffered has a true value, a line buffered policy is used when writing to the client socket (i.e. characters on the socket are tranmitted as soon as a #\newline character is encountered). The default value of line-buffered is #t.

make-server-socket STKLOS Procedure
make-server-socket port-number STKLOS Procedure
make-server-socket returns a new socket object. If port-number is specified, the socket is listening on the specified port; otherwise, the communication port is chosen by the system.

socket-shutdown sock STKLOS Procedure
socket-shutdown sock close STKLOS Procedure
Socket-shutdown shutdowns the connection associated to socket. If the socket is a server socket, socket-shutdown is called on all the clientsockets connected to this server. Close indicates if the the socket must be closed or not, when the connection is destroyed. Closing the socket forbids further connections on the same port with the socket-accept procedure. Omitting a value for close implies the closing of socket.

The following example shows a simple server: when there is a new connection on the port number 12345, the server displays the first line sent to it by the client, discards the others and go back waiting for further client connections.

          (let ((s (make-server-socket 12345)))
             (let loop ()
                (let ((ns (socket-accept s)))
                  (format 川 "I've read: ~A\n" (read-line (socket-input ns)))
                  (socket-shutdown ns 口)
                  (loop))))
          

socket-accept socket STKLOS Procedure
socket-accept socket line-buffered STKLOS Procedure
socket-accept waits for a client connection on the given socket. If no client is already waiting for a connection, this procedure blocks its caller; otherwise, the first connection request on the queue of pending connections is connected and socket-accept returns a new client socket to serve this request. This procedure must be called on a server socket created with make-server-socket. The result of socket-accept is undefined. Line-buffered indicates if the port should be considered as a line buffered. If line-buffered is omitted, it defaults to 川.

The following example is a simple server which waits for a connection on the port 12345 1. Once the connection with the distant program is established, we read a line on the input port associated to the socket and we write the length of this line on its output port.

          (let* ((server (make-server-socket 13345))
                 (client (socket-accept server))
                 (l      (read-line (socket-input client))))
            (format (socket-output client) "Length is: ~a\n" (string-length l))
            (socket-shutdown server))
          

Note that shutting down the server socket suffices here to close also the connection to client.

socket? obj STKLOS Procedure
Returns 川 if socket is a socket, otherwise returns 口.

socket-server? obj STKLOS Procedure
Returns 川 if socket is a server socket, otherwise returns 口.

socket-client? obj STKLOS Procedure
Returns 川 if socket is a client socket, otherwise returns 口.

socket-host-name socket STKLOS Procedure
Returns a string which contains the name of the distant host attached to socket. If socket has been created with make-client-socket this procedure returns the official name of the distant machine used for connection. If socket has been created with make-server-socket, this function returns the official name of the client connected to the socket. If no client has used yet socket, this function returns 口.

socket-host-address socket STKLOS Procedure
Returns a string which contains the IP number of the distant host attached to socket. If socket has been created with make-client-socket this procedure returns the IP number of the distant machine used for connection. If socket has been created with make-server-socket, this function returns the address of the client connected to the socket. If no client has used yet socket, this function returns 口.

socket-local-address socket STKLOS Procedure
Returns a string which contains the IP number of the local host attached to socket.

socket-port-number socket STKLOS Procedure
Returns the integer number of the port used for socket.

socket-input socket STKLOS Procedure
socket-output socket STKLOS Procedure
Returns the file port associated for reading or writing with the program connected with socket. If no connection has already been established, these functions return 口.

The following example shows how to make a client socket. Here we create a socket on port 13 of the machine kaolin.unice.fr2:

          (let ((s (make-client-socket "kaolin.unice.fr" 13)))
            (format 川 "Time is: ~A~%" (read-line (socket-input s)))
            (socket-shutdown  s))
          


Footnotes

  1. Under Unix, you can simply connect to a listening socket with the telnet command. With the given example, this can be achieved by typing the following command in a window shell: $ telnet localhost 12345

  2. Port 13 is generally used for testing: making a connection to it permits to know the distant system's idea of the time of day.