1. Unit tcp
    1. tcp-listen
    2. tcp-listener?
    3. tcp-close
    4. tcp-accept
    5. tcp-accept-ready?
    6. tcp-listener-port
    7. tcp-listener-fileno
    8. tcp-connect
    9. tcp-addresses
    10. tcp-port-numbers
    11. tcp-abandon-port
    12. tcp-buffer-size
    13. tcp-read-timeout
    14. tcp-write-timeout
    15. tcp-connect-timeout
    16. tcp-accept-timeout
    17. Example

Unit tcp

This unit provides basic facilities for communicating over TCP sockets. The socket interface should be mostly compatible to the one found in PLT Scheme.

This unit uses the extras unit.

All errors related to failing network operations will raise a condition of kind (exn i/o network).

tcp-listen

[procedure] (tcp-listen TCPPORT [BACKLOG [HOST]])

Creates and returns a TCP listener object that listens for connections on TCPPORT, which should be an exact integer. BACKLOG specifies the number of maximally pending connections (and defaults to 4). If the optional argument HOST is given and not #f, then only incoming connections for the given host (or IP) are accepted.

tcp-listener?

[procedure] (tcp-listener? X)

Returns #t if X is a TCP listener object, or #f otherwise.

tcp-close

[procedure] (tcp-close LISTENER)

Reclaims any resources associated with LISTENER.

tcp-accept

[procedure] (tcp-accept LISTENER)

Waits until a connection is established on the port on which LISTENER is listening and returns two values: an input- and output-port that can be used to communicate with the remote process. The current value of tcp-accept-timeout is used to determine the maximal number of milliseconds (if any) to wait until a connection is established. When a client connects any read- and write-operations on the returned ports will use the current values (at the time of the connection) of tcp-read-timeout and tcp-write-timeout, respectively, to determine the maximal number of milliseconds to wait for input/output before a timeout error is signalled.

Note: this operation and any I/O on the ports returned will not block other running threads.

tcp-accept-ready?

[procedure] (tcp-accept-ready? LISTENER)

Returns #t if there are any connections pending on LISTENER, or #f otherwise.

tcp-listener-port

[procedure] (tcp-listener-port LISTENER)

Returns the port number assigned to LISTENER (If you pass 0 to tcp-listen, then the system will choose a port-number for you).

tcp-listener-fileno

[procedure] (tcp-listener-fileno LISTENER)

Returns the file-descriptor associated with LISTENER.

tcp-connect

[procedure] (tcp-connect HOSTNAME [TCPPORT])

Establishes a client-side TCP connection to the machine with the name HOSTNAME (a string) at TCPPORT (an exact integer) and returns two values: an input- and output-port for communicating with the remote process. The current value of tcp-connect-timeout is used to determine the maximal number of milliseconds (if any) to wait until the connection is established. When the connection takes place any read- and write-operations on the returned ports will use the current values (at the time of the call to tcp-connect) of tcp-read-timeout and tcp-write-timeout, respectively, to determine the maximal number of milliseconds to wait for input/output before a timeout error is signalled.

If the TCPPORT is omitted, the port is parsed from the HOSTNAME string. The format expected is HOSTNAME:PORT. The PORT can either be a string representation of an integer or a service name which is translated to an integer using the POSIX function getservbyname.

Note: any I/O on the ports returned will not block other running threads.

tcp-addresses

[procedure] (tcp-addresses PORT)

Returns two values for the input- or output-port PORT (which should be a port returned by either tcp-accept or tcp-connect): the IP address of the local and the remote machine that are connected over the socket associated with PORT. The returned addresses are strings in XXX.XXX.XXX.XXX notation.

tcp-port-numbers

[procedure] (tcp-port-numbers PORT)

Returns two values for the input- or output-port PORT (which should be a port returned by either tcp-accept or tcp-connect): the TCP port numbers of the local and the remote machine that are connected over the socket associated with PORT.

tcp-abandon-port

[procedure] (tcp-abandon-port PORT)

Marks the socket port PORT as abandoned. This is mainly useful to close down a port without breaking the connection.

tcp-buffer-size

[parameter] tcp-buffer-size

Sets the size of the output buffer. By default no output-buffering for TCP output is done, but to improve performance by minimizing the number of TCP packets, buffering may be turned on by setting this parameter to an exact integer greater zero. A buffer size of zero or #f turns buffering off. The setting of this parameter takes effect at the time when the I/O ports for a particular socket are created, i.e. when tcp-connect or tcp-accept is called.

Note that since output is not immediately written to the associated socket, you may need to call flush-output, once you want the output to be transmitted. Closing the output port will flush automatically.

tcp-read-timeout

[parameter] tcp-read-timeout

Determines the timeout for TCP read operations in milliseconds. A timeout of #f disables timeout checking. The default read timeout is 60000, i.e. 1 minute.

tcp-write-timeout

[parameter] tcp-write-timeout

Determines the timeout for TCP write operations in milliseconds. A timeout of #f disables timeout checking. The default write timeout is 60000, i.e. 1 minute.

tcp-connect-timeout

[parameter] tcp-connect-timeout

Determines the timeout for tcp-connect operations in milliseconds. A timeout of #f disables timeout checking and is the default.

tcp-accept-timeout

[parameter] tcp-accept-timeout

Determines the timeout for tcp-accept operations in milliseconds. A timeout of #f disables timeout checking and is the default.

Example

A very simple example follows. Say we have the two files client.scm and server.scm:

; client.scm
(declare (uses tcp))
(define-values (i o) (tcp-connect "localhost" 4242))
(write-line "Good Bye!" o)
(print (read-line i))
; server.scm
(declare (uses tcp))
(define l (tcp-listen 4242))
(define-values (i o) (tcp-accept l))
(write-line "Hello!" o)
(print (read-line i))
(close-input-port i)
(close-output-port o)
% csc server.scm
% csc client.scm
% ./server &
% ./client
Good Bye!
Hello!

Previous: Unit utils

Next: Unit lolevel