I am practicing socket programming using stream methods in PHP.
I am using PHP 7.3.11.
I found very unexpected error message which says "undefined index " error where
Here is my server side code
<?php
error_reporting(E_ALL);
/* allow the script to hang around waiting for connection.*/
set_time_limit(0);
$address = '127.0.0.1';
$port = 9016;
$server = stream_socket_server($address .":". $port, $errno, $errorMessage);
$req = array( "R004"=>"R004 exist"
, "R001" => "R001 Ruccess"
, "R002"=>"R002Rcontinue"
, "R003"=>"R003 lRquit"
, "R005"=>"R005 hello");
$res = array("TT","SOO1", "S002", "S003");
if ($server === false)
{
throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
}
echo json_encode($req);
while($socket = stream_socket_accept($server)){
$rand = rand(1,3);
$peer = stream_socket_get_name($socket, true);
$pkt = stream_socket_recvfrom($socket, 1500, 0,$peer);
if( !empty($pkt)){
$pkt = trim(preg_replace('/\s+/','',$pkt));
echo "pkt[{$pkt}]";
echo "EXITS:".array_key_exists($pkt, $req)."\n";
echo "pkt[{$pkt}]".PHP_EOL;
echo $req[$pkt].PHP_EOL;
stream_socket_sendto($socket, $res[$rand], 0, $peer);
}
fclose($socket);
usleep(1000);
}
stream_socket_shutdown($server, \STREAM_SHUT_RDWR);
and here is my client side code
error_reporting(E_ALL);
set_time_limit(0);
$address = '127.0.0.1';
$port = 9016;
$local = "tcp://{$address}:{$port}";
$socket = stream_socket_client($local, $errno, $errstr, 30);
$c_res = array("S001" => "Success", "S002" => "continue", "S003" => "quit");
$c_req = array("ROO3", "R005", "R002", "R001", "R004");
while ($socket = stream_socket_client($local, $errno, $errstr, 30)) {
$rand = rand(1, 4);
// echo "SEND[$c_req[$rand]]" . PHP_EOL;
$sent = stream_socket_sendto($socket, $c_req[$rand] . PHP_EOL);
if ($sent > 0) {
usleep(1000);
$s_res = stream_socket_recvfrom($socket, 1500, 0, $peer);
// $s_res = fread($socket, 4096);
$s_res = trim(preg_replace('/\s+/', '', $s_res));
echo "c_res[{$s_res}]";
echo "EXITS:" . array_key_exists($s_res, $c_res) . "\n";
echo "c_res[{$s_res}]" . PHP_EOL;
echo $c_res[$s_res] . PHP_EOL;
}
}
stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
when I ran this code both server and client ( server runs first and thereafter does client)
I found this kind of error message
PHP Notice: Undefined index: SOO1 in E:\xampp\htdocs\mave_gen\www\adm\client.php on line 29
I do not understand this error message at all because all other references prints out OK except S001.
Obviously, there is S001 is defined in the code.
Do I miss something here ?
Thank you in advance.
The line SOO1 should be S001 in $res = array("TT","SOO1", "S002", "S003"); in the server side code. You have entered letter "O" in SOO1 but it should be "0" (zero) according to the rest of your code.
And in the client side code modify the following line of code
if ($sent > 0) {
usleep(1000);
$s_res = stream_socket_recvfrom($socket, 1500, 0, $peer);
// $s_res = fread($socket, 4096);
$s_res = trim(preg_replace('/\s+/', '', $s_res));
echo "c_res[{$s_res}]";
echo "EXITS:" . array_key_exists($s_res, $c_res) . "\n";
echo "c_res[{$s_res}]" . PHP_EOL;
echo $c_res[$s_res] . PHP_EOL;
}
to the following line
if ($sent > 0) {
usleep(1000);
$s_res = stream_socket_recvfrom($socket, 1500, 0, $peer);
if(!empty($s_res))
{
// $s_res = fread($socket, 4096);
$s_res = trim(preg_replace('/\s+/', '', $s_res));
echo "c_res[{$s_res}]";
echo "EXITS:" . array_key_exists($s_res, $c_res) . "\n";
echo "c_res[{$s_res}]" . PHP_EOL;
echo $c_res[$s_res] . PHP_EOL;
}
}
After these changes it should work.
Related
I have used WebSocket in PHP. but somehow it will close after some time(so many requests).
Is it anyway to handle it?
We got following error when we try to run it after stop:
socket_bind(): unable to bind address [10013]: An attempt was made to
access a socket in a way forbidden by its access permissions.
in C:\wamp64\www\Websocket\server.php on line 52
Warning: socket_listen(): unable to listen on socket [10022]: An invalid argument was supplied.
in C:\wamp64\www\Websocket\server.php on line 55
Here is my code:
<?php
set_time_limit(0);
error_reporting(E_ALL);
//include 'Triangulate.php';
$filename = 'logs.txt';
class Values
{
public $distance;
public $rasp_id;
public $rssi;
}
$host = '10.37.54.167'; //host
$port = '9000'; //port
$null = null; //null var
$rasp = array(
47 => array(17, 0),
124 => array(1,6.4),
43 => array(1,0),
44 => array(4.6,2));
//Create TCP/IP sream socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//reuseable port
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
//bind socket to specified host
socket_bind($socket, 0, $port);
//listen to port
socket_listen($socket);
//create & add listning socket to the list
$clients = array($socket);
$distance = array();
$totalbicken = array(array());
//start endless loop, so that our script doesn't stop
while (true) {
//manage multipal connections
$changed = $clients;
//returns the socket resources in $changed array
socket_select($changed, $null, $null, 0, 10);
$fp = fopen($filename, 'a+');
//check for new socket
if (in_array($socket, $changed)) {
$socket_new = socket_accept($socket); //accpet new socket
$clients[] = $socket_new; //add socket to client array
$header = socket_read($socket_new, 1024); //read data sent by the socket
perform_handshaking($header, $socket_new, $host, $port); //perform websocket handshake
socket_getpeername($socket_new, $ip); //get ip address of connected socket
$response = mask(json_encode(array('type' => 'system', 'message' => $ip . ' connected'))); //prepare json data
fwrite ($fp,$response . "\n");
// send_message($response); //notify all users about new connection
// send_message_socket($response, $socket_new);
//make room for new socket
$found_socket = array_search($socket, $changed);
unset($changed[$found_socket]);
}
//loop through all connected sockets
foreach ($changed as $changed_socket) {
//check for any incomming data
try {
while (socket_recv($changed_socket, $buf, 1024, 0) >= 1) {
$received_text = unmask($buf); //Unmask Recive data.
fwrite ($fp,"Text receive:" . $received_text . "\n");
$tst_msg = json_decode($received_text); //json decode.
fwrite ($fp,"Json Decord!". "\n");
if (!isset($tst_msg->action)) {
} else {
$user_action = $tst_msg->action; //sender name.
if ($user_action == "distance") { //If action is distance.
$rasp_id = $tst_msg->rasp_id; //get raspberry uniq id.
$beacon_id = $tst_msg->beacon_id; //get beacon id
$distance_temp = $tst_msg->distance; // get distance
$rssi = $tst_msg->rssi; //get RSSI.
fwrite ($fp,isset($distance[$beacon_id]). "\n");
fwrite ($fp,"Get all values!". "\n");
// echo mask(json_encode(array('type'=>'message', 'action'=>'distance', 'beacon_id'=>$beacon_id, 'rasp_id'=>$rasp_id, 'distance'=>$distance)));
if (isset($distance[$beacon_id])) {
$totalbicken[$beacon_id] = count($distance[$beacon_id]); //get total distance of sender beacon
} else {
$totalbicken[$beacon_id] = 0;
}
if (isset($rasp[$rasp_id])) {
echo "\nRequest from: " . $rasp_id;
$myVal = new Values();
$myVal->distance = $distance_temp;
$myVal->rasp_id = $rasp_id;
$myVal->rssi = $rssi;
$flag = false;
if (isset($distance[$beacon_id])) {
if (count($distance[$beacon_id]) > 0) {
for ($i = 0; $i < count($distance[$beacon_id]); $i++) {
$myTempVal = $distance[$beacon_id][$i];
if (($myTempVal->rasp_id) == $rasp_id) {
$flag = true;
$distance[$beacon_id][$i] = $myVal;
break;
}
}
if (!$flag) {
$distance[$beacon_id][$totalbicken[$beacon_id]] = $myVal;
}
} else {
$distance[$beacon_id][$totalbicken[$beacon_id]] = $myVal;
}
} else {
$distance[$beacon_id][$totalbicken[$beacon_id]] = $myVal;
}
if (count($distance[$beacon_id]) > 2) {
/*$data = triangulate($rasp[$distance[$beacon_id][0]->rasp_id], $distance[$beacon_id][0]->distance,
$rasp[$distance[$beacon_id][1]->rasp_id], $distance[$beacon_id][1]->distance,
$rasp[$distance[$beacon_id][2]->rasp_id], $distance[$beacon_id][2]->distance);*/
fwrite ($fp,"Before Triangulation!". "\n");
$data = getTrilateration($rasp[$distance[$beacon_id][0]->rasp_id], $distance[$beacon_id][0]->distance,
$rasp[$distance[$beacon_id][1]->rasp_id], $distance[$beacon_id][1]->distance,
$rasp[$distance[$beacon_id][2]->rasp_id], $distance[$beacon_id][2]->distance);
fwrite ($fp,"End Triangulation!". "\n");
$response_text = mask(json_encode(array('type' => 'message', 'action' => 'distance',
'beacon_id' => $beacon_id, 'distance' => $distance[$beacon_id], 'coordinates' => $data)));
fwrite ($fp,"Response: ". $response_text . "\n");
send_message($response_text);
}
}
}
fwrite ($fp,"Break!: ". "\n");
break 2; //exist this loop*/
}
}
} catch (Exception $e) {
fwrite ($fp,"Exception: done! \n");
fwrite ($fp,"Exception: ". $e->getMessage(). "\n");
echo "Exception:" . $e->getMessage();
}
$buf = #socket_read($changed_socket, 1024, PHP_NORMAL_READ);
if ($buf === false) { // check disconnected client
// remove client for $clients array
try {
$found_socket = array_search($changed_socket, $clients);
socket_getpeername($changed_socket, $ip);
unset($clients[$found_socket]);
//notify all users about disconnected connection
$response = mask(json_encode(array('type' => 'system', 'message' => $ip . ' disconnected')));
send_message($response);
} catch (Exception $e) {
echo "Exception:" . $e;
}
}
}
}
// close the listening socket
socket_close($socket);
$fclose ($fp);
function send_message($msg)
{
global $clients;
foreach ($clients as $changed_socket) {
#socket_write($changed_socket, $msg, strlen($msg));
}
return true;
}
function send_message_socket($msg, $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];
}
}
$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/Websocket/shout.php\r\n" .
"Sec-WebSocket-Accept:$secAccept\r\n\r\n";
socket_write($client_conn, $upgrade, strlen($upgrade));
}
Thanks in advance!
I have setup Linux environment and test. It is working fine. It might a firewall issue in windows.
As per my knowledge, the newer version of windows (after service pack-3) has some of the RPC rules for connection (Limit of HTTP requests and connection in second) so, that might be an issue.
Please read this.
As of now, change the operating system is the one of a solution.
Thanks & Regards
Jignesh M. Mehta
I got this error:
Warning: socket_select(): supplied argument is not a valid Socket resource in /volume1/web/is/xxxx/listen-new.php on line 12
PHP Warning: socket_select(): supplied argument is not a valid Socket resource in /volume1/web/is/xxxx/listen-new.php on line 12
this my snippet code
My code is:
$port = $this->port;
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($sock, 0, $port);
socket_listen($sock);
$this->clients[] = $sock;
$data = array();
while (true) {
$read = $this->clients;
$write = array(); //NULL
$except = array();//NULL
$sckt = socket_select($read, $write, $except, 0);
if($sckt === false){
echo "socket_select() failed, reason: " .
socket_strerror(socket_last_error()) . "\n";
}
elseif($sckt > 0) {
if (in_array($sock, $read)) {
$this->clients[] = $newsock = socket_accept($sock);
//var_dump($this->gps);
socket_write($newsock, "Connected\n");
//var_dump($this->gps);
/**try{
socket_getpeername($newsock, $ip); //error
echo "New client connected: {$ip}\n";
}
catch(Exception $e){
echo "error : $e->getMessage()\n";
}**/
$key = array_search($sock, $read);
unset($read[$key]);
}
foreach ($read as $read_sock) {
$data = #socket_read($read_sock, 2048);
if ($data === false) {
$gpsdisc = array_search($read_sock, array_column($this->gps, 'pid'));
$key = array_search($read_sock, $this->clients);
unset($this->clients[$key]);
unset($this->gps[$gpsdisc]);
echo "client disconnected.\n";
continue;
}
else{
$data = trim($data);
if (!empty($data)) {
var_dump($data);
$buf = bin2hex($data);
$start = substr($buf, 0,4);
if($start=="7878"){
$protocol = substr($buf, 6,2);
if($protocol=="01"){
$imei = substr($buf, 8,16);
$cariGPS = $this->cariGPS($imei);
if($cariGPS!=NULL){
$this->setGPS($imei,$read_sock,$cariGPS);
$reply = $this->authLogin($data);
$rep = hex2bin("$reply");
socket_write($read_sock, $rep, strlen($rep));
}
else
echo "$imei salah";
}
elseif($protocol=="15"){
//echo "$buf\n";
$this->reply($data);
}
elseif($protocol=="12"){
$hex12 = bin2hex($data);
//echo "$hex12\n";
}
else{
$hex12 = bin2hex($data);
echo "$hex12\n";
}
echo "$protocol\n";
}
else{
if($data=="where"){
//echo $data."\n";
$this->where($this->gps);
}
elseif($data=="quit"){
$gpsdisc = array_search($read_sock, array_column($this->gps, 'pid'));
unset($this->gps[$gpsdisc]);
socket_close($read_sock);
$key = array_search($read_sock, $this->clients);
unset($this->clients[$key]);
}
else{
echo "command not found\n";
}
}
}
}
}
}
}
socket_close($sock);
PS I also had to change $write = null and $except = null
Are there any solution for this?
It looks like you supplied only the server code but omitted the client code. Can you post the client code please? I guess the problem is there. With my test client (fyi I used socket_create) I cannot reproduce the problem you are reporting.
Also mine is php7 (if you are curious).
This may have some hints for you: Socket_read() says "not a valid resource"
$socket = socket_create(AF_INET, SOCK_DGRAM, 0);
socket_set_option($socket,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>2, "usec"=>0));
$result = socket_connect($socket, $this->ip, $this->port);
if($result <= 0) {
echo "connect() failed.\nReason: ($result) \n";
}
$data = "\xFF\xFF\xFF\xFF\x54\x53\x6F\x75\x72\x63\x65\x20\x45\x6E\x67\x69\x6E\x65\x20\x51\x75\x65\x72\x79\x00";
socket_write($socket, $data, strlen($data));
$out = socket_read($socket, 4096);
socket_close($socket);
if($out == null ){
echo 'data null';
}
else {
$queryData = explode("\x00", substr($out, 6), 5);
$server['ip'] = $this->ip;
$server['port'] = $this->port;
$server['name'] = $queryData[0];
$server['map'] = $queryData[1];
$server['game'] = $queryData[2];
$server['description'] = $queryData[3];
$packet = $queryData[4];
//$app_id = array_pop(unpack("S", substr($packet, 0, 2)));
$server['players'] = ord(substr($packet, 2, 1));
$server['playersmax'] = ord(substr($packet, 3, 1));
$server['bots'] = ord(substr($packet, 4, 1));
$server['dedicated'] = substr($packet, 5, 1);
$server['os'] = substr($packet, 6, 1);
$server['password'] = ord(substr($packet, 7, 1));
$server['vac'] = ord(substr($packet, 8, 1));
$this->info = $server;
echo "<pre>";
print_r($queryData);
echo "</pre>";
}
//error_reporting($oldErrorReporting);
This code work on my old server (centos + php5 + apache2);
and after transfer to another server (ubuntu + php5 + apache2), it stopped working.
When The problem is not to set up php (it is on the socket support), but the fact that he simply does not receive a request, although no connection error, he betrays.
Among the possible reasons for thinking that the problem of accessing the service socket with external ip, but I did not know how to fix it.
Thanks in advance for your answers!
I am creating a socket script that will listen to our 3 devices.and the device is setup in one server ip and one port only.
$file = fopen('txt.log','a+');
$server = stream_socket_server('tcp://'.$ipserver.':'.$port, $errno, $errorMessage);
if(!$server) {
echo "$errorMessage ($errno)<br />\n";
}
else{
while($client = #stream_socket_accept($server,$timeout)) {
stream_copy_to_stream($client, $file);
fclose($file);
fclose($client);
}
}
but the problem is that if one device is connected,the two devices cannot connect anymore.I appreciate some one can help me how to get this work.or give me some idea
Thank you in advance.
$file = fopen('txt.log', 'a+');
$server = stream_socket_server("tcp://$ipserver:$port", $errno, $errorMessage);
if (!$server)
echo "$errorMessage ($errno)<br />\n";
else
{
$s = array($server);
$t = $timeout == -1 ? NULL : $timeout;
while ($r = $s and stream_select($r, $n=NULL, $n=NULL, $t))
foreach ($r as $stream)
if ($stream == $server) // new client
$s[] = stream_socket_accept($server, -1);
else
if (!fputs($file, fgets($stream)))
{
fclose($stream);
array_splice($s, array_search($stream, $s), 1);
}
}
i was wondering if there is any way to turn my website into a proxy server ..
i found plenty of scripts using PHP but they all require navigating to site in order to use
the proxy, but what i really want is a script that enables me to access the site via browser configuration like in firefox when you enter the IP and port number in the options dialog, is there any kind of scripts that does that ?
any links may help me get quick on the subject are welcomed ..
Thank you,
AB
http://sourceforge.net/projects/poxy/
http://www.phpmyproxy.com/
(and more, ask google)
Is this the kind of proxy software you're looking for? Just plain HTTP proxies in PHP.
Redirect your adapter to this computer's ip and port, its synchronous though so it'll be slow.
$addr = gethostbyname('0.0.0.0'); //ip sensitive :((
$server = stream_socket_server("tcp://" . $addr . ":8000", $errno, $errorMessage);
echo "connected to: $addr:8000";
if ($server === false) {
throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
}
$conns = array( $server ); // connections
$connection = 0;
// loop forever
for (;;) {
$reads = $conns;
// get number of connections with new data
$mod = stream_select($reads, $write, $except, 5);
if ($mod===false) break;
// I have no idea what this does but what im doing here is separating the client ip and port from server 1:1 only!
foreach ($reads as $read) {
if ($read===$server) {
// if a client is connected
if ($client = #stream_socket_accept( $server )) {
echo "\nconnection from " . stream_socket_get_name( $client, true ) . "\n";
$recv = fread($client, 1024);
$rec_arr = explode( ' ', $recv );
echo hex_dump($recv);
if(strpos($recv, "CONNECT ")!==0) {
if( $src = #fopen( $rec_arr[ 1 ], 'rb') ) {
while ($chunk = fread($src, 1024000)) {
#fwrite( $client, $chunk );
}
$chunk = "";
fclose( $src );
}
}
stream_socket_shutdown($client, STREAM_SHUT_RDWR);
}
}
}
}
function hex_dump($data, $newline="\n")
{
$from = '';
$to = '';
$width = 16; # number of bytes per line
$pad = '.'; # padding for non-visible characters
if ($from==='')
{
for ($i=0; $i<=0xFF; $i++)
{
$from .= chr($i);
$to .= ($i >= 0x20 && $i <= 0x7E) ? chr($i) : $pad;
}
}
$hex = str_split(bin2hex($data), $width*2);
$chars = str_split(strtr($data, $from, $to), $width);
$offset = 0;
foreach ($hex as $i => $line)
{
$line = strtoupper( $line );
echo sprintf('%6X',$offset).' : '.implode(' ', str_split($line,2)) . ' [' . $chars[$i] . ']' . $newline;
$offset += $width;
}
}