Socket Module
The socket
module provides functions for interacting with sockets.
Functions can be individually imported and directly accessed using the named import syntax:
import { AF_INET, SOCK_STREAM, create as socket } from 'socket';
let sock = socket(AF_INET, SOCK_STREAM, 0);
sock.connect('192.168.1.1', 80);
sock.send(…);
sock.recv(…);
sock.close();
Alternatively, the module namespace can be imported using a wildcard import statement:
import * as socket from 'socket';
let sock = socket.create(socket.AF_INET, socket.SOCK_STREAM, 0);
sock.connect('192.168.1.1', 80);
sock.send(…);
sock.recv(…);
sock.close();
Additionally, the socket module namespace may also be imported by invoking the ucode
interpreter with the -lsocket
switch.
- Source
Classes
socket.socket
Represents a socket handle.
Methods
addrinfo(hostname, serviceopt, hintsopt) → {AddressInfo[]}nullable
Resolves the given hostname and optional service name into a list of network addresses, according to the provided hints.
The addrinfo()
function provides an API for performing DNS and service name resolution. It returns an array of objects, each representing a resolved address.
Returns an array of resolved addresses. Returns null
if an error occurred during resolution.
Name | Type | Description |
---|---|---|
hostname | string | The hostname to resolve. |
service | string | (optional) Optional service name to resolve. If not provided, the service field of the resulting address information structures is left uninitialized. |
hints | Object | (optional) Optional hints object that provides additional control over the resolution process. It can contain the following properties:
|
// Resolve all addresses
const addresses = socket.addrinfo('example.org');
// Resolve IPv4 addresses for a given hostname and service
const ipv4addresses = socket.addrinfo('example.com', 'http', { family: socket.AF_INET });
// Resolve IPv6 addresses without specifying a service
const ipv6Addresses = socket.addrinfo('example.com', null, { family: socket.AF_INET6 });
- Source
- See
- Socket Types
- Address Info Flags
connect(host, serviceopt, hintsopt, timeoutopt) → {socket}
Creates a network socket and connects it to the specified host and service.
This high level function combines the functionality of create(), addrinfo() and connect() to simplify connection establishment with the socket module.
Name | Type | Description |
---|---|---|
host | string | | The host to connect to, can be an IP address, hostname, SocketAddress, or an array value returned by iptoarr(). |
service | string | | (optional) The service to connect to, can be a symbolic service name (such as "http") or a port number. Optional if host is specified as SocketAddress. |
hints | Object | (optional) Optional preferences for the socket. It can contain the following properties:
If no hints are not provided, the default socket type preference is set to |
timeout | number | (optional, default: -1 )The timeout in milliseconds for socket connect operations. If set to a negative value, no specifc time limit is imposed and the function will block until either a connection was successfull or the underlying operating system timeout is reached. |
// Resolve host, try to connect to both resulting IPv4 and IPv6 addresses
let conn = socket.connect("example.org", 80);
// Enforce usage of IPv6
let conn = socket.connect("example.com", 80, { family: socket.AF_INET6 });
// Connect a UDP socket
let conn = socket.connect("192.168.1.1", 53, { socktype: socket.SOCK_DGRAM });
// Bypass name resolution by specifying a SocketAddress structure
let conn = socket.connect({ address: "127.0.0.1", port: 9000 });
// Use SocketAddress structure to connect a UNIX domain socket
let conn = socket.connect({ path: "/var/run/daemon.sock" });
- Source
create(domainopt, typeopt, protocolopt) → {socket}nullable
Creates a network socket instance.
This function creates a new network socket with the specified domain and type, determined by one of the modules AF_*
and SOCK_*
constants respectively, and returns the resulting socket instance for use in subsequent socket operations.
The domain argument specifies the protocol family, such as AF_INET or AF_INET6, and defaults to AF_INET if not provided.
The type argument specifies the socket type, such as SOCK_STREAM or SOCK_DGRAM, and defaults to SOCK_STREAM if not provided. It may also be bitwise OR-ed with SOCK_NONBLOCK to enable non-blocking mode or SOCK_CLOEXEC to enable close-on-exec semantics.
The protocol argument may be used to indicate a particular protocol to be used with the socket, and it defaults to 0 (automatically determined protocol) if not provided.
Returns a socket descriptor representing the newly created socket.
Returns null
if an error occurred during socket creation.
Name | Type | Description |
---|---|---|
domain | number | (optional, default: AF_INET )The communication domain for the socket, e.g., AF_INET or AF_INET6. |
type | number | (optional, default: SOCK_STREAM )The socket type, e.g., SOCK_STREAM or SOCK_DGRAM. It may also be bitwise OR-ed with SOCK_NONBLOCK or SOCK_CLOEXEC. |
protocol | number | (optional, default: 0 )The protocol to be used with the socket. |
A socket instance representing the newly created socket.
// Create a TCP socket
const tcp_socket = create(AF_INET, SOCK_STREAM);
// Create a nonblocking IPv6 UDP socket
const udp_socket = create(AF_INET6, SOCK_DGRAM | SOCK_NONBLOCK);
- Source
error(numericopt) → {string|number}
Query error information.
Returns a string containing a description of the last occurred error when the numeric argument is absent or false.
Returns a positive (errno
) or negative (EAI_*
constant) error code number when the numeric argument is true
.
Returns null
if there is no error information.
Name | Type | Description |
---|---|---|
numeric | boolean | (optional) Whether to return a numeric error code ( |
// Trigger socket error by attempting to bind IPv6 address with IPv4 socket
socket.create(socket.AF_INET, socket.SOCK_STREAM, 0).bind("::", 8080);
// Print error (should yield "Address family not supported by protocol")
print(socket.error(), "\n");
// Trigger resolve error
socket.addrinfo("doesnotexist.org");
// Query error code (should yield -2 for EAI_NONAME)
print(socket.error(true), "\n"); //
- Source
listen(host, serviceopt, hintsopt, backlogopt) → {socket}
Binds a listening network socket to the specified host and service.
This high-level function combines the functionality of create(), addrinfo(), bind(), and listen() to simplify setting up a listening socket with the socket module.
Name | Type | Description |
---|---|---|
host | string | | The host to bind to, can be an IP address, hostname, SocketAddress, or an array value returned by iptoarr(). |
service | string | | (optional) The service to listen on, can be a symbolic service name (such as "http") or a port number. Optional if host is specified as SocketAddress. |
hints | Object | (optional) Optional preferences for the socket. It can contain the following properties:
If no hints are provided, the default socket type preference is set to |
backlog | number | (optional, default: 128 )The maximum length of the queue of pending connections. |
// Listen for incoming TCP connections on port 80
let server = socket.listen("localhost", 80);
// Listen on IPv6 address only
let server = socket.listen("machine.local", 8080, { family: socket.AF_INET6 });
// Listen on a UNIX domain socket
let server = socket.listen({ path: "/var/run/server.sock" });
- Source
nameinfo(address, flagsopt) → {Object}nullable
Resolves the given network address into hostname and service name.
The nameinfo()
function provides an API for reverse DNS lookup and service name resolution. It returns an object containing the following properties:
hostname
: The resolved hostname.service
: The resolved service name.
Returns an object representing the resolved hostname and service name. Return null
if an error occurred during resolution.
Name | Type | Description |
---|---|---|
address | string | | The network address to resolve. It can be specified as:
|
flags | number | (optional) Optional flags that provide additional control over the resolution process, specified as bitwise OR-ed number of |
// Resolve a network address into hostname and service name
const result = network.getnameinfo('192.168.1.1:80');
print(result); // { "hostname": "example.com", "service": "http" }
- Source
- See
- Socket Types
- AName Info Constants
poll(timeout, …sockets) → {PollSpec[]}
Polls a number of sockets for state changes.
Returns an array of [socket, flags]
tuples for each socket with pending events. When a tuple is passed as socket argument, it is included as-is into the result tuple array, with the flags entry changed to a bitwise OR-ed value describing the pending events for this socket. When a plain socket instance (or another kind of handle) is passed, a new tuple array is created for this socket within the result tuple array, containing this socket as first and the bitwise OR-ed pending events as second element.
Returns null
if an error occurred.
Name | Type | Description |
---|---|---|
timeout | number | Amount of milliseconds to wait for socket activity before aborting the poll call. If set to |
sockets | socket | | (repeatable) An arbitrary amount of socket arguments. Each argument may be either a plain socket instance (or any other kind of handle implementing a |
let x = socket.connect("example.org", 80);
let y = socket.connect("example.com", 80);
// Pass plain socket arguments
let events = socket.poll(10, x, y);
print(events); // [ [ "<socket 0x7>", 0 ], [ "<socket 0x8>", 0 ] ]
// Passing tuples allows attaching state information and requesting
// different I/O events
let events = socket.poll(10,
[ x, socket.POLLOUT | socket.POLLHUP, "This is example.org" ],
[ y, socket.POLLOUT | socket.POLLHUP, "This is example.com" ]
);
print(events); // [ [ "<socket 0x7>", 4, "This is example.org" ],
// [ "<socket 0x8>", 4, "This is example.com" ] ]
- Source
sockaddr(address) → {socket.SocketAddress}nullable
Parses the provided address value into a socket address representation.
This function parses the given address value into a socket address representation required for a number of socket operations. The address value can be provided in various formats:
- For IPv4 addresses, it can be a string representing the IP address, optionally followed by a port number separated by colon, e.g.
192.168.0.1:8080
. - For IPv6 addresses, it must be an address string enclosed in square brackets if a port number is specified, otherwise the brackets are optional. The address string may also include a scope ID in the form
%ifname
or%number
, e.g.[fe80::1%eth0]:8080
orfe80::1%15
. - Any string value containing a slash is treated as UNIX domain socket path.
- Alternatively, it can be provided as an array returned by iptoarr(), representing the address octets.
- It can also be an object representing a network address, with properties for
address
(the IP address) andport
or a single propertypath
to denote a UNIX domain socket address.
Name | Type | Description |
---|---|---|
address | string | | The address value to parse. |
A socket address representation of the provided address value, or null
if the address could not be parsed.
// Parse an IP address string with port
const address1 = sockaddr('192.168.0.1:8080');
// Parse an IPv6 address string with port and scope identifier
const address2 = sockaddr('[fe80::1%eth0]:8080');
// Parse an array representing an IP address
const address3 = sockaddr([192, 168, 0, 1]);
// Parse a network address object
const address4 = sockaddr({ address: '192.168.0.1', port: 8080 });
// Convert a path value to a UNIX domain socket address
const address5 = sockaddr('/var/run/daemon.sock');
- Source
Type Definitions
AddressInfo: Object
Represents a network address information object returned by addrinfo()
.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
addr | socket.SocketAddress | A socket address structure. | ||
canonname | string | optional | null | (optional, default: null )The canonical hostname associated with the address. |
family | number | The address family (e.g., | ||
flags | number | Additional flags indicating properties of the address. | ||
protocol | number | The protocol number. | ||
socktype | number | The socket type (e.g., |
- Source
PollSpec: Array
Represents a poll state serving as input parameter and return value type for poll()
.
Name | Type | Description |
---|---|---|
0 | socket | The polled socket instance. |
1 | number | Requested or returned status flags of the polled socket instance. |
- Source
Address Families
Constants representing address families and socket domains.
Name | Type | Description |
---|---|---|
AF_UNSPEC | number | Unspecified address family. |
AF_UNIX | number | UNIX domain sockets. |
AF_INET | number | IPv4 Internet protocols. |
AF_INET6 | number | IPv6 Internet protocols. |
AF_PACKET | number | Low-level packet interface. |
- Source
Address Info Flags
The AI_*
flags may be passed as bitwise OR-ed number in the flags property of the hints dictionary argument of addrinfo().
Name | Type | Description |
---|---|---|
AI_ADDRCONFIG | number | Address configuration flag. |
AI_ALL | number | Return IPv4 and IPv6 socket addresses. |
AI_CANONIDN | number | Canonicalize using the IDNA standard. |
AI_CANONNAME | number | Fill in the canonical name field. |
AI_IDN | number | Enable IDN encoding. |
AI_NUMERICHOST | number | Prevent hostname resolution. |
AI_NUMERICSERV | number | Prevent service name resolution. |
AI_PASSIVE | number | Use passive socket. |
AI_V4MAPPED | number | Map IPv6 addresses to IPv4-mapped format. |
- Source
IP Protocol Constants
The IPPROTO_IP
constant specifies the IP protocol number and may be passed as third argument to create() as well as level argument value to getopt() and setopt().
The IP_*
constants are option names recognized by getopt() and setopt(), in conjunction with the IPPROTO_IP
socket level.
Name | Type | Description |
---|---|---|
IPPROTO_IP | number | Dummy protocol for IP. |
IP_ADD_MEMBERSHIP | number | Add an IP group membership. |
IP_ADD_SOURCE_MEMBERSHIP | number | Add an IP group/source membership. |
IP_BIND_ADDRESS_NO_PORT | number | Bind to the device only. |
IP_BLOCK_SOURCE | number | Block IP group/source. |
IP_DROP_MEMBERSHIP | number | Drop an IP group membership. |
IP_DROP_SOURCE_MEMBERSHIP | number | Drop an IP group/source membership. |
IP_FREEBIND | number | Allow binding to an IP address not assigned to a network interface. |
IP_HDRINCL | number | Header is included with data. |
IP_MSFILTER | number | Filter IP multicast source memberships. |
IP_MTU | number | Path MTU discovery. |
IP_MTU_DISCOVER | number | Control Path MTU discovery. |
IP_MULTICAST_ALL | number | Receive all multicast packets. |
IP_MULTICAST_IF | number | Set outgoing interface for multicast packets. |
IP_MULTICAST_LOOP | number | Control multicast packet looping. |
IP_MULTICAST_TTL | number | Set time-to-live for outgoing multicast packets. |
IP_NODEFRAG | number | Don't fragment IP packets. |
IP_OPTIONS | number | Set/get IP options. |
IP_PASSSEC | number | Pass security information. |
IP_PKTINFO | number | Receive packet information. |
IP_RECVERR | number | Receive all ICMP errors. |
IP_RECVOPTS | number | Receive all IP options. |
IP_RECVORIGDSTADDR | number | Receive original destination address of the socket. |
IP_RECVTOS | number | Receive IP TOS. |
IP_RECVTTL | number | Receive IP TTL. |
IP_RETOPTS | number | Set/get IP options. |
IP_ROUTER_ALERT | number | Receive ICMP msgs generated by router. |
IP_TOS | number | IP type of service and precedence. |
IP_TRANSPARENT | number | Transparent proxy support. |
IP_TTL | number | IP time-to-live. |
IP_UNBLOCK_SOURCE | number | Unblock IP group/source. |
- Source
IPv6: Object
The IPPROTO_IPV6
constant specifies the IPv6 protocol number and may be passed as third argument to create() as well as level argument value to getopt() and setopt().
The IPV6_*
constants are option names recognized by getopt() and setopt(), in conjunction with the IPPROTO_IPV6
socket level.
Name | Type | Description |
---|---|---|
IPPROTO_IPV6 | number | The IPv6 protocol. |
IPV6_ADDRFORM | number | Turn an AF_INET6 socket into a socket of a different address family. Only AF_INET is supported. |
IPV6_ADDR_PREFERENCES | number | Specify preferences for address selection. |
IPV6_ADD_MEMBERSHIP | number | Add an IPv6 group membership. |
IPV6_AUTHHDR | number | Set delivery of the authentication header control message for incoming datagrams. |
IPV6_AUTOFLOWLABEL | number | Enable or disable automatic flow labels. |
IPV6_DONTFRAG | number | Control whether the socket allows IPv6 fragmentation. |
IPV6_DROP_MEMBERSHIP | number | Drop an IPv6 group membership. |
IPV6_DSTOPTS | number | Set delivery of the destination options control message for incoming datagrams. |
IPV6_FLOWINFO_SEND | number | Control whether flow information is sent. |
IPV6_FLOWINFO | number | Set delivery of the flow ID control message for incoming datagrams. |
IPV6_FLOWLABEL_MGR | number | Manage flow labels. |
IPV6_FREEBIND | number | Allow binding to an IP address not assigned to a network interface. |
IPV6_HOPLIMIT | number | Set delivery of the hop limit control message for incoming datagrams. |
IPV6_HOPOPTS | number | Set delivery of the hop options control message for incoming datagrams. |
IPV6_JOIN_ANYCAST | number | Join an anycast group. |
IPV6_LEAVE_ANYCAST | number | Leave an anycast group. |
IPV6_MINHOPCOUNT | number | Set the minimum hop count. |
IPV6_MTU | number | Retrieve or set the MTU to be used for the socket. |
IPV6_MTU_DISCOVER | number | Control path-MTU discovery on the socket. |
IPV6_MULTICAST_ALL | number | Control whether the socket receives all multicast packets. |
IPV6_MULTICAST_HOPS | number | Set the multicast hop limit for the socket. |
IPV6_MULTICAST_IF | number | Set the device for outgoing multicast packets on the socket. |
IPV6_MULTICAST_LOOP | number | Control whether the socket sees multicast packets that it has sent itself. |
IPV6_PKTINFO | number | Set delivery of the IPV6_PKTINFO control message on incoming datagrams. |
IPV6_RECVDSTOPTS | number | Control receiving of the destination options control message. |
IPV6_RECVERR | number | Control receiving of asynchronous error options. |
IPV6_RECVFRAGSIZE | number | Control receiving of fragment size. |
IPV6_RECVHOPLIMIT | number | Control receiving of hop limit. |
IPV6_RECVHOPOPTS | number | Control receiving of hop options. |
IPV6_RECVORIGDSTADDR | number | Control receiving of the original destination address. |
IPV6_RECVPATHMTU | number | Control receiving of path MTU. |
IPV6_RECVPKTINFO | number | Control receiving of packet information. |
IPV6_RECVRTHDR | number | Control receiving of routing header. |
IPV6_RECVTCLASS | number | Control receiving of traffic class. |
IPV6_ROUTER_ALERT_ISOLATE | number | Control isolation of router alert messages. |
IPV6_ROUTER_ALERT | number | Pass forwarded packets containing a router alert hop-by-hop option to this socket. |
IPV6_RTHDR | number | Set delivery of the routing header control message for incoming datagrams. |
IPV6_RTHDRDSTOPTS | number | Set delivery of the routing header destination options control message. |
IPV6_TCLASS | number | Set the traffic class. |
IPV6_TRANSPARENT | number | Enable transparent proxy support. |
IPV6_UNICAST_HOPS | number | Set the unicast hop limit for the socket. |
IPV6_UNICAST_IF | number | Set the interface for outgoing unicast packets. |
IPV6_V6ONLY | number | Restrict the socket to sending and receiving IPv6 packets only. |
- Source
Message Flags
PropertiesName | Type | Description |
---|---|---|
MSG_CONFIRM | number | Confirm path validity. |
MSG_DONTROUTE | number | Send without using routing tables. |
MSG_DONTWAIT | number | Enables non-blocking operation. |
MSG_EOR | number | End of record. |
MSG_MORE | number | Sender will send more. |
MSG_NOSIGNAL | number | Do not generate SIGPIPE. |
MSG_OOB | number | Process out-of-band data. |
MSG_FASTOPEN | number | Send data in TCP SYN. |
MSG_CMSG_CLOEXEC | number | Sets the close-on-exec flag on the received file descriptor. |
MSG_ERRQUEUE | number | Receive errors from ICMP. |
MSG_PEEK | number | Peeks at incoming messages. |
MSG_TRUNC | number | Report if datagram truncation occurred. |
MSG_WAITALL | number | Wait for full message. |
- Source
Name Info Constants
The NI_*
flags may be passed as bitwise OR-ed number via the flags argument of nameinfo().
Name | Type | Description |
---|---|---|
NI_DGRAM | number | Datagram socket type. |
NI_IDN | number | Enable IDN encoding. |
NI_NAMEREQD | number | Hostname resolution required. |
NI_NOFQDN | number | Do not force fully qualified domain name. |
NI_NUMERICHOST | number | Return numeric form of the hostname. |
NI_NUMERICSERV | number | Return numeric form of the service name. |
- Source
Packet Socket Constants
The SOL_PACKET
constant specifies the packet socket level and may be passed as level argument value to getopt() and setopt().
Most PACKET_*
constants are option argument values recognized by getopt() and setopt(), in conjunction with the SOL_PACKET
socket level.
The constants PACKET_MR_PROMISC
, PACKET_MR_MULTICAST
and PACKET_MR_ALLMULTI
are used in conjunction with the PACKET_ADD_MEMBERSHIP
and PACKET_DROP_MEMBERSHIP
options to specify the packet socket receive mode.
The constants PACKET_HOST
, PACKET_BROADCAST
, PACKET_MULTICAST
, PACKET_OTHERHOST
and PACKET_OUTGOING
may be used as packet_type value in socket address structures.
Name | Type | Description |
---|---|---|
SOL_PACKET | number | Socket options at the packet API level. |
PACKET_ADD_MEMBERSHIP | number | Add a multicast group membership. |
PACKET_DROP_MEMBERSHIP | number | Drop a multicast group membership. |
PACKET_AUXDATA | number | Receive auxiliary data (packet info). |
PACKET_FANOUT | number | Configure packet fanout. |
PACKET_LOSS | number | Retrieve the current packet loss statistics. |
PACKET_RESERVE | number | Reserve space for packet headers. |
PACKET_RX_RING | number | Configure a receive ring buffer. |
PACKET_STATISTICS | number | Retrieve packet statistics. |
PACKET_TIMESTAMP | number | Retrieve packet timestamps. |
PACKET_TX_RING | number | Configure a transmit ring buffer. |
PACKET_VERSION | number | Set the packet protocol version. |
PACKET_QDISC_BYPASS | number | Bypass queuing discipline for outgoing packets. |
PACKET_MR_PROMISC | number | Enable promiscuous mode. |
PACKET_MR_MULTICAST | number | Receive multicast packets. |
PACKET_MR_ALLMULTI | number | Receive all multicast packets. |
PACKET_HOST | number | Receive packets destined for this host. |
PACKET_BROADCAST | number | Receive broadcast packets. |
PACKET_MULTICAST | number | Receive multicast packets. |
PACKET_OTHERHOST | number | Receive packets destined for other hosts. |
PACKET_OUTGOING | number | Transmit packets. |
- Source
Poll Event Constants
The following constants represent event types for polling operations and are set or returned as part of a PollSpec tuple by the poll() function. When passed via an argument PollSpec to poll()
, they specify the I/O events to watch for on the corresponding handle. When appearing in a PollSpec returned by poll()
, they specify the I/O events that occurred on a watched handle.
Name | Type | Description |
---|---|---|
POLLIN | number | Data available to read. |
POLLPRI | number | Priority data available to read. |
POLLOUT | number | Writable data available. |
POLLERR | number | Error condition. |
POLLHUP | number | Hang up. |
POLLNVAL | number | Invalid request. |
POLLRDHUP | number | Peer closed or shutdown writing. |
- Source
Shutdown Constants
The SHUT_*
constants are passed as argument to the shutdown() function to specify which direction of a full duplex connection to shut down.
Name | Type | Description |
---|---|---|
SHUT_RD | number | Disallow further receptions. |
SHUT_WR | number | Disallow further transmissions. |
SHUT_RDWR | number | Disallow further receptions and transmissions. |
- Source
Socket Option Constants
The SOL_SOCKET
constant is passed as level argument to the getopt() and setopt() functions in order to set or retrieve generic socket option values.
The SO_*
constants are passed as option argument in conjunction with the SOL_SOCKET
level to specify the specific option to get or set on the socket.
Name | Type | Description |
---|---|---|
SOL_SOCKET | number | Socket options at the socket API level. |
SO_ACCEPTCONN | number | Reports whether socket listening is enabled. |
SO_ATTACH_BPF | number | Attach BPF program to socket. |
SO_ATTACH_FILTER | number | Attach a socket filter. |
SO_ATTACH_REUSEPORT_CBPF | number | Attach BPF program for cgroup and skb program reuseport hook. |
SO_ATTACH_REUSEPORT_EBPF | number | Attach eBPF program for cgroup and skb program reuseport hook. |
SO_BINDTODEVICE | number | Bind socket to a specific interface. |
SO_BROADCAST | number | Allow transmission of broadcast messages. |
SO_BUSY_POLL | number | Enable busy polling. |
SO_DEBUG | number | Enable socket debugging. |
SO_DETACH_BPF | number | Detach BPF program from socket. |
SO_DETACH_FILTER | number | Detach a socket filter. |
SO_DOMAIN | number | Retrieves the domain of the socket. |
SO_DONTROUTE | number | Send packets directly without routing. |
SO_ERROR | number | Retrieves and clears the error status for the socket. |
SO_INCOMING_CPU | number | Retrieves the CPU number on which the last packet was received. |
SO_INCOMING_NAPI_ID | number | Retrieves the NAPI ID of the device. |
SO_KEEPALIVE | number | Enable keep-alive packets. |
SO_LINGER | number | Set linger on close. |
SO_LOCK_FILTER | number | Set or get the socket filter lock state. |
SO_MARK | number | Set the mark for packets sent through the socket. |
SO_OOBINLINE | number | Enables out-of-band data to be received in the normal data stream. |
SO_PASSCRED | number | Enable the receiving of SCM_CREDENTIALS control messages. |
SO_PASSSEC | number | Enable the receiving of security context. |
SO_PEEK_OFF | number | Returns the number of bytes in the receive buffer without removing them. |
SO_PEERCRED | number | Retrieves the credentials of the foreign peer. |
SO_PEERSEC | number | Retrieves the security context of the foreign peer. |
SO_PRIORITY | number | Set the protocol-defined priority for all packets. |
SO_PROTOCOL | number | Retrieves the protocol number. |
SO_RCVBUF | number | Set the receive buffer size. |
SO_RCVBUFFORCE | number | Set the receive buffer size forcefully. |
SO_RCVLOWAT | number | Set the minimum number of bytes to process for input operations. |
SO_RCVTIMEO | number | Set the timeout for receiving data. |
SO_REUSEADDR | number | Allow the socket to be bound to an address that is already in use. |
SO_REUSEPORT | number | Enable duplicate address and port bindings. |
SO_RXQ_OVFL | number | Reports if the receive queue has overflown. |
SO_SNDBUF | number | Set the send buffer size. |
SO_SNDBUFFORCE | number | Set the send buffer size forcefully. |
SO_SNDLOWAT | number | Set the minimum number of bytes to process for output operations. |
SO_SNDTIMEO | number | Set the timeout for sending data. |
SO_TIMESTAMP | number | Enable receiving of timestamps. |
SO_TIMESTAMPNS | number | Enable receiving of nanosecond timestamps. |
SO_TYPE | number | Retrieves the type of the socket (e.g., SOCK_STREAM). |
- Source
Socket Types
The SOCK_*
type and flag constants are used by create() to specify the type of socket to open. The accept() function recognizes the SOCK_NONBLOCK
and SOCK_CLOEXEC
flags and applies them to accepted peer sockets.
Name | Type | Description |
---|---|---|
SOCK_STREAM | number | Provides sequenced, reliable, two-way, connection-based byte streams. |
SOCK_DGRAM | number | Supports datagrams (connectionless, unreliable messages of a fixed maximum length). |
SOCK_RAW | number | Provides raw network protocol access. |
SOCK_PACKET | number | Obsolete and should not be used. |
SOCK_NONBLOCK | number | Enables non-blocking operation. |
SOCK_CLOEXEC | number | Sets the close-on-exec flag on the new file descriptor. |
- Source
TCP Protocol Constants
The IPPROTO_TCP
constant specifies the TCP protocol number and may be passed as third argument to create() as well as level argument value to getopt() and setopt().
The TCP_*
constants are option argument values recognized by getopt() and setopt(), in conjunction with the IPPROTO_TCP
socket level.
Name | Type | Description |
---|---|---|
IPPROTO_TCP | number | TCP protocol. |
TCP_CONGESTION | number | Set the congestion control algorithm. |
TCP_CORK | number | Delay packet transmission until full-sized packets are available. |
TCP_DEFER_ACCEPT | number | Delay accepting incoming connections until data arrives. |
TCP_FASTOPEN | number | Enable TCP Fast Open. |
TCP_FASTOPEN_CONNECT | number | Perform TFO connect. |
TCP_INFO | number | Retrieve TCP statistics. |
TCP_KEEPCNT | number | Number of keepalive probes. |
TCP_KEEPIDLE | number | Time before keepalive probes begin. |
TCP_KEEPINTVL | number | Interval between keepalive probes. |
TCP_LINGER2 | number | Lifetime of orphaned FIN_WAIT2 state sockets. |
TCP_MAXSEG | number | Maximum segment size. |
TCP_NODELAY | number | Disable Nagle's algorithm. |
TCP_QUICKACK | number | Enable quick ACKs. |
TCP_SYNCNT | number | Number of SYN retransmits. |
TCP_USER_TIMEOUT | number | Set the user timeout. |
TCP_WINDOW_CLAMP | number | Set the maximum window. |
- Source
UDP Protocol Constants
The IPPROTO_UDP
constant specifies the UDP protocol number and may be passed as third argument to create() as well as level argument value to getopt() and setopt().
The UDP_*
constants are option argument values recognized by getopt() and setopt(), in conjunction with the IPPROTO_UDP
socket level.
Name | Type | Description |
---|---|---|
IPPROTO_UDP | number | UDP protocol. |
UDP_CORK | number | Cork data until flush. |
- Source