These tests depend on being able to bind port 31337.
Try a range of ports until one binds or we give up trying.
if (!$server) {
die('Unable to create AF_INET socket [server]');
}
- if (!socket_bind($server, '127.0.0.1', 31337)) {
- die('Unable to bind to 127.0.0.1:31337');
+ $bound = false;
+ for($port = 31337; $port < 31357; ++$port) {
+ if (socket_bind($server, '127.0.0.1', $port)) {
+ $bound = true;
+ break;
+ }
+ }
+ if (!$bound) {
+ die("Unable to bind to 127.0.0.1");
}
if (!socket_listen($server, 2)) {
die('Unable to listen on socket');
if (!$client) {
die('Unable to create AF_INET socket [client]');
}
- if (!socket_connect($client, '127.0.0.1', 31337)) {
+ if (!socket_connect($client, '127.0.0.1', $port)) {
die('Unable to connect to server socket');
}
if (!$server) {
die('Unable to create AF_INET6 socket [server]');
}
- if (!socket_bind($server, '::1', 31337)) {
- die('Unable to bind to [::1]:31337');
+ $bound = false;
+ for($port = 31337; $port < 31357; ++$port) {
+ if (socket_bind($server, '::1', $port)) {
+ $bound = true;
+ break;
+ }
+ }
+ if (!$bound)
+ die("Unable to bind to [::1]:$port");
}
if (!socket_listen($server, 2)) {
die('Unable to listen on socket');
if (!$client) {
die('Unable to create AF_INET6 socket [client]');
}
- if (!socket_connect($client, '::1', 31337)) {
+ if (!socket_connect($client, '::1', $port)) {
die('Unable to connect to server socket');
}
/* Bind and connect sockets to localhost */
$localhost = '127.0.0.1';
- /* Hold the port associated to address */
- $port = 31337;
-
/* Setup socket server */
$server = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
if (!$server) {
die('Unable to create AF_INET socket [server]');
}
-
- if (!socket_bind($server, $localhost, $port)) {
- die('Unable to bind to '.$localhost.':'.$port);
+
+ $minport = 31337;
+ $maxport = 31356;
+ $bound = false;
+ for($port = $minport; $port <= $maxport; ++$port) {
+ if (socket_bind($server, $localhost, $port)) {
+ $bound = true;
+ break;
+ }
+ }
+ if (!$bound) {
+ die('Unable to bind to '.$localhost);
}
if (!socket_listen($server, 2)) {
die('Unable to listen on socket');
die('Unable to accept connection');
}
- if (!socket_getpeername($client, $address, $port)) {
+ if (!socket_getpeername($client, $address, $peerport)) {
die('Unable to retrieve peer name');
}
- var_dump($address, $port);
+ var_dump($address, $port === $peerport);
socket_close($client);
socket_close($socket);
?>
--EXPECT--
string(9) "127.0.0.1"
-int(31337)
+bool(true)
/* Bind and connect sockets to localhost */
$localhost = '::1';
- /* Hold the port associated to address */
- $port = 31337;
-
/* Setup socket server */
$server = socket_create(AF_INET6, SOCK_STREAM, getprotobyname('tcp'));
if (!$server) {
die('Unable to create AF_INET6 socket [server]');
}
-
- if (!socket_bind($server, $localhost, $port)) {
- die('Unable to bind to '.$localhost.':'.$port);
+
+ $minport = 31337;
+ $maxport = 31356;
+ $bound = false;
+ for($port = $minport; $port <= $maxport; ++$port) {
+ if (socket_bind($server, $localhost, $port)) {
+ $bound = true;
+ break;
+ }
+ }
+ if (!$bound) {
+ die('Unable to bind to '.$localhost);
}
if (!socket_listen($server, 2)) {
die('Unable to listen on socket');
die('Unable to accept connection');
}
- if (!socket_getpeername($client, $address, $port)) {
+ if (!socket_getpeername($client, $address, $peerport)) {
die('Unable to retrieve peer name');
}
- var_dump($address, $port);
+ var_dump($address, $port === $peerport);
socket_close($client);
socket_close($socket);
?>
--EXPECT--
string(3) "::1"
-int(31337)
+bool(true)