OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 1

  • Thread starter Thread starter Davide Cavallini
  • Start date Start date
D

Davide Cavallini

Guest
In Chrome 50.0.2661.94 m

Code:
var a = new WebSocket('ws://192.168.0.153:9000/socket_listener_1_WINDOWS.php');

ERROR (from Yesterday afternoon- It works on the other clients pc's, but also "ws://echo.websocket.org/" works using my pc as client)

Code:
WebSocket connection to 'ws://192.168.0.153:9000/socket_listener_1_WINDOWS.php' failed: One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 1

(what and where are "reserved1, 2 and 3"???)

This is the PHP script of the server (launched with XAMPP 3.2.2):

Code:
<?php

//error_reporting(E_ALL);

set_time_limit(0);

$IP = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : gethostbyname(gethostname());

echo $IP . '\r\n';

$host = $IP; //$_SERVER['SERVER_ADDR']; //host
$port = 9000; //port
$null = NULL;
$read = NULL;
$except = NULL;

function send_message($msg) {
    global $clients;
    foreach ($clients as $changed_socket) {
        @socket_write($changed_socket, $msg, strlen($msg));
    }
    return true;
}

//Unmask incoming framed message
function unmask($text) {
    $length = ord($text[1]) & 127;
    if ($length == 126) {
        $masks = substr($text, 4, 4);
        $data = substr($text, 8);
    } elseif ($length == 127) {
        $masks = substr($text, 10, 4);
        $data = substr($text, 14);
    } else {
        $masks = substr($text, 2, 4);
        $data = substr($text, 6);
    }
    $text = "";
    for ($i = 0; $i < strlen($data); ++$i) {
        $text .= $data[$i] ^ $masks[$i % 4];
    }
    return $text;
}

//Encode message for transfer to client.
function mask($text) {
    $b1 = 0x80 | (0x1 & 0x0f);
    $length = strlen($text);

    if ($length <= 125)
        $header = pack('CC', $b1, $length);
    elseif ($length > 125 && $length < 65536)
        $header = pack('CCn', $b1, 126, $length);
    elseif ($length >= 65536)
        $header = pack('CCNN', $b1, 127, $length);
    return $header . $text;
}

//handshake new client.
function perform_handshaking($receved_header, $client_conn, $host, $port) {
    $headers = array();
    $lines = preg_split("/\r\n/", $receved_header);
    foreach ($lines as $line) {
        $line = chop($line);
        if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) {
            $headers[$matches[1]] = $matches[2];
        }
    }

//var_dump($headers);

    $secKey = $headers['Sec-WebSocket-Key'];

    $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
//hand shaking header
    $upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
            "Upgrade: websocket\r\n" .
            "Connection: Upgrade\r\n" .
            "WebSocket-Origin: $host\r\n" .
            "WebSocket-Location: ws://$host:$port/socket_listener_1.php\r\n" .
            "Sec-WebSocket-Accept:$secAccept\r\n\r\n";
    socket_write($client_conn, $upgrade, strlen($upgrade));
}

$ultimotimestamp = time();
//----------------------------------------------------------------------------------
//Così dovrebbe ripartire il socket in caso di crash
//Crea collegamento TCP/IP
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

//Se non ci si può connettere all'indirizzo del socket, quindi il server è chiuso
//riparte tutto se il server non si connette
/* -E- */ while (@socket_connect($socket, $host, $port) === FALSE) {

//Parte un nuovo server
    echo "SOCKET SERVER STARTED\n\n";

//Porta riutilizabile (DEFAULT SI)
    socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
//Crea collegamento TCP/IP
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

//Lega il socket ad un indirizzo specifico
    if (socket_bind($socket, $host, $port) !== FALSE) {

//mette il socket server in ascolto
        socket_listen($socket);

//crea, e aggiunge il socket server alla lista
        $clients = array($socket);

//comincia un loop che non si ferma mai
        while (true) {


//la variabile changed prende la lista dei clienti (nel primo caso solo il server)
            $changed = $clients;

//La funzione socket_select() accetta un array di socket e si mette in attesa di una variazione di stato su questi. 
//Questa, derivando come background dai socket BSD, riconoscerà che questi array di risorse socket sono in realtà 
//dei set di descrittori di file. Saranno monitorati 3 set di socket.
            socket_select($changed, $read, $except, 10, 0);

//se in socket ci sono nuovi clients
            if (in_array($socket, $changed)) {

                $socket_new = socket_accept($socket); //accetta il nuovo socket (di default)

                $header = socket_read($socket_new, 2048); //legge l'header del nuovo client, al quale inviare i dati

                array_push($clients, $socket_new); //aggiunge alla lista dei clients il nuovo collegamento


                if (!empty($header)) { //Se l'header non è vuoto
                    perform_handshaking($header, $socket_new, $host, $port); //fa un nuovo handshake, ossia una sorta di codifica dei dati

                    socket_getpeername($socket_new, $ip); //prende l'ip dal socket connesso

                    $response = mask(json_encode(array('type' => 'system', 'message' => $ip . ' connected'))); //prepare json data
                    send_message($response); //notify all users about new connection

                    echo $ip . " connesso\n\n";
                }


                $found_socket = array_search($socket, $changed); //cerca array socket dentro changed

                unset($changed[$found_socket]); //elimina l'indice trovato dall'array
            }


            if (time() - $ultimotimestamp >= 5) {
                $response = mask(json_encode(array('type' => 'bip', 'message' => "1")));
                send_message($response); //notify all users about new connection

                $ultimotimestamp = time();
            }

//ciclo dei socket connessi
            foreach ($changed as $changed_socket) {

//scrive i dati del socket in un buffer di 2048 bytes
                /* -E- */ //while (socket_recv($changed_socket, $buf, 2048, 0) > 0) {
                while ($bytes = socket_recv($changed_socket, $buf, 2048, 0) > 1) {
//while (!false === ($buf = socket_read($changed_socket, 2048, PHP_BINARY_READ))) {
                    //var_dump($bytes);
//echo '\n\n';

                    $received_text = unmask($buf); //smaschera dati buffer
                    $tst_msg = json_decode($received_text); //decodifica in json 

                    $response_text = mask(json_encode($tst_msg)); //lo rimaschera
                    send_message($response_text); //invia il dato a $changed_socket, che è una risorsa socket

                    echo $response_text . "\n\n"; //Mostra il dato inviato a schermo

                    break 2; //esce fino al ciclo while (di 2 livelli sopra)
                }

//il buffer legge il socket di prima, fino a 2048 bytes
                /* -E- */ //if (socket_recv($changed_socket, $buf, 2048, 0) === FALSE) { // se il $buf è false, quindi si presuppone che il client sia disconnesso

                if ($bytes = socket_recv($changed_socket, $buf, 2048, 0) === FALSE) {
                    var_dump($bytes);
                    $found_socket = array_search($changed_socket, $clients);
                    socket_getpeername($changed_socket, $ip);
                    unset($clients[$found_socket]); // rimuove il client dall'array dei clients
                }
            }
            //sleep(0.05);
        }
        socket_close($socket); //in caso di errore del ciclo si chiude il socket in ascolto
    }
    socket_close($socket);
}
socket_close($socket);

SERVER SCREEN: socket listener

CLIENT SCREEN opcode

response header

This is the Wireshark TCP trace from my client. Ask me if you need new screenshots.

tcp trace

I have no Firewalls and Antivirus activated. Do you know why it doesn't work on my pc? It worked for 1 year first.

Thank you

<p>In Chrome 50.0.2661.94 m</p>

<pre><code>var a = new WebSocket('ws://192.168.0.153:9000/socket_listener_1_WINDOWS.php');
</code></pre>

<p>ERROR (from Yesterday afternoon- It works on the other clients pc's, but also "ws://echo.websocket.org/" works using my pc as client)</p>

<pre><code>WebSocket connection to 'ws://192.168.0.153:9000/socket_listener_1_WINDOWS.php' failed: One or more reserved bits are on: reserved1 = 0, reserved2 = 1, reserved3 = 1
</code></pre>

<p>(what and where are "reserved1, 2 and 3"???)</p>

<p>This is the PHP script of the server (launched with XAMPP 3.2.2):</p>

<pre><code><?php

//error_reporting(E_ALL);

set_time_limit(0);

$IP = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : gethostbyname(gethostname());

echo $IP . '\r\n';

$host = $IP; //$_SERVER['SERVER_ADDR']; //host
$port = 9000; //port
$null = NULL;
$read = NULL;
$except = NULL;

function send_message($msg) {
global $clients;
foreach ($clients as $changed_socket) {
@socket_write($changed_socket, $msg, strlen($msg));
}
return true;
}

//Unmask incoming framed message
function unmask($text) {
$length = ord($text[1]) & 127;
if ($length == 126) {
$masks = substr($text, 4, 4);
$data = substr($text, 8);
} elseif ($length == 127) {
$masks = substr($text, 10, 4);
$data = substr($text, 14);
} else {
$masks = substr($text, 2, 4);
$data = substr($text, 6);
}
$text = "";
for ($i = 0; $i < strlen($data); ++$i) {
$text .= $data[$i] ^ $masks[$i % 4];
}
return $text;
}

//Encode message for transfer to client.
function mask($text) {
$b1 = 0x80 | (0x1 & 0x0f);
$length = strlen($text);

if ($length <= 125)
$header = pack('CC', $b1, $length);
elseif ($length > 125 && $length < 65536)
$header = pack('CCn', $b1, 126, $length);
elseif ($length >= 65536)
$header = pack('CCNN', $b1, 127, $length);
return $header . $text;
}

//handshake new client.
function perform_handshaking($receved_header, $client_conn, $host, $port) {
$headers = array();
$lines = preg_split("/\r\n/", $receved_header);
foreach ($lines as $line) {
$line = chop($line);
if (preg_match('/\A(\S+): (.*)\z/', $line, $matches)) {
$headers[$matches[1]] = $matches[2];
}
}

//var_dump($headers);

$secKey = $headers['Sec-WebSocket-Key'];

$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
//hand shaking header
$upgrade = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
"Upgrade: websocket\r\n" .
"Connection: Upgrade\r\n" .
"WebSocket-Origin: $host\r\n" .
"WebSocket-Location: ws://$host:$port/socket_listener_1.php\r\n" .
"Sec-WebSocket-Accept:$secAccept\r\n\r\n";
socket_write($client_conn, $upgrade, strlen($upgrade));
}

$ultimotimestamp = time();
//----------------------------------------------------------------------------------
//Così dovrebbe ripartire il socket in caso di crash
//Crea collegamento TCP/IP
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

//Se non ci si può connettere all'indirizzo del socket, quindi il server è chiuso
//riparte tutto se il server non si connette
/* -E- */ while (@socket_connect($socket, $host, $port) === FALSE) {

//Parte un nuovo server
echo "SOCKET SERVER STARTED\n\n";

//Porta riutilizabile (DEFAULT SI)
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
//Crea collegamento TCP/IP
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

//Lega il socket ad un indirizzo specifico
if (socket_bind($socket, $host, $port) !== FALSE) {

//mette il socket server in ascolto
socket_listen($socket);

//crea, e aggiunge il socket server alla lista
$clients = array($socket);

//comincia un loop che non si ferma mai
while (true) {


//la variabile changed prende la lista dei clienti (nel primo caso solo il server)
$changed = $clients;

//La funzione socket_select() accetta un array di socket e si mette in attesa di una variazione di stato su questi.
//Questa, derivando come background dai socket BSD, riconoscerà che questi array di risorse socket sono in realtà
//dei set di descrittori di file. Saranno monitorati 3 set di socket.
socket_select($changed, $read, $except, 10, 0);

//se in socket ci sono nuovi clients
if (in_array($socket, $changed)) {

$socket_new = socket_accept($socket); //accetta il nuovo socket (di default)

$header = socket_read($socket_new, 2048); //legge l'header del nuovo client, al quale inviare i dati

array_push($clients, $socket_new); //aggiunge alla lista dei clients il nuovo collegamento


if (!empty($header)) { //Se l'header non è vuoto
perform_handshaking($header, $socket_new, $host, $port); //fa un nuovo handshake, ossia una sorta di codifica dei dati

socket_getpeername($socket_new, $ip); //prende l'ip dal socket connesso

$response = mask(json_encode(array('type' => 'system', 'message' => $ip . ' connected'))); //prepare json data
send_message($response); //notify all users about new connection

echo $ip . " connesso\n\n";
}


$found_socket = array_search($socket, $changed); //cerca array socket dentro changed

unset($changed[$found_socket]); //elimina l'indice trovato dall'array
}


if (time() - $ultimotimestamp >= 5) {
$response = mask(json_encode(array('type' => 'bip', 'message' => "1")));
send_message($response); //notify all users about new connection

$ultimotimestamp = time();
}

//ciclo dei socket connessi
foreach ($changed as $changed_socket) {

//scrive i dati del socket in un buffer di 2048 bytes
/* -E- */ //while (socket_recv($changed_socket, $buf, 2048, 0) > 0) {
while ($bytes = socket_recv($changed_socket, $buf, 2048, 0) > 1) {
//while (!false === ($buf = socket_read($changed_socket, 2048, PHP_BINARY_READ))) {
//var_dump($bytes);
//echo '\n\n';

$received_text = unmask($buf); //smaschera dati buffer
$tst_msg = json_decode($received_text); //decodifica in json

$response_text = mask(json_encode($tst_msg)); //lo rimaschera
send_message($response_text); //invia il dato a $changed_socket, che è una risorsa socket

echo $response_text . "\n\n"; //Mostra il dato inviato a schermo

break 2; //esce fino al ciclo while (di 2 livelli sopra)
}

//il buffer legge il socket di prima, fino a 2048 bytes
/* -E- */ //if (socket_recv($changed_socket, $buf, 2048, 0) === FALSE) { // se il $buf è false, quindi si presuppone che il client sia disconnesso

if ($bytes = socket_recv($changed_socket, $buf, 2048, 0) === FALSE) {
var_dump($bytes);
$found_socket = array_search($changed_socket, $clients);
socket_getpeername($changed_socket, $ip);
unset($clients[$found_socket]); // rimuove il client dall'array dei clients
}
}
//sleep(0.05);
}
socket_close($socket); //in caso di errore del ciclo si chiude il socket in ascolto
}
socket_close($socket);
}
socket_close($socket);
</code></pre>

<p>SERVER SCREEN:
<a href="https://i.sstatic.net/CsOiU.png" rel="noreferrer"><img src="https://i.sstatic.net/CsOiU.png" alt="socket listener"></a></p>

<p>CLIENT SCREEN
<a href="https://i.sstatic.net/yzrVh.png" rel="noreferrer"><img src="https://i.sstatic.net/yzrVh.png" alt="opcode"></a></p>

<p><a href="https://i.sstatic.net/CMFik.png" rel="noreferrer"><img src="https://i.sstatic.net/CMFik.png" alt="response header"></a></p>

<p>This is the Wireshark TCP trace from my client. Ask me if you need new screenshots.</p>

<p><a href="https://i.sstatic.net/93z80.png" rel="noreferrer"><img src="https://i.sstatic.net/93z80.png" alt="tcp trace"></a></p>

<p>I have no Firewalls and Antivirus activated. Do you know why it doesn't work on my pc? It worked for 1 year first.</p>

<p>Thank you</p>
 

Latest posts

Top