Module net

This module provides support for networking using TCP and UDP sockets.

Class DatagramSocket

Instance Methods

Class ServerSocket

Instance Methods

Class Socket

Instance Methods


DatagramSocket ()

The DatagramSocket class is used to create a UDP socket.


DatagramSocket.prototype.bind (host, port)

Binds the socket to a local address and port. If address or port are omitted the system will choose a local address and port to bind the socket.

Parameters

String host address (interface) to which the socket will be bound.
Number port port number to bind the socket to.

DatagramSocket.prototype.close ()

Close the socket immediately


DatagramSocket.prototype.connect (host, port)

Connect the socket to a remote address. If a DatagramSocket is connected, it may only send data to and receive data from the given address. By default DatagramSockets are not connected.

Parameters

String host IP address or hostname
Number port port number or service name

DatagramSocket.prototype.disconnect ()

Disconnects the socket.


DatagramSocket.prototype.getTimeout ()

Return the current timeout of this DatagramSocket. A value of zero implies that timeout is disabled, i.e. receive() will never time out.

Returns

Number the current timeout

DatagramSocket.prototype.isBound ()

Returns whether this socket is bound to an address.

Returns

Boolean true if the socket has been bound to an address

DatagramSocket.prototype.isClosed ()

Returns whether the socket is closed or not.

Returns

Boolean true if the socket has been closed

DatagramSocket.prototype.isConnected ()

Returns whether the socket is connected or not.

Returns

Boolean true if the socket has been connected to a remote address

DatagramSocket.prototype.localAddress ()

Get the local address to which this socket is bound. This returns an object with a property address containing the IP address as string and a property port containing the port number, e.g. {address: '127.0.0.1', port: 8080}.

Returns

Object an address descriptor

DatagramSocket.prototype.receive (length, buffer)

Receive a datagram packet from this socket. This method does not return the sender's IP address, so it is meant to be in conjunction with connect().

Parameters

Number length the maximum number of bytes to receive
ByteArray buffer optional buffer to store bytes in

Returns

ByteArray the received data

DatagramSocket.prototype.receiveFrom (length, buffer)

Receive a datagram packet from this socket. This method returns an object with the following properties:

  • address: the sender's IP address as string
  • port: the sender's port number
  • data: the received data

Parameters

Number length the maximum number of bytes to receive
ByteArray buffer optional buffer to store bytes in

Returns

Object the received packet

DatagramSocket.prototype.remoteAddress ()

Get the remote address to which this socket is connected. This returns an object with a property address containing the IP address as string and a property port containing the port number, e.g. {address: '127.0.0.1', port: 8080}.

Returns

Object an address descriptor

DatagramSocket.prototype.send (data)

Send a datagram packet from this socket. This method does not allow the specify the recipient's IP address, so it is meant to be in conjunction with connect().

Parameters

Binary data the data to send

DatagramSocket.prototype.sendTo (host, port, data)

Send a datagram packet from this socket to the specified address.

Parameters

String host the IP address of the recipient
Number port the port number
Binary data the data to send

DatagramSocket.prototype.setTimeout (timeout)

Enable/disable timeout with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to receive() for this DatagramSocket will block for only this amount of time.

Parameters

Number timeout timeout in milliseconds

ServerSocket ()

This class implements a server socket. Server sockets wait for requests coming in over the network.


ServerSocket.prototype.accept ()

Listens for a connection to be made to this socket and returns a new Socket object. The method blocks until a connection is made.

Returns

Socket a newly connected socket object

ServerSocket.prototype.bind (host, port)

Binds the socket to a local address and port. If address or port are omitted the system will choose a local address and port to bind the socket.

Parameters

String host address (interface) to which the socket will be bound.
Number port port number to bind the socket to.

ServerSocket.prototype.close ()

Close the socket immediately


ServerSocket.prototype.getTimeout ()

Return the current timeout of this ServerSocket. A value of zero implies that timeout is disabled, i.e. accept() will never time out.

Returns

Number the current timeout

ServerSocket.prototype.isBound ()

Returns whether this socket is bound to an address.

Returns

Boolean true if the socket has been bound to an address

ServerSocket.prototype.isClosed ()

Returns whether the socket is closed or not.

Returns

Boolean true if the socket has been closed

ServerSocket.prototype.localAddress ()

Get the local address to which this socket is bound. This returns an object with a property address containing the IP address as string and a property port containing the port number, e.g. {address: '127.0.0.1', port: 8080}

Returns

Object an address descriptor

ServerSocket.prototype.setTimeout (timeout)

Enable/disable timeout with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to accept() for this ServerSocket will block for only this amount of time.

Parameters

Number timeout timeout in milliseconds

Socket ()

The Socket class is used to create a TCP socket. Newly created sockets must be connected to a remote address before being able to send and receive data.


Socket.prototype.bind (host, port)

Binds the socket to a local address and port. If address or port are omitted the system will choose a local address and port to bind the socket.

Parameters

String host address (interface) to which the socket will be bound.
Number port port number to bind the socket to.

Socket.prototype.close ()

Close the socket immediately


Socket.prototype.connect (host, port, [timeout])

Initiate a connection on a socket. Connect to a remote port on the specified host with a connection timeout. Throws an exception in case of failure.

Parameters

String host IP address or hostname
Number port port number or service name
Number [timeout] optional timeout value in milliseconds

Socket.prototype.getStream ()

Get the I/O stream for this socket.

Returns

Stream a binary stream

See


Socket.prototype.getTimeout ()

Return the current timeout of this Socket. A value of zero implies that timeout is disabled, i.e. read() will never time out.

Returns

Number the current timeout

Socket.prototype.isBound ()

Returns whether this socket is bound to an address.

Returns

true if the socket has been bound to an address

Socket.prototype.isClosed ()

Returns whether the socket is closed or not.

Returns

true if the socket has been closed

Socket.prototype.isConnected ()

Returns whether the socket is connected or not.

Returns

true if the socket has been connected to a remote address

Socket.prototype.localAddress ()

Get the local address to which this socket is bound. This returns an object with a property address containing the IP address as string and a property port containing the port number, e.g. {address: '127.0.0.1', port: 8080}.

Returns

Object an address descriptor

Socket.prototype.remoteAddress ()

Get the remote address to which this socket is connected. This returns an object with a property address containing the IP address as string and a property port containing the port number, e.g. {address: '127.0.0.1', port: 8080}.

Returns

Object an address descriptor

Socket.prototype.setTimeout (timeout)

Enable/disable timeout with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() on this socket's stream will block for only this amount of time.

Parameters

Number timeout timeout in milliseconds