1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00
Files
archived-php-src/ext/sockets/tests/socket_tcp_keepalive.phpt
Jakub Zelenka 2d105afce7 socket: properly expose TCP keepalive constants (#20376)
This fixes missing constants on MacOS where TCP_KEEPALIVE,
TCP_KEEPINTVL and TCP_KEEPCNT are available. Currently only
TCP_KEEPALIVE is defined there.
2025-11-03 19:12:09 +01:00

53 lines
1.4 KiB
PHP

--TEST--
Test SO_KEEPALIVE and TCP keepalive constants
--EXTENSIONS--
sockets
--SKIPIF--
<?php
if (!defined('TCP_KEEPIDLE') && !defined('TCP_KEEPALIVE')) {
die('skip TCP_KEEPIDLE/TCP_KEEPALIVE not available');
}
if (!defined('TCP_KEEPINTVL')) {
die('skip TCP_KEEPINTVL not available');
}
if (!defined('TCP_KEEPCNT')) {
die('skip TCP_KEEPCNT not available');
}
?>
--FILE--
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$socket) {
die("socket failed");
}
socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
$keepalive = socket_get_option($socket, SOL_SOCKET, SO_KEEPALIVE);
echo "SO_KEEPALIVE: " . ($keepalive ? "enabled" : "disabled") . "\n";
if (defined('TCP_KEEPIDLE')) {
socket_set_option($socket, SOL_TCP, TCP_KEEPIDLE, 60);
$idle = socket_get_option($socket, SOL_TCP, TCP_KEEPIDLE);
echo "TCP_KEEPIDLE: $idle\n";
} else {
socket_set_option($socket, SOL_TCP, TCP_KEEPALIVE, 60);
$idle = socket_get_option($socket, SOL_TCP, TCP_KEEPALIVE);
echo "TCP_KEEPIDLE: $idle\n";
}
socket_set_option($socket, SOL_TCP, TCP_KEEPINTVL, 10);
$intvl = socket_get_option($socket, SOL_TCP, TCP_KEEPINTVL);
echo "TCP_KEEPINTVL: $intvl\n";
socket_set_option($socket, SOL_TCP, TCP_KEEPCNT, 5);
$cnt = socket_get_option($socket, SOL_TCP, TCP_KEEPCNT);
echo "TCP_KEEPCNT: $cnt\n";
socket_close($socket);
?>
--EXPECT--
SO_KEEPALIVE: enabled
TCP_KEEPIDLE: 60
TCP_KEEPINTVL: 10
TCP_KEEPCNT: 5