[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.11.2 High-level network functions

Builtin Class: <socket>
Abstracts a socket, a communication endpoint.

For a connection-oriented socket, you can access the communication channel by two ports associated to the socket, one for input and another for output. socket-input-port and socket-output-port returns those ports, respectively.

The following two functions are convenient ways to create a connection-oriented socket. Those functions are to provide an easy methods for typical cases, but have less control. If you need more than these functions provide, use low-level interface.

Function: make-client-socket &optional address-spec ...
Creates and returns a client socket, connected to the address specified by address-spec ....

(make-client-socket 'unix path)
The client socket is connected to the unix domain server socket of addreess path.
(make-client-socket 'inet host port)
The client socket is connected to the inet domain server socket with hostname host and port port. TCP protocol is assumed. host can be either a dotted decimal notation of IPv4 address, or a hostname. port must be an exact integer.
(make-client-socket host port)
This works the same as above. This form is for compatibility with STk.

This function raises an error if it cannot create a socket, or cannot connect to the specified address.

 
(make-client-socket 'inet "www.w3.com" 80)
  => ;a socket connected to www.w3.com, port 80
(make-client-socket "127.0.0.1" 23)
  => ;a socket connected to localhost, port 23
(make-client-socket 'unix "/tmp/.sock"
  => ;a socket connected to a unix domain socket "/tmp/.sock"

Function: make-server-socket &optional address-spec ...
Creates and returns a server socket, listening the address specified by address-spec.

(make-server-socket 'unix path)
The socket is bound to a unix domain socket with a name path.
(make-server-socket 'inet port [:reuse-addr? flag])
The socket is bound to an inet domain TCP socket, listening port port, which must be a non-negative exact integer. If port is zero, the system assigns one of available port numbers. If a keyword argument reuse-addr? is given and true, SO_REUSEADDR option is set to the socket before bound to the port. This allows the process to bind the server socket immediately after other process releases the port.
(make-server-socket port [:reuse-addr? flag])
This is a synonym to the above form. This form is backward-compatible with STk's make-server-socket.

 
(make-server-socket 'inet 8080)
  => #<socket (listen "0.0.0.0:8080")>
(make-server-socket 8080)
  => #<socket (listen "0.0.0.0:8080")>
(make-server-socket 'inet 0)
  => #<socket (listen "0.0.0.0:35628")>
(make-server-socket 'unix "/tmp/.sock")
  => #<socket (listen "/tmp/.sock")>

Several accessors are available on the returned socket object.

Function: socket-address socket
Returns a socket address associated with socket. If no address has been associated to the socket, #f is returned.

Function: socket-input-port socket &keyword (buffering :modest)
Function: socket-output-port socket &keyword (buffering :line)
Returns an input and output port associated with socket, respectively.

The keyword argument buffering specifies the buffering mode of the port. See section 6.18.3 File ports, for explanation of the buffering mode.

Function: socket-close socket
Closes socket. All the ports associated to socket are closed as well. If the socket is connected, it is shut down before closing.

Function: call-with-client-socket socket proc
socket must be a connected client socket. proc is called with two arguments, an input port that reads from the socket and an output port that writes to the socket. The socket is closed after proc returns or proc raises an error.

This is an example of usage of high-level socket functions, a very simple http client.

 
#!/usr/bin/env gosh
(use gauche.regexp)
(use gauche.net)

(define (usage)
  (display "Usage: swget url\n" (current-error-port))
  (exit 1))

;; Returns three values: host, port, and path.
(define (parse-url url)
  (rxmatch-let (rxmatch #/^http:\/\/([-A-Za-z\d.]+)(:(\d+))?(\/.*)?/ url)
      (#f host #f port path)
    (values host port path)))

(define (get url)
  (receive (host port path) (parse-url url)
    (call-with-client-socket
        (make-client-socket 'inet host (string->number (or port "80")))
      (lambda (in out)
        (format out "GET ~a HTTP/1.0\r\n" path)
        (format out "host: ~a\r\n\r\n" host)
        (flush out)
        (copy-port in (current-output-port))))))

(define (main args)
  (if (= (length args) 2)
      (get (cadr args))
      (usage))
  0)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Ken Dickey on November, 28 2002 using texi2html