How to get http response in php ratchet - php

I am going to build a system which will receive a streaming raw data form browser. Data will use websocket to transmit and receive, then system will keep going to send chunk data to HTTP server. Flowchart show below:
First, I separate websocket server and HTTP client into 2 program, both can work. websocket server use ratchet, HTTP client use stream_socket_client and stream_select.
Then, I try to combine websocket server and HTTP client together, I can receive data from browser but I cannot get any response from HTTP server. How can I get HTTP response in Ratchet? Many Thanks.
The following is my php code:
Chat.php
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
function read_response_code($response){
foreach (explode("\n", $response) as $a) {
if (strpos($a, 'HTTP/1.1') !== false) {
$arr = explode(" ", $a);
if ($arr[1] != "100")
{
return 0;
}
return 1;
}
}
return 0;
}
class Socket implements MessageComponentInterface {
protected $clients;
public function __construct(){
$this->clients = new \SplObjectStorage;
$this->SERVER = "tcp://192.168.0.1:80";
$this->socket = [];
$this->clients->connect = 0;
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
echo "Try to connect http server...\n";
$this->socket = stream_socket_client($this->SERVER, $errno, $errstr, 30);
if (!$this->socket){
//disconnect websocket
$conn->close();
}
else{
$header = "POST / HTTP/1.1\r\n"
. "Host: 192.168.0.1\r\n"
. "Accept: */*\r\n"
. "Content-Type: application/octet-stream\r\n"
. "Transfer-Encoding: chunked\r\n"
. "Expect: 100-continue\r\n\r\n";
$written = fwrite($this->socket, $header);
$response = fread($this->socket, 100);
$ret = read_response_code($response);
if ($ret == 0)
{
echo "Connect HTTP server fail.\n";
$conn->close();
}
else
{
echo "Connect success!!!!\n";
$this->clients->connect = 1;
}
}
}
public function onMessage(ConnectionInterface $from, $msg) {
//combine http client
if ($this->clients->connect == 1)
{
$read = array($this->socket);
$write = NULL;
$except = NULL;
$num = stream_select($read, $write, $except, 0, 400000);
if ($num === false) {
/* Error handling */
}
elseif ($num > 0) {
// cannot read any response
echo "xxxxxxxxxxxxxx";
}
else {
// send data to HTTP server
$size = strlen($msg);
$hex = dechex($size);
fwrite($this->socket, "$hex\r\n");
$written = fwrite($this->socket, $msg);
fwrite($this->socket, "\r\n");
}
}
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove from connection list
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
Chat-server.php
<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$loop = \React\EventLoop\Factory::create();
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Socket()
)
),
8080
);
$server->run();
HTTP_client.php
<?php
$SERVER = "tcp://192.168.0.1:80";
$SEND_STATUS = 0;
$file = fopen("test.raw", "rb");
$socket = stream_socket_client($SERVER, $errno, $errstr, 30);
$header = "POST / HTTP/1.1\r\n"
. "Host: 192.168.0.1\r\n"
. "Accept: */*\r\n"
. "Content-Type: application/octet-stream\r\n"
. "Transfer-Encoding: chunked\r\n"
. "Expect: 100-continue\r\n\r\n";
$written = fwrite($socket, $header);
$response = fread($socket, 100);
function send_data($file, $socket, $bytes) {
global $SEND_STATUS;
if ( feof($file) ) {
$SEND_STATUS = 1;
fwrite($socket, "0\r\n\r\n");
fclose($file);
return(false);
}
$buffer = fread($file, $bytes);
$size = strlen($buffer);
$hex = dechex($size);
fwrite($socket, "$hex\r\n");
$written = fwrite($socket, $buffer);
fwrite($socket, "\r\n");
}
while (true) {
$read = array($socket);
$write = NULL;
$except = NULL;
$num = stream_select($read, $write, $except, 0, 400000);
if ($num === false) {
/* Error handling */
}
elseif ($num > 0) {
foreach ($read as $r) {
echo "xxxxxxxxx";
}
}
else {
if ( $SEND_STATUS === 0 ) {
send_data($file, $socket, 16000);
}
}
}
?>

Related

issue in web-socket php

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

Socket & Http in Loop break

I'm trying to test the "best" solution to communicate to PHP with NodeJS, but I can't do the test because I make something wrong with the loops.
Here is my NodeJS Code:
var ENCODING = 'utf8';
// SOCKETS
var net = require('net'); // Load the TCP Library
// HTTP REQUEST
var http = require('http'); // Load the HTTP Library | WEBSOCKETS NEED THIS!!
var querystring = require('querystring'); // Load Query Library
// SOCKET SERVER
net.createServer(function (socket) {
socket.setEncoding(ENCODING);
socket.on('data', function (data) {
socket.end('TEXT FROM SOCKET', ENCODING);
});
socket.on('close', function () {
});
}).listen(5000);
// HTTP SERVER
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end("TEXT FROM HTTP");
}).listen(2525);
And here my PHP test:
set_time_limit(0);
$address = '127.0.0.1';
$response = null;
// METHOD: HTTP (2525)
$start = microtime();
for ($x=0; $x<1000; $x++) {
$response = getHttp($address);
}
echo "<br>METHOD 1: " . (microtime() - $start);
// METHOD: Open + Loop Send + Close (5000)
$start = microtime();
$sc = SockOpen($address);
for ($x=0; $x<1000; $x++) {
$response = SockWrite($sc, $address);
}
SockClose($sc);
echo "<br>METHOD 2: " . (microtime() - $start);
// MMETHOD: Loop (Open + Send + Close) (5000)
$start = microtime();
for ($x=0; $x<1000; $x++) {
$sc = SockOpen($address);
$response = SockWrite($sc, $address);
SockClose($sc);
}
echo "<br>METHOD 3: " . (microtime() - $start);
function SockOpen($address) {
ob_implicit_flush();
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
return false;
}
$result = socket_connect($socket, $address, 5000);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
return false;
}
return $socket;
}
function SockWrite($socket, $address) {
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: ".$address."\r\n";
$in .= "Connection: Close\r\n\r\n";
//$in = "key1:value1\n";
socket_write($socket, $in, strlen($in));
$buffer=null;
while ($out = socket_read($socket, 2048)) {
$buffer .= $buffer;
}
return $buffer;
}
function SockClose($socket) {
socket_close($socket); die();
}
function getHttp($address) {
$url = 'http://'.$address.':2525/';
$data = array('key1' => 'value1');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: text/plain\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
One simple connection Work OK, but if i make a loop, the socket "break" the connection and loop fails. The fail occurs in the HTTP and Socket method.
Any idea how to solve this?
thanks
I update the code, searching for the errors, i found problems in server & client codes, here are the updateds:
Client:
set_time_limit(0);
$address = '127.0.0.1';
$response = null;
// METHOD: HTTP (2525)
$start = microtime();
$fails = 0;
for ($x=0; $x<1000; $x++) {
$response = getHttp($address);
if (empty($response)) {
$fails++;
}
}
echo "<br>METHOD 1: " . (microtime() - $start) . " errors: " . $fails;
// METHOD: Open + Loop Send + Close (5000)
$start = microtime();
$fails = 0;
$sc = SockOpen($address);
for ($x=0; $x<1000; $x++) {
$response = SockWrite($sc, $address);
if (empty($response)) {
$fails++;
}
}
SockClose($sc);
echo "<br>METHOD 2: " . (microtime() - $start) . " errors: " . $fails;
// MMETHOD: Loop (Open + Send + Close) (5000)
$start = microtime();
$fails = 0;
for ($x=0; $x<1000; $x++) {
$sc = SockOpen($address);
$response = SockWrite($sc, $address);
if (empty($response)) {
$fails++;
}
SockClose($sc);
}
echo "<br>METHOD 3: " . (microtime() - $start) . " errors: " . $fails;
function SockOpen($address) {
//ob_implicit_flush();
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
return false;
}
$result = socket_connect($socket, $address, 5000);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
return false;
}
return $socket;
}
function SockWrite($socket, $address) {
//$in = "HEAD / HTTP/1.1\r\n";
//$in .= "Host: ".$address."\r\n";
//$in .= "Connection: Close\r\n\r\n";
$in = "key1:value1";
socket_write($socket, $in, strlen($in));
$buffer=null;
//while ($out = socket_read($socket, 1024, MSG_WAITALL)) {
while (socket_recv($socket, $buffer, 2048, MSG_WAITALL) === true) {
$buffer .= $buffer;
}
return $buffer;
}
function SockClose($socket) {
socket_shutdown($socket);
socket_close($socket);
}
function getHttp($address) {
$url = 'http://'.$address.':2525/';
$data = array('key1' => 'value1');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: text/plain\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
try {
$result = #file_get_contents($url, false, $context);
} catch (Exception $e) {
return false;
}
return $result;
}
Server:
var ENCODING = 'utf8';
// SOCKETS
var net = require('net'); // Load the TCP Library
// HTTP REQUEST
var http = require('http'); // Load the HTTP Library | WEBSOCKETS NEED THIS!!
var querystring = require('querystring'); // Load Query Library
// SOCKET SERVER
net.createServer(function (socket) {
socket.setEncoding(ENCODING);
//socket.write("TEXT FROM SOCKET", ENCODING);
socket.on('data', function (data) {
socket.write("TEXT FROM SOCKET\n", ENCODING);
//socket.end();
});
socket.on('close', function () {
});
}).listen(5000);
// HTTP SERVER
http.createServer(function (req, response) {
var body = 'TEXT FROM HTTP';
response.writeHead(200, {
'Content-Length': body.length,
'Content-Type': 'text/plain' });
response.write(body);
response.end();
}).listen(2525);
And the results:
METHOD 1: 0.385564 errors: 26
METHOD 2: 0.062286 errors: 0
METHOD 3: 0.255954 errors: 0
I think the HTTP method errors are because of simult connections by the same client (PHP).
An as spected, the fast is open + loop + close, and the slowest is HTTP
PD: negatives for..?

How can I detect when a stream client is no longer available in PHP (eg network cable pulled out)

Is there any way (other than checking for ping responses) to detect when a client stream (I don't know if a stream would be any different from sockets) becomes unavailable (ie there is no longer any connection but no clean disconnection was made)?
Using this code:
#!/usr/bin/env php
<?php
$socket = stream_socket_server(
'tcp://192.168.1.1:47700',
$errno,
$errstr,
STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,
stream_context_create(
array(),
array()
)
);
if (!$socket) {
echo 'Could not listen on socket'."\n";
}
else {
$clients = array((int)$socket => $socket);
$last = time();
while(true) {
$read = $clients;
$write = null;
$ex = null;
stream_select(
$read,
$write,
$ex,
5
);
foreach ($read as $sock) {
if ($sock === $socket) {
echo 'Incoming on master...'."\n";
$client = stream_socket_accept(
$socket,
5
);
if ($client) {
stream_set_timeout($client, 1);
$clients[(int)$client] = $client;
}
}
else {
echo 'Incoming on client '.((int)$sock)."...\n";
$length = 1400;
$remaining = $length;
$buffer = '';
$metadata['unread_bytes'] = 0;
do {
if (feof($sock)) {
break;
}
$result = fread($sock, $length);
if ($result === false) {
break;
}
$buffer .= $result;
if (feof($sock)) {
break;
}
$continue = false;
if (strlen($result) == $length) {
$continue = true;
}
$metadata = stream_get_meta_data($sock);
if ($metadata && isset($metadata['unread_bytes']) && $metadata['unread_bytes']) {
$continue = true;
$length = $metadata['unread_bytes'];
}
} while ($continue);
if (strlen($buffer) === 0 || $buffer === false) {
echo 'Client disconnected...'."\n";
stream_socket_shutdown($sock, STREAM_SHUT_RDWR);
unset($clients[(int)$sock]);
}
else {
echo 'Received: '.$buffer."\n";
}
echo 'There are '.(count($clients) - 1).' clients'."\n";
}
}
if ($last < (time() - 5)) {
foreach ($clients as $id => $client) {
if ($client !== $socket) {
$text = 'Yippee!';
$ret = fwrite($client, $text);
if ($ret !== strlen($text)) {
echo 'There seemed to be an error sending to the client'."\n";
}
}
}
}
}
}
if ($socket) {
stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
}
and a sockets client on a different computer, I can connect to the server, send and receive data, and disconnect cleanly and everything functions as expected. If, however, I pull the network connection on the client computer, nothing is detected on the server side - the server keeps on listening to the client socket, and also writes to it without any error manifesting itself.
As I understand it, calling feof($stream) will tell you if the remote socket disconnected, but I'm not absolutely certain about that. I'm using ping/pong myself while continuing to research a solution.
You need to set a socket timeout, in which case you get an error if a client does not respond in a timely fashion.
Check PHP's stream_set_timeout function:
http://www.php.net/manual/en/function.stream-set-timeout.php
Also check socket_set_option:
http://php.net/manual/en/function.socket-set-option.php
and finally, check out this great article on how to use sockets in PHP effectively:
"PHP Socket Programming, done the Right Way™"
http://christophh.net/2012/07/24/php-socket-programming/

Why PHP socket_select() cannot detect changes when client send data

I am writing a WebSocket server. While handshaking is successful and server can send encoded data using RFC standard, socket_select() can only detect changes when a new client is connected, not when the client send data to the server. What is happening?
class Server{
private $address;
private $port;
private $master;
private $sockets;
private $stream_sockets;
private $clients;
private $verbose_mode;
function __construct($address = '127.0.0.1', $port = 5001, $verbose_mode = true){
$this->address = $address;
$this->port = $port;
$this->verbose_mode = $verbose_mode;
$this->console("Socket server is starting...");
//socket creation
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1))
{
$this->console("Failed to set socket option: ".socket_strerror(socket_last_error()));
}
if(!socket_set_nonblock($socket))
{
$this->console("Failed to set socket nonblock: ".socket_strerror(socket_last_error()));
}
if(!is_resource($socket))
{
$this->console("Failed to create socket: ".socket_strerror(socket_last_error()));
}
if(!socket_bind($socket, $this->address, $this->port))
{
$this->console("Failed to bind socket: ".socket_strerror(socket_last_error()));
}
if(!socket_listen($socket, 20))
{
$this->console("Failed to listen: ".socket_strerror(socket_last_error()));
}
$this->master = $socket; //add current socket resource as master socket
$this->sockets = array($socket);//add current resource to all sockets
$this->console("Socket server started on {$this->address}:{$this->port}");
}
public function run(){
$this->console("Start running...");
$write = array();
$except = array();
while(isset($this->sockets[0])){
$changed_sockets = $this->sockets;
$result = socket_select($changed_sockets, $write, $except, 1 ,0);
//#stream_select($changed_sockets, $write = null, $except = null, 0, 5000);
if($result > 0)
{
$this->console("number of sockets: ".count($this->sockets));
$this->console("number of changed sockets: ".$result);
foreach($changed_sockets as $socket)
{
if($socket == $this->master)//self changed
{
if(($accepted_socket = socket_accept($this->master))!== false)//incoming connection
{
$this->connect($accepted_socket);//add as a client to the pool with info to be tracked without handshaking at first
}
else
{
$this->console("Socket error: ".socket_strerror(socket_last_error()));
}
}
else //must be others in sockets pool
{
$this->console("Finding socket associated with the client...");
$client = $this->get_client_by_socket($socket); //get client object to track them using socket that they are associated with
if($client){
$this->console("receiving data from the client.");
$bytes = #socket_recv($socket, $data, 2048, MSG_DONTWAIT);
$this->console("byte size received: $bytes");
if(!$client->get_handshake())//check if handshaking has done
{
$this->console("handshaking...");
$this->handshake($client, $data);//handshaking if it is not done previously
}
else if($bytes === 0)
{
$this->disconnect($client);
}
else
{
$this->console("incoming data from client {client->get_id()}");
$this->read($client, $data);//read from client if there are changes in sockets
}
}
}
}
}
}
}
private function slingshot($client, $read){
$send="00:00:00:00".",DM,SAY,0,".$read;
fwrite($client->get_stream_socket(), $send);//output to apollo
//workaround for apollo
if($client->get_initial())
{
$initial = 7;
$continue = 0;
}
else
{
$initial = 8;
$continue = 1;
}
while(TRUE)
{
//input from iris
$num = fgets($client->get_stream_socket(), $initial);//$number of words
if(ltrim($num) > 0)
{
$res = fgets($client->get_stream_socket(), ltrim($num)+1);
if($res!="")
{
fgets($fp,1);
$client->set_initial(false);
$res = $num.$res;
$res = substr($res,6+$continue);
//output to client
$message = rtrim($res);
send($client, $message);
break;
}
}
}
}
private function read($client, $received){
$read = $this->unmask($received);
$this->console("received from client: ".$read);
if($read == "##client exit##") {
$this->console("Killing a child process");
posix_kill($client->get_pid(), SIGTERM);
$this->console("Process {$client->get_pid()} is terminated.");
}
else
{
$this->console("start a child process");
$pid = pcntl_fork();
if($pid == -1)
{
die('could not fork.');
}
else if($pid)
{
$client->set_pid($pid);
}
else
{
//we are the child
$this->slingshot($client, $read);
}
}
}
private function disconnect($client){
$this->console("Disconnecting client #{$client->get_id()}");
$i = array_search($client, $this->clients);//search client in clients pool
$j = array_search($client->get_socket(), $this->sockets);//search client's socket in socket pool
if($j >= 0)
{
array_splice($this->sockets, $j, 1);
socket_close($client->get_socket());
$this->console("Socket closed.");
}
if($i >= 0)
{
array_splice($this->clients, $i, 1);
}
$this->console("Client #{$client->get_id()} disconnected.");
}
private function unmask($payload) {
$length = ord($payload[1]) & 127;
if($length == 126)
{
$masks = substr($payload, 4, 4);
$data = substr($payload, 8);
}
elseif($length == 127)
{
$masks = substr($payload, 10, 4);
$data = substr($payload, 14);
}
else
{
$masks = substr($payload, 2, 4);
$data = substr($payload, 6);
}
$text = '';
for ($i = 0; $i < strlen($data); ++$i){
$text .= $data[$i] ^ $masks[$i%4];
}
return $text;
}
private function encode($text){
// 0x1 text frame (FIN + opcode)
$b1 = 0x80 | (0x1 & 0x0f);
$length = strlen($text);
if($length <= 125)
{
$header = pack('CC', $b1, $length);
}
elseif($length > 125 && $length < 65536)
{
$header = pack('CCS', $b1, 126, $length);
}
elseif($length >= 65536)
{
$header = pack('CCN', $b1, 127, $length);
}
return $header.$text;
}
private function send($client, $text){
$this->console("Client {$client->get_id()}<<".$text);
$text = $this->encode($text);
if(socket_write($client->get_socket(), $text, strlen($text)) === false) {
$this->console("Unable to write to client #{$client->get_id()}'s socket");
$this->disconnect($client);
}
}
private function start_process(){
$this->console("start a child process");
$pid = pcntl_fork();
if($pid == -1)
{
die('could not fork.');
}
else if($pid)
{
$client->set_pid($pid);
}
else
{
//we are the child
$this->send($client, "something to be sent.");
}
}
private function handshake($client, $headers){//data as headers
$this->console("Getting client WebSocket version...");
if(preg_match("/Sec-WebSocket-Version: (.*)\r\n/", $headers, $match))
{
$version = $match[1];
}
else
{
$this->console("The client doesn't support WebSocket.");
}
$this->console("Client WebSocket version is {$version}, (required: 13)");
if($version == 13)
{
$this->console("Getting headers...");
if(preg_match("/GET (.*) HTTP/", $headers, $match))
{
$root = $match[1];
}
if(preg_match("/Host: (.*)\r\n/", $headers, $match))
{
$host = $match[1];
}
if(preg_match("/Origin: (.*)\r\n/", $headers, $match))
{
$origin = $match[1];
}
if(preg_match("/Sec-WebSocket-Key: (.*)\r\n/", $headers, $match))
{
$key = $match[1];
}
$this->console("client Headers are:");
$this->console("\t- Root: ".$root);
$this->console("\t- Host: ".$host);
$this->console("\t- Origin: ".$origin);
$this->console("\t- Sec-WebSocket-Key: ".$key);
$this->console("Generating Sec-WebSocket-Accept key...");
$acceptKey = $key.'258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
$acceptKey = base64_encode(sha1($acceptKey, true));
$upgrade = "HTTP/1.1 101 Switching Protocols\r\n".
"Upgrade: websocket\r\n".
"Connection: Upgrade\r\n".
"Sec-WebSocket-Accept: $acceptKey".
"\r\n\r\n";
$this->console("sending this response to the client #{$client->get_id()}:\r\n".$upgrade);
socket_write($client->get_socket(), $upgrade);
$client->set_handshake(true);
$this->console("Handshake is successfully done!");
return true;
}
else
{
$this->console("WebSocket version 13 is required (the client supports version {$version})");
return false;
}
}
private function get_client_by_socket($socket){
foreach($this->clients as $client)//get all client objects from the pool and check one by one
{
if($client->get_socket() == $socket)//if socket returned from the client matches with parameter
{
$this->console("client found");
return $client;
}
}
return false;//no such client
}
private function connect($socket){
$this->console("creating client...");
$client_id = uniqid();
while(true){
$stream_socket = #stream_socket_client("tcp://127.0.0.1:10000", $errno, $errstr);
if($stream_socket)
{
$this->console("Apollo client created for client #$client_id.");
break;
}
else
{
$this->console("creation failed. Attempting to recreate Apollo client.");
}
}
$client = new Client($client_id, $socket, $stream_socket);
$this->clients[] = $client; //add the socket as client to be tracked
$this->sockets[] = $socket;//add socket as resource to sockets pool
$this->stream_sockets[] = $stream_socket;//add socket as resource to stream sockets pool
$this->console("Client #{$client->get_id()} is successfully created!");
}
private function console($text, $continue = true){
if(!$continue)
{
die($text);
}
if($this->verbose_mode)
{
echo date('[Y-m-d H:i:s] ').$text."\r\n";
}
}
}
That's because you have to accept the new incoming connection using socket_accept(), passing in the socket you've used for socket_listen().
The resulting socket can be used to check for incoming data as expected.
Edit
It seems that you forgot to add $write = $this->sockets; before your call to socket_select().
I figured out that to enable the socket select to detect data from the client, socket server has to send a dummy message to the client first immediately after handshake.

php socket http post not working

I'm trying to write a php script to send a http post request to a url. It doesnt seem to pass through for the server is not receiving it. Can anyone help?
<?php
function postXMLToURL ($server, $path, $xmlDocument) {
$xmlSource = $xmlDocument;
$contentLength = strlen($xmlSource);
//$fp = fsockopen($server, 80);
$fp = fsockopen($server,8080);
fwrite($fp, "POST $path HTTP/1.0\r\n");
fwrite($fp, "Host: $server\r\n");
fwrite($fp, "Content-Type: application/xml\r\n");
fwrite($fp, "Content-Length: $contentLength\r\n");
fwrite($fp, "Connection: close\r\n");
fwrite($fp, "\r\n"); // all headers sent
fwrite($fp, $xmlSource);
$result = '';
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
return $result;
}
function getBody ($httpResponse) {
$lines = preg_split('/(\r\n|\r|\n)/', $httpResponse);
$responseBody = '';
$lineCount = count($lines);
for ($i = 0; $i < $lineCount; $i++) {
if ($lines[$i] == '') {
break;
}
}
for ($j = $i + 1; $j < $lineCount; $j++) {
$responseBody .= $lines[$j] . "\n";
}
return $responseBody;
}
$xmlDocument = new DomDocument($final_xml); //final_xml is my xml in a string
$result = postXMLtoURL("localhost", "/resources", $xmlDocument);
$responseBody = getBody($result);
$resultDocument = new DOMDocument();
$resultDocument->loadXML($responseBody);
header('Content-Type: application/xml');
echo $resultDocument->saveXML();
}
?>
You need to learn how to help yourself.
There's no error detection in the code - you should at least check fp is valid after the call to fsockopen, for preference there should be a LOT more error checking.
Also, get hold of something like wireshark and see what packets your code is generating.
C.
You could aso use stream_context_create();
Once i wrote a php class you are wecome to use it.
<?php
class HTTPRequest
{
protected $url;
protected $method;
protected $headers;
protected $data = "";
protected $useragent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
public function __construct(){}
public function setHeaders($headers)
{
if(is_array($headers))
{
$this->headers = $headers;
return true;
}
return false;
}
public function get($request)
{
if(!$this->headers)
{
throw new Exception("Please set Headers");
}
$this->url = $request;
$this->method = "GET";
return $this->send();
}
public function put($request,$xml)
{
if(!$this->header)
{
throw new Exception("Please set Headers");
}
$this->url = $request;
$this->method = "PUT";
$this->data = $xml;
return $this->send();
}
public function post($request,$xml)
{
if(!$this->headers)
{
throw new Exception("Please set Headers");
}
$this->url = $request;
$this->method = "POST";
$this->data = $xml;
return $this->send();
}
public function delete($request)
{
if(!$this->headers)
{
throw new Exception("Please set Headers");
}
$this->url = $request;
$this->method = "DELETE";
return $this->send();
}
public function setUserAgent($useragent)
{
$this->useragent = $useragent;
}
protected function send()
{
$params = array('http' => array
(
'method' => $this->method,
'content' => $this->data,
'user_agent' => $this->useragent
)
);
$headers = "";
if (!empty($this->headers) && is_array($this->headers))
{
foreach ($this->headers as $header)
{
$headers .= $header."\n";
}
}
$params['http']['header'] = $headers;
$context = stream_context_create($params);
$fp = fopen($this->url, 'r', false, $context);
if (!$fp)
{
throw new Exception("Problem with ".$this->url);
}
$response = stream_get_contents($fp);
if ($response === false)
{
throw new Exception("Problem reading data from ".$this->url);
}
return $response;
}
}
?>

Categories