TCP Listener Socket
An IO class that listens for and accepts incoming TCP connection requests.
Constructor
Listener
Creates a new TCP Listener
object instance.
Parameters
options
An object of properties used to construct the class.
port
(optional) - a number specifying the port to listen on, defaults to0
which will select an arbitrary available port on the system.
address
(optional) - A string with the IP address of the network interface to bind to, defaults to ”::” for IPv6 or “0.0.0.0” for IPv4.
onReadable(requests)
(optional): A callback function that is invoked when one or more new connection requests are received by the socket, which can be retrieved using theread
method. Therequests
argument indicates the total number of pending connection requests.
Exceptions
If the constructor requires a resource that is already in use — whether by a script or the native host — an Error
exception is thrown.
Instance Properties
Includes properties of the IO Class Pattern
. Specific to this class:
format
Always returns "socket/tcp"
.
Instance Methods
read
Returns a TCP
Socket instance. The instance is already connected to the remote endpoint without any callbacks.
Return value
An instance of TCP
Socket if available.
Examples
The class can be imported from the embedded
namespace:
Simple TCP Server
This example implements a simple HTTP echo server. It accepts incoming requests and sends back the complete request (including the request headers) as the response body. The Echo
class reads the request and generates the response based on the TCP
Socket class.