listen

Synopsis

$ listen(port, [timeout], [$host], [&closure], [option => value, ...])

Instantiates a server socket to listen for TCP/IP connections on the specified port and accepts a connection.

Parameters

port - the port number to listen on

timeout - the number of milliseconds to wait for a connection before returning $null

$host - a variable to place the hostname of the connecting host into.

&closure - if &closure is specified, this function call will not block. &closure will be called when a connection is established. When called, the closure receives the following arguments:
ArgumentDescription
$1a $handle for the connected socket

option => value - various socket options that can be set:

Returns

A $handle to a TCP/IP socket. This handle can be read from and written to using Sleep's IO functions.

Side Effects / Notes

Errors

Examples

# A simple "Hello World" server $socket = listen(5001); if (checkError($error)) { println("Error occured: $error"); } else { println($socket, "Hello World!"); } closef($socket);

$ telnet 127.0.0.1 5001 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Hello World! Connection closed by foreign host.

See Also