stream_socket_client 并且服务器不工作
stream_socket_client and server not working
我正在尝试在客户端和服务器之间创建连接(使用 TLS 1.2)。
Client.php
$stream_context = stream_context_create(['ssl' => [
'local_cert' => "path/to/cer.pem",
'verify_peer' => true,
'verify_peer_name' => false,
'passphrase' => "password to cert",
'verify_depth' => 0
]]);
$socket = stream_socket_client("tlsv1.2://127.0.0.1:8000", $errno, $errstr, 3, STREAM_CLIENT_CONNECT, $stream_context);
if ($socket === false) {
return false;
}
$req = "POST /Serwer.php HTTP/1.1\r\n" .
"Content-Type: text/xml;charset=UTF-8\r\n" .
"Host: 127.0.0.1\r\n" .
"Connection: Close\r\n" .
"Hello world!\r\n";
$start = time();
fwrite($socket, $req);
$resp = '';
while (!feof($socket)) {
if (time() - $start > 15) {
break;
}
$f = fgets($socket);
$resp .= $f;
}
fclose($socket);
echo $resp;
Server.php
$stream_context = stream_context_create(['ssl' => [
'local_cert' => "path/to/cert.pem",
'passphrase' => "password to cert",
'allow_self_signed' => true,
'verify_peer' => false
]]);
$server = stream_socket_server("tlsv1.2://127.0.0.1:8001",$errno, $error, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $stream_context);
if ($server === false) {
return false;
}
$connects = array();
while (true) {
$read = $connects;
$read []= $server;
$write = $except = null;
$mod_fd = stream_select($read, $write, $except, 3); // return always 0, I don't know why
if ($mod_fd === false) {
break;
}
if (in_array($server, $read)) {
$connect = stream_socket_accept($server, -1);
$connects[] = $connect;
unset($read[ array_search($server, $read) ]);
}
foreach($read as $connect) {
$headers = '';
while ($buffer = rtrim(fgets($connect))) {
$headers .= $buffer;
}
fwrite($connect, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\nHello!");
fclose($connect);
unset($connects[ array_search($connect, $connects) ]);
}
fclose($server);
如果我在客户端和服务器中使用同一个端口,例如 8000,则会出现错误“地址已在使用中”。如何检查并接受证书并在服务器端读取短语(“Hello world”)?我还读到我需要使用命令“openssl s_client -connect 127.0.0.1:8001”,但我不知道如何在 PHP 中使用它以及我应该在什么时候使用它。谢谢!
这两行你应该特别感兴趣
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
$fp = stream_socket_client("tcp://127.0.0.1:8000", $errno, $errstr,
30);
我正在尝试在客户端和服务器之间创建连接(使用 TLS 1.2)。
Client.php
$stream_context = stream_context_create(['ssl' => [
'local_cert' => "path/to/cer.pem",
'verify_peer' => true,
'verify_peer_name' => false,
'passphrase' => "password to cert",
'verify_depth' => 0
]]);
$socket = stream_socket_client("tlsv1.2://127.0.0.1:8000", $errno, $errstr, 3, STREAM_CLIENT_CONNECT, $stream_context);
if ($socket === false) {
return false;
}
$req = "POST /Serwer.php HTTP/1.1\r\n" .
"Content-Type: text/xml;charset=UTF-8\r\n" .
"Host: 127.0.0.1\r\n" .
"Connection: Close\r\n" .
"Hello world!\r\n";
$start = time();
fwrite($socket, $req);
$resp = '';
while (!feof($socket)) {
if (time() - $start > 15) {
break;
}
$f = fgets($socket);
$resp .= $f;
}
fclose($socket);
echo $resp;
Server.php
$stream_context = stream_context_create(['ssl' => [
'local_cert' => "path/to/cert.pem",
'passphrase' => "password to cert",
'allow_self_signed' => true,
'verify_peer' => false
]]);
$server = stream_socket_server("tlsv1.2://127.0.0.1:8001",$errno, $error, STREAM_SERVER_BIND|STREAM_SERVER_LISTEN, $stream_context);
if ($server === false) {
return false;
}
$connects = array();
while (true) {
$read = $connects;
$read []= $server;
$write = $except = null;
$mod_fd = stream_select($read, $write, $except, 3); // return always 0, I don't know why
if ($mod_fd === false) {
break;
}
if (in_array($server, $read)) {
$connect = stream_socket_accept($server, -1);
$connects[] = $connect;
unset($read[ array_search($server, $read) ]);
}
foreach($read as $connect) {
$headers = '';
while ($buffer = rtrim(fgets($connect))) {
$headers .= $buffer;
}
fwrite($connect, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\nHello!");
fclose($connect);
unset($connects[ array_search($connect, $connects) ]);
}
fclose($server);
如果我在客户端和服务器中使用同一个端口,例如 8000,则会出现错误“地址已在使用中”。如何检查并接受证书并在服务器端读取短语(“Hello world”)?我还读到我需要使用命令“openssl s_client -connect 127.0.0.1:8001”,但我不知道如何在 PHP 中使用它以及我应该在什么时候使用它。谢谢!
这两行你应该特别感兴趣
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
$fp = stream_socket_client("tcp://127.0.0.1:8000", $errno, $errstr, 30);