[doc-en] master: PHP 8.6: Document the new socket context options (#6168)
| From: | Asish Kumar via GitHub | Date: | Sat, 28 Nov 2026 08:38:33 +0000 |
| Subject: | [doc-en] master: PHP 8.6: Document the new socket context options (#6168) | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-24032@lists.php.net to get a copy of this message | ||
Author: Asish Kumar (officialasishkumar)
Committer: GitHub (web-flow)
Pusher: lacatoire
Date: 2026-11-28T09:38:29+01:00
Commit: https://github.com/php/doc-en/commit/57a75bf8b672de9bc9ff381457a9138639fbf798
Raw diff: https://github.com/php/doc-en/commit/57a75bf8b672de9bc9ff381457a9138639fbf798.diff
PHP 8.6: Document the new socket context options (#6168)
PHP 8.6 added eight socket context options. The 8.6 migration guide
already lists them and links to the socket context options page, but
that page did not describe any of them. Add an entry for each option,
based on main/streams/xp_socket.c and main/network.c:
- so_reuseaddr: SO_REUSEADDR stays enabled by default. Setting the
option to false disables it, and on Windows sets SO_EXCLUSIVEADDRUSE
instead. It only applies to stream_socket_server().
- so_rcvbuf and so_sndbuf: the receive and send buffer sizes of TCP
and UDP sockets. Values outside 1 to 2147483647 make opening the
stream fail, and the operating system may adjust the size.
- so_linger: a positive number of seconds enables lingering, zero or a
negative value disables it, and values above 65535 are clamped.
- so_keepalive, tcp_keepidle, tcp_keepintvl and tcp_keepcnt: enable TCP
keepalive and tune the probes. TCP_KEEPALIVE is used on macOS, and
each option is ignored where the platform does not support it.
For stream_socket_server(), so_linger and the keepalive options are set
on the listening socket and inherited by the accepted connections, as
the php-src tests check.
Also add an 8.6.0 changelog row and an example that enables keepalive
on a client connection.
Changed paths:
M language/context/socket.xml
Diff:
diff --git a/language/context/socket.xml b/language/context/socket.xml
index 641df2fd8c20..518faf57021c 100644
--- a/language/context/socket.xml
+++ b/language/context/socket.xml
@@ -83,6 +83,30 @@
</note>
</listitem>
</varlistentry>
+ <varlistentry xml:id="context.socket.so_reuseaddr">
+ <term><parameter>so_reuseaddr</parameter></term>
+ <listitem>
+ <simpara>
+ Whether to set <literal>SO_REUSEADDR</literal> on the socket, which
+ allows binding to an ip:port pair while connections of a previous
+ socket bound to it are still in the <literal>TIME_WAIT</literal> 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.
+ </simpara>
+ <simpara>
+ Defaults to &true;.
+ When set to &false;, <literal>SO_REUSEADDR</literal> is not set,
+ and on Windows <literal>SO_EXCLUSIVEADDRUSE</literal> is set instead,
+ which prevents other sockets from binding to the same ip:port pair.
+ </simpara>
+ <note>
+ <simpara>
+ This is only applicable to <function>stream_socket_server</function>.
+ </simpara>
+ </note>
+ </listitem>
+ </varlistentry>
<varlistentry xml:id="context.socket.so_broadcast">
<term><parameter>so_broadcast</parameter></term>
<listitem>
@@ -96,6 +120,147 @@
</note>
</listitem>
</varlistentry>
+ <varlistentry xml:id="context.socket.so_rcvbuf">
+ <term><parameter>so_rcvbuf</parameter></term>
+ <listitem>
+ <simpara>
+ The size of the receive buffer of the socket in bytes
+ (<literal>SO_RCVBUF</literal>).
+ The value must be an integer between <literal>1</literal> and
+ <literal>2147483647</literal>; otherwise, opening the stream fails.
+ </simpara>
+ <simpara>
+ 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.
+ </simpara>
+ <note>
+ <simpara>
+ This is only applicable to TCP and UDP sockets.
+ </simpara>
+ </note>
+ </listitem>
+ </varlistentry>
+ <varlistentry xml:id="context.socket.so_sndbuf">
+ <term><parameter>so_sndbuf</parameter></term>
+ <listitem>
+ <simpara>
+ The size of the send buffer of the socket in bytes
+ (<literal>SO_SNDBUF</literal>).
+ The value must be an integer between <literal>1</literal> and
+ <literal>2147483647</literal>; otherwise, opening the stream fails.
+ </simpara>
+ <simpara>
+ 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.
+ </simpara>
+ <note>
+ <simpara>
+ This is only applicable to TCP and UDP sockets.
+ </simpara>
+ </note>
+ </listitem>
+ </varlistentry>
+ <varlistentry xml:id="context.socket.so_linger">
+ <term><parameter>so_linger</parameter></term>
+ <listitem>
+ <simpara>
+ The number of seconds for which closing the socket waits for unsent
+ data to be sent (<literal>SO_LINGER</literal>).
+ 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 <literal>65535</literal> are treated as
+ <literal>65535</literal>.
+ </simpara>
+ <simpara>
+ With <function>stream_socket_server</function>, it is set on the
+ listening socket and inherited by the accepted connections.
+ </simpara>
+ <note>
+ <simpara>
+ This is only applicable to TCP sockets.
+ </simpara>
+ </note>
+ </listitem>
+ </varlistentry>
+ <varlistentry xml:id="context.socket.so_keepalive">
+ <term><parameter>so_keepalive</parameter></term>
+ <listitem>
+ <simpara>
+ Setting this option to &true; enables TCP keepalive
+ (<literal>SO_KEEPALIVE</literal>): 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 <parameter>tcp_keepidle</parameter>,
+ <parameter>tcp_keepintvl</parameter>, and
+ <parameter>tcp_keepcnt</parameter>; the settings of the operating
+ system are used for those that are not set.
+ </simpara>
+ <simpara>
+ With <function>stream_socket_server</function>, the keepalive options
+ are set on the listening socket and inherited by the accepted
+ connections.
+ </simpara>
+ <note>
+ <simpara>
+ This is only applicable to TCP sockets.
+ </simpara>
+ </note>
+ </listitem>
+ </varlistentry>
+ <varlistentry xml:id="context.socket.tcp_keepidle">
+ <term><parameter>tcp_keepidle</parameter></term>
+ <listitem>
+ <simpara>
+ The number of seconds a connection must be idle before the first
+ keepalive probe is sent (<literal>TCP_KEEPIDLE</literal>, or
+ <literal>TCP_KEEPALIVE</literal> on macOS).
+ </simpara>
+ <note>
+ <simpara>
+ This is only applicable to TCP sockets, and only has an effect if
+ <parameter>so_keepalive</parameter> is enabled.
+ It is ignored on platforms that do not support it.
+ </simpara>
+ </note>
+ </listitem>
+ </varlistentry>
+ <varlistentry xml:id="context.socket.tcp_keepintvl">
+ <term><parameter>tcp_keepintvl</parameter></term>
+ <listitem>
+ <simpara>
+ The number of seconds between consecutive keepalive probes
+ (<literal>TCP_KEEPINTVL</literal>).
+ </simpara>
+ <note>
+ <simpara>
+ This is only applicable to TCP sockets, and only has an effect if
+ <parameter>so_keepalive</parameter> is enabled.
+ It is ignored on platforms that do not support it.
+ </simpara>
+ </note>
+ </listitem>
+ </varlistentry>
+ <varlistentry xml:id="context.socket.tcp_keepcnt">
+ <term><parameter>tcp_keepcnt</parameter></term>
+ <listitem>
+ <simpara>
+ The number of unanswered keepalive probes after which the connection
+ is dropped (<literal>TCP_KEEPCNT</literal>).
+ </simpara>
+ <note>
+ <simpara>
+ This is only applicable to TCP sockets, and only has an effect if
+ <parameter>so_keepalive</parameter> is enabled.
+ It is ignored on platforms that do not support it.
+ </simpara>
+ </note>
+ </listitem>
+ </varlistentry>
<varlistentry xml:id="context.socket.tcp_nodelay">
<term><parameter>tcp_nodelay</parameter></term>
<listitem>
@@ -121,6 +286,17 @@
</row>
</thead>
<tbody>
+ <row>
+ <entry>8.6.0</entry>
+ <entry>
+ Added <parameter>so_reuseaddr</parameter>,
+ <parameter>so_rcvbuf</parameter>, <parameter>so_sndbuf</parameter>,
+ <parameter>so_linger</parameter>,
<parameter>so_keepalive</parameter>,
+ <parameter>tcp_keepidle</parameter>,
+ <parameter>tcp_keepintvl</parameter>, and
+ <parameter>tcp_keepcnt</parameter>.
+ </entry>
+ </row>
<row>
<entry>7.1.0</entry>
<entry>
@@ -186,6 +362,30 @@ $context = stream_context_create($opts);
// ...and use it to fetch the data
echo file_get_contents('http://www.example.com', false, $context);
+?>
+]]>
+ </programlisting>
+ </example>
+ <example xml:id="context.socket.example-keepalive">
+ <title>Enabling TCP keepalive, as of PHP 8.6.0</title>
+ <programlisting role="php">
+<![CDATA[
+<?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);
?>
]]>
</programlisting>