Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

stream_socket_client and server not working

I’m trying to create a connection between the client and the server (with 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$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);

If I use in client and server one port, for example 8000, than i have error "address already in use". How do I check and accept the certificate and read the phrase ("Hello world") on the server side? Also I read that I need to use the command "openssl s_client -connect 127.0.0.1:8001", but I don’t know how to use it in PHP and when exactly should I use. Thanks!

>Solution :

These two lines should be of particular interest to you

$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);

Read this serwer&client

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading