<server> ( addrspec encoding -- server )
Networking » Connection-oriented networking

Prev:with-local-address ( addr quot -- )
Next:accept ( server -- client remote )


Vocabulary
io.sockets

Inputs
addrspecan address specifier
encodingan encoding descriptor


Outputs
servera handle


Word description
Begins listening for network connections to a local address. Server objects respond to two words:
dispose - stops listening on the port and frees all associated resources
accept - blocks until there is a connection, and returns a stream of the encoding passed to the constructor


Notes
To start a TCP/IP server which listens for connections from any host, use an address specifier returned by the following code, where 1234 is the desired port number:
f 1234 <inet> resolve-host

To start a server which listens for connections from the loopback interface only, use an address specifier returned by the following code, where 1234 is the desired port number:
"localhost" 1234 <inet> resolve-host

Since resolve-host can return multiple address specifiers, your server code must listen on them all to work properly. The io.servers vocabulary can be used to help with this.

To start a TCP/IP server which listens for connections on a randomly-assigned port, set the port number in the address specifier to 0, and then read the addr slot of the server instance to obtain the actual port number it is listening on:
f 0 <inet4> ascii <server> [ addr>> . ] [ dispose ] bi
T{ inet4 f "0.0.0.0" 58901 }


Errors
Throws an error if the address is already in use, or if it if the system forbids access.

Definition