5. Bigloo -- Standard Library 5.9 Socket support

Browsing
Home: | Bigloo A ``practical Scheme compiler'' for Bigloo version 2.5a April 2002
Previous chapter: | Core Language
Next chapter: | Pattern Matching
Standard Library
    5.1 Scheme Library
    5.2 Input and output
    5.3 Structures and Records
    5.4 Serialization
    5.5 Bit manipulation
    5.6 Hash Tables
    5.7 System programming
    5.8 Process support
    5.9 Socket support
    5.10 Posix Regular Expressions
Chapters
    Copyright
    Acknowledgements
    1. Table of contents
    2. Overview of Bigloo
    3. Modules
    4. Core Language
    5. Standard Library
    6. Pattern Matching
    7. Object System
    8. Threads
    9. Regular parsing
    10. Lalr(1) parsing
    11. Errors and Assertions
    12. Eval and code interpretation
    13. Macro expansion
    14. Command Line Parsing
    15. Explicit typing
    16. The C interface
    17. The Java interface
    18. Bigloo Libraries
    19. SRFIs
    20. DSSSL support
    21. Compiler description
    22. User Extensions
    23. Bigloo Development Environment
    24. Global Index
    25. Library Index
    Bibliography
| Bigloo defines sockets, on systems that 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. The implementation and this documentation are, to a great extent copies of the STk [Gallesio95] socket support.
make-client-socket hostname port-number [buffered] | bigloo procedure
`make-client-socket` returns a new socket object. This socket establishes a link between the running application listening on port `port-number` of `hostname`. If optional argument `buffered` is `#f` then the input port associated with the socket is unbuffered. This is useful for socket clients connected to servers that do not emit #\Newline character after emissions. If optional argument `buffered` is missing or is not to `#f` the input port uses a buffer. When a socket is used in unbufferized mode the characters available on the input port must be read exclusively with `read-char` or `read-line`. It is forbidden to use `read` or any regular grammar. This limitation is imposed by Rgc (see Regular Parsing) that intrinsicly associate buffers with regular grammars. If the current Rgc implementation is improved on the coming version this restriction will be suppressed.
socket? obj | bigloo procedure
socket-server? obj | bigloo procedure
socket-client? obj | bigloo procedure
Returns `#t` if `obj` is a socket, a socket server a socket client. Otherwise returns `#f`. Socket servers and socket clients are sockets.
socket-hostname socket | bigloo 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 the socket, this function returns `#f`.
socket-host-address socket | bigloo 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 the socket, this function returns `#f`.
socket-local-address socket | bigloo procedure
Returns a string which contains the IP number of the local host attached to `socket`.
socket-port-number socket | bigloo procedure
Returns the integer number of the port used for `socket`.
socket-input socket | bigloo procedure
socket-output socket | bigloo 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 `#f`. The following example shows how to make a client socket. Here we create a socket on port 13 of the machine ```kaolin.unice.fr`'' is generally used for testing: making a connection to it permits to know the distant system's idea of the time of day.1:
    (let ((s (make-client-socket "kaolin.unice.fr" 13)))
      (print "Time is: " (read-line (socket-input s)))
      (socket-shutdown  s))
make-server-socket [port-number] | bigloo 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 choosen by the system.
socket-accept-connection socket [buffered] | bigloo procedure
`socket-accept-connection` 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 to `socket`. This procedure must be called on a server socket created with `make-server-socket`. If optional argument `buffered` is `#f` then the input port associated with the socket is unbuffered. This is useful for socket clients connected to servers that do not emit #\Newline character after emissions. If optional argument `buffered` is missing or is not to `#f` the input port uses a buffer. The result of `socket-accept-connection` is undefined. Note: When a socket is used in unbufferized mode the characters available on the input port must be read exclusively with `read-char` or `read-line`. It is forbidden to use `read` or any regular grammar. This limitation is imposed by Rgc (see Regular Parsing) that intrinsicly associate buffers with regular grammars. If the current Rgc implementation is improved on the coming version this restriction will be suppressed. The following exemple is a simple server which waits for a connection on the port 1234 listening socket with the `telnet` command. With the given example, this can be achived by typing the following command in a window shell: `$ telnet localhost 1234` 2. 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 ((s (make-server-socket 1234)))
      (socket-accept-connection s)
      (let ((l (read-line (socket-input s))))
        (fprint (socket-output s) "Length is: " (string-length l))
        (flush-output-port (socket-output s)))
      (socket-shutdown s))
socket-shutdown socket [close] | bigloo procedure
`Socket-shutdown` shutdowns the connection associated to `socket`. `Close` is a boolean; it indicates if 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-connection` procedure. Omitting a value for `close` implies the closing of socket. The result of `socket-shutdown` is undefined. The following example shows a simple server: when there is a new connection on the port number 1234, 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 1234)))
      (let loop ()
        (socket-accept-connection s)
        (print "I've read: " (read-line (socket-input s)))
        (socket-shutdown s #f)
        (loop)))
socket-down? socket | bigloo procedure
Returns `#t` if `socket` has been previously closed with `socket-shutdown`. It returns `#f` otherwise.
socket-dup socket | bigloo procedure
Returns a copy of `socket`. The original and the copy socket can be used interchangeably. However, if a new connection is accepted on one socket, the characters exchanged on this socket are not visible on the other socket. Duplicating a socket is useful when a server must accept multiple simultaneous connections. The following example creates a server listening on port 1234. This server is duplicated and, once two clients are present, a message is sent on both connections.
    (define s1 (make-server-socket 1234))
    (define s2 (socket-dup s1))
    (socket-accept-connection s1)
    (socket-accept-connection s2)
    ;; blocks until two clients are present
    (display #"Hello,\n" (socket-output s1))
    (display #"world\n"  (socket-output s2))
    (flush-output-port (socket-output s1))
    (flush-output-port (socket-output s2))
Here is another example of making use of sockets:
    (define s (make-server-socket))

    (dynamic-wind
       ;; Init: Launch an xterm with telnet running
       ;; on the s listening port and connect
       (lambda ()
          (run-process "xterm" "-e" "telnet" "localhost"
                       (number->string (socket-port-number s)))
          (socket-accept-connection s)
          (display #"\nWelcome on the socket REPL.\n\n> " (socket-output s))
          (flush-output-port (socket-output s)))

       ;; Action: A toplevel like loop
       (lambda ()
          (let loop ()
             (fprint (socket-output s)
                     "; Result: "
                     (eval (read (socket-input s))))
             (display "> " (socket-output s))
             (flush-output-port (socket-output s))
             (loop)))

       ;; Termination: We go here when
       ;;     -a: an error occurs
       ;;     -b: connection is closed
       (lambda ()
          (print #"Shutdown ......\n")
          (socket-shutdown s)))
At, to conclude here is a last example that uses sockets. It implements a client-server architecture and it uses unbufferized (see `socket-accept-connection`) input ports. First, here is the code of the client:
    (module client)

    (let* ((s (make-client-socket "localhost" 8080 #f))
           (p (socket-output s)))
       (display "string" p)
       (newline p)
       (display "abc" p)
       (flush-output-port p)
       (let loop ()
          (loop)))
Then, here is the code of the server:
    (module server)

    (let ((s (make-server-socket 8080)))
       (socket-accept-connection s #f)
       (let ((pin (socket-input s)))
          (let loop ()
             (display (read-char pin))
             (flush-output-port (current-output-port))
             (loop))))

This Scribe page has been generated by `scribeinfo`.
Last update Thu Apr 25 09:40:15 2002