$ 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.


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:
$1a $handle for the connected socket

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


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

Side Effects / Notes



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

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

See Also