Socket context options
Socket context options — Socket context option listing
Description
Socket context options are available for all wrappers that work over
sockets, like tcp, http and
ftp.
Options
bindto
-
Used to specify the IP address (either IPv4 or IPv6) and/or the
port number that PHP will use to access the network. The syntax
is ip:port for IPv4 addresses, and
[ip]:port for IPv6 addresses.
Setting the IP or the port to 0 will let the system
choose the IP and/or port.
Note:
As FTP creates two socket connections during normal operation,
the port number cannot be specified using this option.
backlog
-
Used to limit the number of outstanding connections in the
socket's listen queue.
Note:
This is only applicable to stream_socket_server().
ipv6_v6only
-
Overrides the OS default regarding mapping IPv4 into IPv6.
Note:
This is important in particular when trying to listen on IPv4 addresses
separately while there exists a binding on [::].
This is only applicable to stream_socket_server().
so_reuseport
-
Allows multiple bindings to a same ip:port pair, even from separate processes.
Note:
This is only applicable to stream_socket_server().
so_reuseaddr
-
Whether to set SO_REUSEADDR on the socket, which
allows binding to an ip:port pair while connections of a previous
socket bound to it are still in the TIME_WAIT state,
for example when a server is restarted.
On Windows, it also allows binding to an ip:port pair that another
socket is bound to.
Defaults to true.
When set to false, SO_REUSEADDR is not set,
and on Windows SO_EXCLUSIVEADDRUSE is set instead,
which prevents other sockets from binding to the same ip:port pair.
Note:
This is only applicable to stream_socket_server().
so_broadcast
-
Enables sending and receiving data to/from broadcast addresses.
Note:
This is only applicable to stream_socket_server().
so_rcvbuf
-
The size of the receive buffer of the socket in bytes
(SO_RCVBUF).
The value must be an integer between 1 and
2147483647; otherwise, opening the stream fails.
The operating system may round, cap, or otherwise adjust the requested
size.
Setting this option may also turn off the automatic sizing of the
buffer by the operating system.
Note:
This is only applicable to TCP and UDP sockets.
so_sndbuf
-
The size of the send buffer of the socket in bytes
(SO_SNDBUF).
The value must be an integer between 1 and
2147483647; otherwise, opening the stream fails.
The operating system may round, cap, or otherwise adjust the requested
size.
Setting this option may also turn off the automatic sizing of the
buffer by the operating system.
Note:
This is only applicable to TCP and UDP sockets.
so_linger
-
The number of seconds for which closing the socket waits for unsent
data to be sent (SO_LINGER).
A positive value enables lingering for that many seconds.
Zero or a negative value disables lingering, so that closing the socket
returns immediately and the remaining data is sent in the background.
Values above 65535 are treated as
65535.
With stream_socket_server(), it is set on the
listening socket and inherited by the accepted connections.
Note:
This is only applicable to TCP sockets.
so_keepalive
-
Setting this option to true enables TCP keepalive
(SO_KEEPALIVE): probes are sent over a connection
that has been idle for some time, so that a peer which is no longer
reachable is detected and the connection is dropped.
The probes are configured by tcp_keepidle,
tcp_keepintvl, and
tcp_keepcnt; the settings of the operating
system are used for those that are not set.
With stream_socket_server(), the keepalive options
are set on the listening socket and inherited by the accepted
connections.
Note:
This is only applicable to TCP sockets.
tcp_keepidle
-
The number of seconds a connection must be idle before the first
keepalive probe is sent (TCP_KEEPIDLE, or
TCP_KEEPALIVE on macOS).
Note:
This is only applicable to TCP sockets, and only has an effect if
so_keepalive is enabled.
It is ignored on platforms that do not support it.
tcp_keepintvl
-
The number of seconds between consecutive keepalive probes
(TCP_KEEPINTVL).
Note:
This is only applicable to TCP sockets, and only has an effect if
so_keepalive is enabled.
It is ignored on platforms that do not support it.
tcp_keepcnt
-
The number of unanswered keepalive probes after which the connection
is dropped (TCP_KEEPCNT).
Note:
This is only applicable to TCP sockets, and only has an effect if
so_keepalive is enabled.
It is ignored on platforms that do not support it.
tcp_nodelay
-
Setting this option to true will set SOL_TCP,NO_DELAY=1
appropriately, thus disabling the TCP Nagle algorithm.
Examples
Example #1 Basic bindto usage example
<?php
// connect to the internet using the '192.168.0.100' IP
$opts = array(
'socket' => array(
'bindto' => '192.168.0.100:0',
),
);
// connect to the internet using the '192.168.0.100' IP and port '7000'
$opts = array(
'socket' => array(
'bindto' => '192.168.0.100:7000',
),
);
// connect to the internet using the '2001:db8::1' IPv6 address
// and port '7000'
$opts = array(
'socket' => array(
'bindto' => '[2001:db8::1]:7000',
),
);
// connect to the internet using port '7000'
$opts = array(
'socket' => array(
'bindto' => '0:7000',
),
);
// create the context...
$context = stream_context_create($opts);
// ...and use it to fetch the data
echo file_get_contents('http://www.example.com', false, $context);
?>
Example #2 Enabling TCP keepalive, as of PHP 8.6.0
<?php
// Send the first keepalive probe after 60 seconds of inactivity,
// then one every 10 seconds, and drop the connection
// after 3 unanswered probes
$opts = [
'socket' => [
'so_keepalive' => true,
'tcp_keepidle' => 60,
'tcp_keepintvl' => 10,
'tcp_keepcnt' => 3,
],
];
$context = stream_context_create($opts);
$fp = stream_socket_client('tcp://www.example.com:80', context: $context);
?>