This chapter covers the Network library. The Network library provides Internet address protocols and TCP/IP server and client sockets. It exports a single module, called Sockets.
This section covers the start-sockets function, which all libraries using the Network library must call before any other call to the Network library API. It also covers the with-socket-thread macro which registers the current thread as a thread that will call a socket function that blocks.
| Signature: | start-sockets () => () |
|---|---|
| Discussion: | Applications must call this function before using any other
function or variable from the Network library. This function is necessary because the Win32 API library Winsock2, which the Network library calls to get native socket functionality, requires applications to call an initialization function before calling any Winsock2 API functions. The call to start-sockets calls this underlying Winsock2 function. Note that you must not call start-sockets from a top-level form in any DLL project. The combination of this, and the restriction that you must call start-sockets before calling anything else, implies that no Network library function or variable can be called (directly or indirectly) from a top-level form in any DLL project. Instead, the DLL project should define a start function that calls start-sockets (directly or indirectly) or re-export start-sockets so that their clients can arrange to have it called from a top-level form in an appropriate EXE project. Applications using the Network library must arrange for start-sockets to be called (directly or indirectly) before any other sockets API functions. A good place to do this is at the beginning of your start function (usually the main method). For example: define method main () => ();
start-sockets();
let the-server = make(<TCP-server-socket>, port: 7);
...
end;
begin
main();
end;
New start functions that call start-sockets and that are defined for DLL projects that use the Network library will inherit all of the restrictions described above for start-sockets. Calling a Network library function before calling start-sockets results in a <sockets-not-initialized> error. Calling start-sockets from a top-level form in a DLL project will result in unpredictable failures—probably access violations during initialization. |
| Macro Call: | with-socket-thread (#key *server?*)
*body*
end;
|
|---|---|
| Discussion: | Registers the current thread as a blocking socket thread, that is,
a thread which will call a socket function that blocks, such as
read-element or accept. The reason for the registration is that Network library shutdown can then synchronize with these threads. The early part of the shutdown sequence should cause the threads to unblock with an <end-of-stream-error> so that they can do whatever application cleanup is necessary. Once these threads have exited, the rest of the shutdown sequence can be executed. A server socket thread (blocking on accept rather than read-element) notices that the shutdown sequence is underway slightly later, with a <blocking-call-interrupted> condition. |
This section covers Internet address protocols.
This section covers the class <internet-address> and related generic functions and constants.
| Superclasses: | <object> |
|---|---|
| Init-Keywords: |
|
| Discussion: | The class of objects representing Internet addresses used as endpoints for peer-to-peer socket connections. To construct an <internet-address> object you must supply either the name: or address: keyword. For example: make (<internet-address>, name: "www.whatever.com")
or make (<internet-address>, address: "9.74.122.0")
make on <internet-address> returns an instance of <ipv4-address>. |
| Signature: | host-name internet-address => name |
|---|---|
| Discussion: | Returns an instance of <string> containing a symbolic host
name. The internet-address argument must be an instance of
<internet-address>. Usually the name returned is the canonical host name. Note, however, that the implementation is conservative about making DNS calls. Suppose that the <internet-address> instance was created with the name: keyword and no other information. If the application has not made any other requests that would require a DNS call, such as to host-address or aliases, the name that this function returns will be the one specified with the name: keyword, regardless of whether that is the canonical name or not. |
| Signature: | host-address internet-address => address |
|---|---|
| Discussion: | Returns an instance of <string> containing the presentation form of
the host address. In the case of multi-homed hosts this will usually be
the same as:multi-homed-internet-address.all-addresses.first.host-address
In the case of an Internet address created using the address: keyword it will be either the keyword value or all-addresses.first.host-address. |
Returns the host address as a <numeric-address>.
| Signature: | numeric-host-address internet-address => numeric-address |
|---|
| Signature: | all-addresses internet-address => sequence |
|---|---|
| Discussion: | Returns an instance of <sequence> whose elements are <internet-address> objects containing all known addresses for the host. |
| Signature: | aliases internet-address => sequence |
|---|---|
| Discussion: | Returns an instance of <sequence> whose elements are instances of <string> representing alternative names for the host. |
| Type : | <internet-address> |
|---|---|
| Discussion: | An instance of <internet-address> representing the loopback address: “127.0.0.1”. |
| Type : | <internet-address> |
|---|---|
| Discussion: | An instance of <internet-address> representing the host on
which the application using sockets is correctly running. Note that this value is not necessarily the same as would be created by the expression make (<internet-address>, name: "localhost")
The address assigned to the symbolic name localhost is dependent on the configuration of DNS. In some cases this may be configured to be the loopback address rather than a real address for the local host. |
This name is reserved for future development.
This section describes numeric Internet representation and associated protocols.
| Superclasses: | <object> |
|---|---|
| Discussion: | The class of objects representing the numeric form of an Internet
addresses. Currently only ipv4 (32-bit) addresses are supported. Ipv6 addresses will be added when they are supported by Winsock2. In general <numeric-address> objects are accessed using the functions host-order or network-order, depending on the context in which they are employed. |
| Signature: | network-order address => network-order-address |
|---|---|
| Discussion: | Returns the value of the numeric address in network order. The argument
is a general instance of <numeric-address>. The class of the object
returned depends upon the particular subclass of the argument; the
network-order method for <ipv4-numeric-address> returns an instance
of <machine-word>. Network order is big-endian byte order. |
| Signature: | host-order address => host-order-address |
|---|---|
| Discussion: | Like network-order but returns the value in host order. Host order is either big-endian byte order on a big-endian host machine and little-endian on a little-endian host machine. |
| Superclasses: | |
|---|---|
| Init-Keywords: |
|
| Discussion: | The single slot of this class contains a 32-bit value representing a ipv4 address. This slot is accessed by the generic functions network-order and host-order described above. <ipv4-numeric-address> has two concrete subclasses <ipv4-network-order-address> and <ipv4-host-order-address>. Make <ipv4-numeric-address> returns one or the other of these depending upon the value of the order: keyword. |
| Signature: | host-order ip4-numeric-address => machine-word |
|---|---|
| Discussion: | Returns the numeric address in host order as an instance of <machine-word>. The argument is an instance of <ip4-numeric-address>. |
| Signature: | network-order ipv4-numeric-address => machine-word |
|---|---|
| Discussion: | Returns the numeric address in network order as an instance of <machine-word>. The argument is an instance of <ip4-numeric-address>. |
Returns the presentation (dotted string) form of an instance of <ip4-numeric-address>.
| Signature: | as string ipv4-numeric-address => string |
|---|
Concrete subclass for network-order numeric addresses.
| Superclasses: | <ipv4-numeric-address> |
|---|---|
| Discussion: | make(<ipv4-network-order-address>)
is equivalent to make(<ipv4-numeric-address>, order: network-order)
|
Concrete subclass for host order numeric addresses.
| Superclasses: | <ipv4-numeric-address> |
|---|
This section describes socket classes and protocols.
| Superclasses: | <object> |
|---|---|
| Init-Keywords: |
|
| Discussion: |
The common superclass of all socket objects including <socket> (IP client socket), <server-socket> and <socket-accessor>.
Each subclass of <abstract-socket> must provide methods for close and for the following generic functions:
Returns the local port number.
| Signature: | local-port socket => port-number |
|---|---|
| Parameters: |
|
| Values: |
|
Returns the descriptor (handle or fd) for the socket.
| Signature: | socket-descriptor socket => descriptor |
|---|---|
| Parameters: |
|
| Values: |
|
Returns the address of the local host.
| Signature: | local-host socket => host-address |
|---|---|
| Parameters: |
|
| Values: |
|
| Superclasses: | |
|---|---|
| Init-Keywords: |
|
| Discussion: | Server-sockets listen on a specified port for connection requests which come in over the network. Either the port: or service: keyword must be supplied. make on (<server-socket>) returns an instance of <tcp-server-socket> by default. |
| Signature: | accept server-socket #rest args #key => result |
|---|---|
| Discussion: | Blocks until a connect request is received, then it returns a connected instance of <socket>. The particular subclass of <socket> returned depends on the actual class of the argument, which must be a general instance of <server-socket>. Calling accept on <tcp-server-socket> returns a connected <tcp-socket>. The keyword arguments are passed to the creation of the <socket> instance. For UDP sockets accept returns immediately with an instance of <udp-socket>. No blocking happens for UDP sockets because they are connectionless. After reading from a UDP socket returned from accept the socket can be interrogated for the location of the sender using remote-host and remote-port. |
| Macro Call: | with-server-socket (*server-var* [:: *server-class* ], *keywords*)
*body*
end;
|
|---|---|
| Discussion: | Creates an instance of <server-socket>, using the (optional) server-class argument and keyword arguments to make the <server-socket>, and binds it to the local variable named by server-var. The body is evaluated in the context of the binding and the <server-socket> is closed after the body is executed. |
| Macro Call: | start-server ([*server-var* = ]*socket-server-instance*,
*socket-var* [, *keywords* ])
*body*
end;
|
|---|---|
| Discussion: | Enters an infinite while(#t) accept loop on the server socket. Each time accept succeeds the <socket> returned from accept is bound to socket-var and the body is evaluated in the context of the binding. When body exits, accept is called again producing a new binding for socket-var. The optional keywords are passed to the call to accept. |
| Superclasses: | <server-socket> |
|---|---|
| Init-Keywords: |
|
| Discussion: | The class of TCP server sockets. A server socket is an object which listens for requests for connections from the network. When accept is called on the server socket and a request for connection is detected, accept returns a connected <socket>. |
| Signature: | accept server-socket #rest args #key element-type => connected-socket |
|---|---|
| Parameters: |
|
| Values: |
|
| Discussion: | The other keyword arguments are passed directly to the creation of the <tcp-socket> instance. |
The class of general client sockets. All client sockets are streams.
| Superclasses: | <abstract-socket>, <external-stream> |
|---|---|
| Init-Keywords: |
|
| Superclasses: | <socket>, <double-buffered-stream> |
|---|---|
| Init-Keywords: |
|
| Discussion: | Socket streams whose elements are bytes or characters. These inherit buffering protocols and the implications of read, write, read-element, write-element, force-output and suchlike methods from <double-buffered-stream>. |
The class of TCP client sockets.
The class of TCP client sockets.
| Superclasses: | |
|---|---|
| Init-Keywords: |
|
Returns the remote port number for a <socket>.
| Signature: | remote-port socket => port-number |
|---|---|
| Parameters: |
|
| Values: |
|
Returns the remote host for a <socket>.
| Signature: | remote-host socket => remote-host-address |
|---|---|
| Parameters: |
|
| Values: |
|
The class of UDP client sockets.
The class of UDP client sockets.
| Superclasses: | |
|---|---|
| Init-Keywords: |
|
| Discussion: | Of the keywords, host: and one of either service: or port: are required. |
The class of UDP server sockets.
| Superclasses: | <server-socket> |
|---|---|
| Init-Keywords: |
|
| Discussion: | The class of UDP server sockets. A server socket is an object that listens for requests from the network. When accept is called on the UDP server socket, accept returns a <udp-socket>. |
This section lists the socket condition classes in the Network library.
All socket conditions are general instances of <socket-condition>. Some are recoverable and others are not.
| Superclasses: | <simple-condition> |
|---|---|
| Discussion: | The class of socket conditions. It inherits the format-string:
and format-arguments: keywords from
<simple-condition>. Slots: socket-condition-details
|
The class <socket-error> is the superclass of all unrecoverable socket conditions.
| Superclasses: | <socket-condition> |
|---|
The class of socket conditions from which no recovery is possible.
The class <internal-socket-error> is the class of unexpected socket errors.
| Superclasses: | <socket-error> |
|---|---|
| Discussion: | The class of unexpected errors from Open Dylan’s Winsock2 library,
an FFI interface to the native socket library Winsock2. Inspect the contents of the socket-condition-details slot for more information. |
The <recoverable-socket-condition> class is the general class of socket conditions for which an application may be able to take some remedial action.
| Superclasses: | <socket-condition> |
|---|---|
| Discussion: | The general class of socket conditions for which an application may be
able to take some remedial action. For instance, a web browser receiving such conditions as <connection-refused> or <host-not-found> would normally expect to report those conditions to the user and continue with some other connection request from the user, while a server receiving a <connection-closed> condition from a connected <socket> would probably close the <socket> and continue to handle other requests for connections. |
The network — probably a local network — is down. Try again later.
| Superclasses: | <recoverable-socket-condition> |
|---|
A badly formed address string has been passed to a function trying to make an <internet-address>.
| Superclasses: | <recoverable-socket-condition> |
|---|
The Domain Name Server (DNS) cannot resolve the named host or internet address. Try again with a different (correct) name or address.
| Superclasses: | <recoverable-socket-condition> |
|---|
The Domain Name Server (DNS) did not respond or returned an ambiguous result. Try again.
| Superclasses: | <recoverable-socket-condition> |
|---|
The remote host cannot be reached from this host at this time.
| Superclasses: | <recoverable-socket-condition> |
|---|
| Superclasses: | <recoverable-socket-condition> |
|---|---|
| Discussion: | The socket or server socket has been closed. Most operations on closed instances of <tcp-socket>` return instances of <stream-closed-error> (from the Streams library) rather than instances of <socket-closed>. |
| Superclasses: | <recoverable-socket-condition> |
|---|---|
| Discussion: | The attempt to connect to the remote host was not successful. Connection failed for one of the following reasons: because the connect request timed out or because it was refused, or because the remote host could not be reached. |
| Superclasses: | <recoverable-socket-condition> |
|---|---|
| Discussion: | The connection to the remote host has been broken. The socket should be closed. To try again, open a new socket. |
| Superclasses: | <recoverable-socket-condition> |
|---|---|
| Discussion: | A process on the machine is already bound to the same fully qualified address. This condition probably occurred because you were trying to use a port with an active server already installed, or a process crashed without closing a socket. |
A blocking socket call, like read, write or accept, was interrupted.
| Superclasses: | <recoverable-socket-condition> |
|---|
| Superclasses: | <recoverable-socket-condition> |
|---|---|
| Discussion: | The implementation-dependent limit on the number of open sockets has been reached. You must close some sockets before you can open any more. The limits for Windows NT (non-server machines) and Windows 95 are particularly small. |
| Superclasses: | <socket-error> |
|---|---|
| Discussion: | An implementation-specific error from the C-FFI interface to the native socket library. Usually instances of this class these appear in the socket-condition-details slot of another <socket-condition>. |
| Superclasses: | <socket-accessor-error> |
|---|---|
| Discussion: | A Win32-specific error from the Winsock2 library, a C-FFI interface to
the native socket library Winsock2. A function in the FFI library has
returned an error return code. Slots:
|