|
| 1 | +--TEST-- |
| 2 | +stream_socket_server() and stream_socket_client() keepalive option test with string values |
| 3 | +--EXTENSIONS-- |
| 4 | +sockets |
| 5 | +--SKIPIF-- |
| 6 | +<?php |
| 7 | +if (!defined('TCP_KEEPIDLE') && !defined('TCP_KEEPALIVE')) { |
| 8 | + die('skip TCP_KEEPIDLE/TCP_KEEPALIVE not available'); |
| 9 | +} |
| 10 | +if (!defined('TCP_KEEPINTVL')) { |
| 11 | + die('skip TCP_KEEPINTVL not available'); |
| 12 | +} |
| 13 | +if (!defined('TCP_KEEPCNT')) { |
| 14 | + die('skip TCP_KEEPCNT not available'); |
| 15 | +} |
| 16 | +?> |
| 17 | +--FILE-- |
| 18 | +<?php |
| 19 | +// Test server with SO_KEEPALIVE enabled |
| 20 | +$server_context = stream_context_create([ |
| 21 | + 'socket' => [ |
| 22 | + 'so_keepalive' => true, |
| 23 | + 'tcp_keepidle' => '80', |
| 24 | + 'tcp_keepintvl' => '12', |
| 25 | + 'tcp_keepcnt' => '6', |
| 26 | + ] |
| 27 | +]); |
| 28 | + |
| 29 | +$server = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr, |
| 30 | + STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $server_context); |
| 31 | + |
| 32 | +if (!$server) { |
| 33 | + die('Unable to create server'); |
| 34 | +} |
| 35 | + |
| 36 | +$addr = stream_socket_get_name($server, false); |
| 37 | +$port = (int)substr(strrchr($addr, ':'), 1); |
| 38 | + |
| 39 | +// Test client with SO_KEEPALIVE enabled |
| 40 | +$client_context = stream_context_create([ |
| 41 | + 'socket' => [ |
| 42 | + 'so_keepalive' => true, |
| 43 | + 'tcp_keepidle' => '31', |
| 44 | + 'tcp_keepintvl' => '6', |
| 45 | + 'tcp_keepcnt' => '4', |
| 46 | + ] |
| 47 | +]); |
| 48 | + |
| 49 | +$client = stream_socket_client("tcp://127.0.0.1:$port", $errno, $errstr, 30, |
| 50 | + STREAM_CLIENT_CONNECT, $client_context); |
| 51 | + |
| 52 | +if (!$client) { |
| 53 | + die('Unable to create client'); |
| 54 | +} |
| 55 | + |
| 56 | +$accepted = stream_socket_accept($server, 1); |
| 57 | + |
| 58 | +if (!$accepted) { |
| 59 | + die('Unable to accept connection'); |
| 60 | +} |
| 61 | + |
| 62 | +// Verify server side (accepted connection) |
| 63 | +$server_sock = socket_import_stream($accepted); |
| 64 | +$server_keepalive = socket_get_option($server_sock, SOL_SOCKET, SO_KEEPALIVE); |
| 65 | +echo "Server SO_KEEPALIVE: " . ($server_keepalive ? "enabled" : "disabled") . "\n"; |
| 66 | + |
| 67 | +if (defined('TCP_KEEPIDLE')) { |
| 68 | + $server_idle = socket_get_option($server_sock, SOL_TCP, TCP_KEEPIDLE); |
| 69 | + echo "Server TCP_KEEPIDLE: $server_idle\n"; |
| 70 | +} else { |
| 71 | + $server_idle = socket_get_option($server_sock, SOL_TCP, TCP_KEEPALIVE); |
| 72 | + echo "Server TCP_KEEPIDLE: $server_idle\n"; |
| 73 | +} |
| 74 | + |
| 75 | +$server_intvl = socket_get_option($server_sock, SOL_TCP, TCP_KEEPINTVL); |
| 76 | +echo "Server TCP_KEEPINTVL: $server_intvl\n"; |
| 77 | + |
| 78 | +$server_cnt = socket_get_option($server_sock, SOL_TCP, TCP_KEEPCNT); |
| 79 | +echo "Server TCP_KEEPCNT: $server_cnt\n"; |
| 80 | + |
| 81 | +// Verify client side |
| 82 | +$client_sock = socket_import_stream($client); |
| 83 | +$client_keepalive = socket_get_option($client_sock, SOL_SOCKET, SO_KEEPALIVE); |
| 84 | +echo "Client SO_KEEPALIVE: " . ($client_keepalive ? "enabled" : "disabled") . "\n"; |
| 85 | + |
| 86 | +if (defined('TCP_KEEPIDLE')) { |
| 87 | + $client_idle = socket_get_option($client_sock, SOL_TCP, TCP_KEEPIDLE); |
| 88 | + echo "Client TCP_KEEPIDLE: $client_idle\n"; |
| 89 | +} else { |
| 90 | + $client_idle = socket_get_option($client_sock, SOL_TCP, TCP_KEEPALIVE); |
| 91 | + echo "Client TCP_KEEPIDLE: $client_idle\n"; |
| 92 | +} |
| 93 | + |
| 94 | +$client_intvl = socket_get_option($client_sock, SOL_TCP, TCP_KEEPINTVL); |
| 95 | +echo "Client TCP_KEEPINTVL: $client_intvl\n"; |
| 96 | + |
| 97 | +$client_cnt = socket_get_option($client_sock, SOL_TCP, TCP_KEEPCNT); |
| 98 | +echo "Client TCP_KEEPCNT: $client_cnt\n"; |
| 99 | + |
| 100 | +fclose($accepted); |
| 101 | +fclose($client); |
| 102 | +fclose($server); |
| 103 | + |
| 104 | +// Test server with SO_KEEPALIVE disabled |
| 105 | +$server2_context = stream_context_create(['socket' => ['so_keepalive' => false]]); |
| 106 | +$server2 = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr, |
| 107 | + STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $server2_context); |
| 108 | + |
| 109 | +$addr2 = stream_socket_get_name($server2, false); |
| 110 | +$port2 = (int)substr(strrchr($addr2, ':'), 1); |
| 111 | + |
| 112 | +$client2 = stream_socket_client("tcp://127.0.0.1:$port2"); |
| 113 | +$accepted2 = stream_socket_accept($server2, 1); |
| 114 | + |
| 115 | +$server2_sock = socket_import_stream($accepted2); |
| 116 | +$server2_keepalive = socket_get_option($server2_sock, SOL_SOCKET, SO_KEEPALIVE); |
| 117 | +echo "Server disabled SO_KEEPALIVE: " . ($server2_keepalive ? "enabled" : "disabled") . "\n"; |
| 118 | + |
| 119 | +fclose($accepted2); |
| 120 | +fclose($client2); |
| 121 | +fclose($server2); |
| 122 | + |
| 123 | +// Test client with SO_KEEPALIVE disabled |
| 124 | +$server3 = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr, |
| 125 | + STREAM_SERVER_BIND | STREAM_SERVER_LISTEN); |
| 126 | + |
| 127 | +$addr3 = stream_socket_get_name($server3, false); |
| 128 | +$port3 = (int)substr(strrchr($addr3, ':'), 1); |
| 129 | + |
| 130 | +$client3_context = stream_context_create(['socket' => ['so_keepalive' => false]]); |
| 131 | +$client3 = stream_socket_client("tcp://127.0.0.1:$port3", $errno, $errstr, 30, |
| 132 | + STREAM_CLIENT_CONNECT, $client3_context); |
| 133 | + |
| 134 | +$client3_sock = socket_import_stream($client3); |
| 135 | +$client3_keepalive = socket_get_option($client3_sock, SOL_SOCKET, SO_KEEPALIVE); |
| 136 | +echo "Client disabled SO_KEEPALIVE: " . ($client3_keepalive ? "enabled" : "disabled") . "\n"; |
| 137 | + |
| 138 | +fclose($client3); |
| 139 | +fclose($server3); |
| 140 | +?> |
| 141 | +--EXPECT-- |
| 142 | +Server SO_KEEPALIVE: enabled |
| 143 | +Server TCP_KEEPIDLE: 80 |
| 144 | +Server TCP_KEEPINTVL: 12 |
| 145 | +Server TCP_KEEPCNT: 6 |
| 146 | +Client SO_KEEPALIVE: enabled |
| 147 | +Client TCP_KEEPIDLE: 31 |
| 148 | +Client TCP_KEEPINTVL: 6 |
| 149 | +Client TCP_KEEPCNT: 4 |
| 150 | +Server disabled SO_KEEPALIVE: disabled |
| 151 | +Client disabled SO_KEEPALIVE: disabled |
0 commit comments