net

This module provides access to networking utilities.

Socket object

The Socket object is a thin object oriented wrapper around socket(2) and its related functionality.

class net.Socket(domain, type[, options])

Creates a socket object.

Arguments:
  • domain – Socket domain (one of net.AF_INET, net.AF_INET6 or net.AF_UNIX).
  • type – Socket type (one of net.SOCK_STREAM or net.SOCK_DGRAM).
  • options

    Optional object which may contain the following properties:

    • fd: if specified, the socket is built with the given fd, an a new one is not created.
    • nonBlocking: if specified and true, the socket will be breated in non-blocking mode.

See also

socket(2)

Note

Since version 0.3.0 the fds are created with O_CLOEXEC set. You can undo this using os.cloexec().

net.Socket.fd

Returns the file descriptor representing the socket.

net.Socket.prototype.accept()

Waits for an incoming connection and accepts it. Only works on listening sockets.

Returns:The accepted net.Socket() object.

See also

accept(2)

net.Socket.prototype.bind(address)

Bind the socket to the given address. See Socket addresses for the format.

See also

bind(2)

net.Socket.prototype.close()

Closes the socket.

See also

close(2)

net.Socket.prototype.connect(address)

Starts a connection towards the given address. See Socket addresses for the format.

See also

connect(2)

net.Socket.prototype.getsockname()

Returns the socket’s local address. See Socket addresses for the format.

See also

getsockname(2)

net.Socket.prototype.getpeername()

Returns the socket’s peer address. See Socket addresses for the format.

See also

getpeername(2)

net.Socket.prototype.listen([backlog])

Set the socket in listening mode, ready for accepting connections.

Arguments:
  • backlog – The maximum length for the queue of pending connections. If not set it defaults to 128.

See also

listen(2)

net.Socket.prototype.recv([nrecv])

Receive data from a socket. It can only be used in a connected socket.

Arguments:
  • nrecv – Maximum amount of data to receive. If not specified it defaults to 4096. Alternatively, a Buffer can be passed, and data will be read into it.
Returns:

The data that was read as a Uint8Array or the amount of data read as a number, if a Buffer was passed.

See also

recv(2)

net.Socket.prototype.send(data)

Transmit a message to the other socket. It can only be used with connected sockets.

Arguments:
  • data – The message that will be transmitted (can be a string or a Buffer).
Returns:

The number of bytes from data which were actually sent.

See also

send(2)

net.Socket.prototype.recvfrom([nrecv])

Similar to net.Socket.prototype.recv() but it can also be used in non-connected sockets.

Returns:An object with 2 properties: address, which contains the address of the sender and nread if a Buffer was used, or data, with the data as a Uint8Array.

See also

recvfrom(2)

net.Socket.prototype.sendto(data, address)

Similar to net.Socket.prototype.recv() but it can also be used in non-connected sockets and the destination address has to be specified.

See also

sendto(2)

net.Socket.prototype.shutdown(how)

Causes all or part of a full-duplex connection to be shut down. how must be one of net.SHUT_RD, net.SHUT_WR or net.SHUT_RDWR.

See also

shutdown(2)

net.Socket.prototype.setsockopt(level, option, value)

Set a socket option on the given level. value may contain either a number (for numeric or boolean options) or a string containing the binary representation of the value. Use of Buffer objects is recommended to build the binary value.

Example, setting a numeric or boolean option:

var sock = new net.Socket(net.AF_INET, net.SOCK_STREAM);
sock.setsockopt(net.SOL_SOCKET, net.SO_REUSEADDR, true);

Example, setting a binary option:

var sock = new net.Socket(net.AF_INET, net.SOCK_STREAM);
var lingerOpts = new Buffer(8);
lingerOpts.writeInt32LE(1, 0);    // enable lingering
lingerOpts.writeInt32LE(100, 4);    // linger for 100 seconds
sock.setsockopt(net.SOL_SOCKET, net.SO_LINGER, lingerOpts.toString());

See also

setsockopt(2)

net.Socket.prototype.getsockopt(level, option[, size])

Get the value for a socket option on the given level. When size is omitted, the value is assumed to be an integer, but when a number is given a buffer of size size is used. A Buffer object can be used to parse the result.

Example, getting a numeric or boolean option:

var sock = new net.Socket(net.AF_INET, net.SOCK_STREAM);
sock.setsockopt(net.SOL_SOCKET, net.SO_REUSEADDR, true);
var r = sock.getsockopt(net.SOL_SOCKET, net.SO_REUSEADDR);

Example, getting a binary option:

var sock = new net.Socket(net.AF_INET, net.SOCK_STREAM);
var lingerOpts = new Buffer(8);
lingerOpts.writeInt32LE(1, 0);    // enable lingering
lingerOpts.writeInt32LE(100, 4);    // linger for 100 seconds
sock.setsockopt(net.SOL_SOCKET, net.SO_LINGER, lingerOpts.toString());
var r = sock.sgetsockopt(net.SOL_SOCKET, net.SO_LINGER, 8);
var resBuf = new Buffer(r);
assert.equal(resBuf.readInt32LE(0), 1)
assert.equal(resBuf.readInt32LE(4), 100)

See also

getsockopt(2)

net.Socket.prototype.setNonBlocking(set)

Sets the socket in non-blocking mode if true, or blocking mode if false.

Constants

net.AF_INET

IPv4 socket domain.

net.AF_INET6

IPv6 socket domain.

net.AF_UNIX

Unix socket domain.

net.SOCK_STREAM

Stream socket type.

net.SOCK_DGRAM

Datagram socket type.

net.SHUT_RD
net.SHUT_WR
net.SHUT_RDWR

Shutdown modes for net.Socket.prototype.shutdown().

Functions

Socket addresses

Throughout this module, when an address is taken as a parameter or returned from a function, it’s expressed as an object with different properties, depending on the address family:

  • IPv4 sockets (AF_INET family): object containing host and port properties.
  • IPv6 sockets (AF_INET6 family): object containing host, port, flowinfo and scopeid properties. The last two can be omitted and will be assumed to be 0.
  • Unix sockets (AF_UNIX family): string containing the path.

getaddrinfo

net.getaddrinfo(hostname, servname[, hints])

Get a list of IP addresses and port numbers for host hostname and service servname. See getaddrinfo(3) for details.

Example:

sjs> var r = net.getaddrinfo('google.com', 'http');
sjs> outil.inspect(r);
= [ { family: 2,
    type: 2,
    protocol: 17,
    canonname: '',
    address: { host: '216.58.198.174', port: 80 } },
{ family: 2,
    type: 1,
    protocol: 6,
    canonname: '',
    address: { host: '216.58.198.174', port: 80 } },
{ family: 30,
    type: 2,
    protocol: 17,
    canonname: '',
    address:
    { host: '2a00:1450:4009:809::200e',
    port: 80,
    flowinfo: 0,
    scopeid: 0 } },
{ family: 30,
    type: 1,
    protocol: 6,
    canonname: '',
    address:
    { host: '2a00:1450:4009:809::200e',
    port: 80,
    flowinfo: 0,
    scopeid: 0 } } ]

The hints optional argument may contain an object with the following properties: family, type, protocol and flags.

Example:

sjs> var r = net.getaddrinfo('google.com', 'http', {family: net.AF_INET});
sjs> outil.inspect(r);
= [ { family: 2,
    type: 2,
    protocol: 17,
    canonname: '',
    address: { host: '216.58.198.174', port: 80 } },
{ family: 2,
    type: 1,
    protocol: 6,
    canonname: '',
    address: { host: '216.58.198.174', port: 80 } } ]

The returned result is a list of objects containing the following properties: family, type, protocol, canonname and address.

net.gai_strerror(code)

Get the string that describes the given error code.

net.gai_error_map

A Map mapping getaddrinfo error codes to their string versions.

net.AI_*

All addrinfo constants to be used as hints in net.getaddrinfo(). See getaddrinfo(3) for details.

net.EAI_*

All error codes net.getaddrinfo() could give. See getaddrinfo(3) for details.

net.SOL_*
net.IPPROTO_*

Levels to be used with net.Socket.prototype.setsockopt() and net.Socket.prototype.setsockopt().

net.SO_*
net.IP_*
net.IPV6_*
net.TCP_*

Options to be used with net.Socket.prototype.setsockopt() and net.Socket.prototype.setsockopt().

Utility functions

net.isIP(address)

Returns 4 if the given address is an IPv4 address, 6 if it’s an IPv6 address and 0 otherwise.

net.isIPv4(address)

Returns true if the given address is a valid IPv4 address, and false otherwise.

net.isIPv6(address)

Returns true if the given address is a valid IPv6 address, and false otherwise.