<?php $port = 0; $server = null; while (!$server) { $port = rand(50000, 65535); $server = @stream_socket_server("tcp://127.0.0.1:{$port}", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN); } $pid = pcntl_fork(); if ($pid) { test_server(); pcntl_wait($status); } else { test_client(); } function read_all_data($conn, $bytes) { $all_data = ''; $data = ''; // Loop until we read all the bytes we expected or we hit an error. stream_set_timeout($conn, 1); while ($bytes > 0 && ($data = fread($conn, $bytes))) { $bytes -= strlen($data); $all_data .= $data; } return $bytes == 0 ? $all_data : false; } function test_server() { global $server; // The server only accepts once, but the client will call
$server = @stream_socket_server("{$scheme}://127.0.0.1:{$port}", $errno, $errstr, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $contexts[$i]); } $servers[] = $server; $ports[] = $port; } foreach ($schemes as $i => $scheme) { $server = $servers[$i]; $port = $ports[$i]; $pid = pcntl_fork(); if ($pid) { echo "Testing {$scheme}://\n"; test_server($server); pcntl_wait($status); exit; } else { test_client($scheme, $port); } } unlink($pemfile); function read_all_data($conn, $bytes) { $all_data = ''; $data = ''; // Loop until we read all the bytes we expected or we hit an error. stream_set_timeout($conn, 1); while ($bytes > 0 && ($data = fread($conn, $bytes))) { $bytes -= strlen($data); $all_data .= $data; } return $bytes == 0 ? $all_data : false; }