PHP socket (socket_read) - php

I work on a personal project and i have a problem for managing socket on a multi client server.
So, according to php.net, it's possible to put in argument of socket_read() a socket create by socket_create or accepted by socket_accept...
If i understand, socket create by socket_create is a "local" socket and sockets send by socket_accept are sockets client, it's that ?
in this case, i would like to put my server socket in parameter of socket_read for wait a client among several write on this socket.
more clearly, i accept 4 clients and wait that one of them write on this socket, but i can't put in parameter a specific client socket because i don't know which of them will write...
And when i put the socket create by : socket_create, php return me an error
my native language is not English as you could see, but nobody could answer me...
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() a échoué : raison : " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $this->address, $this->port) === false) {
echo "socket_bind() a échoué : raison : " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() a échoué : raison : " . socket_strerror(socket_last_error($sock)) . "\n";
}
for($i=0;$i<$this->maxClient;$i++){
if(($client = socket_accept($sock)) === false){
echo "socket_accept() a échoué : raison : " . socket_strerror(socket_last_error($sock)) . "\n";
break;
}else echo "<br/>Client $client[$i] has connected\n";
$this->joueurs[$i] = new Joueur($client,$i);
}
while($start){
$messageClient = socket_read($sock , 1024);
......
}
So,I have two problems, the first is, i need that socket_read() doesn't block my script and Scanning my sockets until it find something to read.
The second problem is that as my program does it not stopped, my client don't receive anything and when i stop my serveur my client receive the message...
Have you any idea ?
Thanks

Get familiar with socket_select - it allows for waiting on multiple sockets to become readable or/and writable, or have errors. A listening socket becomes readable when there's new connection pending/ready to be accept()-ed.

i found my problem :
for unlock socket_read()
socket_set_option($socket,SOL_SOCKET, SO_RCVTIMEO, array("sec"=>1, "usec"=>0));
socket_set_nonblock($socket);
And for clear buffer i use "/n" without that i could receive message in my client only when the server stop.

Related

fputs only returns unknown command

I have a very small script inwhich I'm trying to get it to post a command in IRC.
<?php
set_time_limit(0);
$socket = fsockopen("irc.twitch.tv", 6667);
fputs($socket,"USER greatbritishbgbot\n");
fputs($socket,"PASS oauth:95uf89rv74gawp93ilfafeqnihpxb74\n");
fputs($socket,"NICK greatbritishbgbot\n");
fputs($socket,"JOIN #greatbritishbg\n");
$ircChannel = "#greatbritishbg";
while($data = fgets($socket, 128)) {
$msg = "test";
fwrite($socket, "PRIVMSG " . $ircChannel . " :" . $msg . "\n");
echo nl2br($data);
flush();
}
?>
Every time I execute this, I receive the error: greatbritishbgbot HELP:Unknown command.
What is the correct way of using fputs to run an IRC command?
Edit: This question was requested to be closed and downvoted because I am apparently asking for debugging information. Which is incorrect. I have asked for an explanation into how one should use fputs in comparison to how I use it.
I ended up using WildPHP which only needed me to modify a config file.
I highly recommend it.

socket_recv I want readable data not byte count

Hello All hopefully you guys don't shoot me with virtual guns if already asked but, here goes.
I am socket_send to an ip/port i see the server receives and responds with data back. My code receives xxxx bytes. I want to know what those bytes entail IE: xml string back so I can parse and use the data back in my application. cURL is not doable in this situation because the server receives headers and does not respond at all.
**Code:**
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$port'...";
$result = socket_connect($socket, $address, $port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$in = $xml;
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
socket_shutdown($socket, 1);
echo "Reading response:\n\n";
$buf = '';
if (false !== ($bytes = socket_recv($socket, $buf, 2048, MSG_WAITALL))) {
echo "Read $bytes bytes from socket_recv(). Closing socket...";
} else {
echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($socket)) . "\n";
}
socket_close($socket);
echo $buf . "\n";
echo "OK.\n\n";
?>
Response:
TCP/IP Connection
OK. OK. Attempting to connect to 'xxx.xxx.xxx.xxx' on port '9000'...OK. Sending HTTP HEAD request...OK. Reading response: Read 1365 bytes from socket_recv(). Closing socket... OK.
Firstly, you can use cURL and not send headers! Just an fyi.
Secondly, it's going to be tricky to determine exactly what type of content it is... Is the server returning any sort of "content type"? If so, you may be able to read that.
Afterwards, you can then use $buf and then attempt to use XML libraries to parse the string (such as PHP's SimpleXML Library).
If, for example, it doesn't parse the XML, then you know it's either a regular string or something else.
cURL should return the content type, however.
After crazy headaches and several medicine hits I found the issue: 1 line OMG
If any one else gets this issue with socket the line after socket_close($socket) I added echo html_entity_decode($buf).

PHP SocketServer not acception more than 255 concurrent connections

I'm running a PHP (5.4.12) script on a Windows Server 2008 machine.
When the total concurrent connections hit 255, the script crashes.
On a PHP 5.2.x system the SocketServer end with an error:
"An operation was attempted on something that is not a socket."
PHP 5.4.12, does not crash but does not handle the connections anymore.
I'm using the following code, taken from the PHP.net website:
http://php.net/manual/en/sockets.examples.php
<?php
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$address = '192.168.1.7';
$port = 8676;
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
echo "socket_create() last error " . socket_strerror(socket_last_error()) . "\n";
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() last error " . socket_strerror(socket_last_error($sock)) . "\n";
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() last error " . socket_strerror(socket_last_error($sock)) . "\n";
}
$clients = array();
do {
$read = array();
$read[] = $sock;
$read = array_merge($read,$clients);
$write = NULL;
$except = NULL;
if(socket_select($read,$write , $except , $tv_sec = 5)===false)
{
echo(PHP_EOL.'\n\ncrash\n\n'.PHP_EOL);
exit();
}
if (in_array($sock, $read)) {
if (($msgsock = socket_accept($sock)) === false) {
echo "socket_accept() falló: razón: " . socket_strerror(socket_last_error($sock)) . "\n";
// break;
}
$clients[] = $msgsock;
$key = array_keys($clients, $msgsock);
$msg = "{$key[0]} welcome msg\n".PHP_EOL;
socket_write($msgsock, $msg, strlen($msg));
}
// Handle Input
foreach ($clients as $key => $client) { // for each client
if (in_array($client, $read)) {
if (false === ($buf = socket_read($client, 2048))) {
echo "socket_read() last error: " . socket_strerror(socket_last_error($client)) . "\n";
// break 2;
}
if (!$buf = trim($buf)) {
continue;
}
if ($buf == 'quit') {
$talkback = print_r($clients);
socket_write($client, $talkback, strlen($talkback));
unset($clients[$key]);
socket_close($client);
break;
}
if ($buf == 'shutdown') {
socket_close($client);
break 2;
}
$talkback = "Client {$key}: Echo: '$buf'.\n";
socket_write($client, $talkback, strlen($talkback));
echo "$buf\n";
}
}
} while (true);
socket_close($sock);
?>
I've tested the script by running "EchoServerTest.exe". I creates a number of concurrent connections.
First I thought it was a problem in WinSock, but I can't find that there is a limitation on 255 connections with WinSock.
I also changed the registry settings TcpNumConnections, TcpTimedWaitDelay and MaxUserPort.
I used the same code on a Ubuntu server, so the problem much be related to Windows.
I tested this on two different servers, both Windows Server 2008 (32bit and 64bit).
One locally and one on the internet. So firewall or other external problems should be ruled out.
I'm out of options.
Before you ask, do you really need so many connections? Yes.
There are many 'dead' connections, so I've altered the code. Now those 'dead' connection are now 'Timed-out'. So I have some time to solve this, but eventually the number of concurrent connections will reach 255 again.
Any ideas on this one?
I think the problem is in this line:
if (socket_listen($sock, 5) === false) {
You are manually restricting to 5 the number of backlog incoming connections queued for processing. In Linux this is silently truncated to SOMAXCONN, not that way on Windows. Try using SOMAXCONN instead of 5.

Live streaming doesn't work

I'm using FFmpeg to capture my screen:
ffmpeg -f dshow -i video="UScreenCapture" -r 5 -s 640x480 -acodec libmp3lame -ac 1 -vcodec mpeg 4 -vtag divx -q 10 -f mpegts tcp://127.0.0.1:1234
so let it stream to somewhere. The accepter script:
error_reporting(E_ALL); /* Allow the script to hang around waiting for connections. */
set_time_limit(30); /* Turn on implicit output flushing so we see what we're getting as it comes in. */
ob_implicit_flush();
$address = '127.0.0.1';
$port = 1234;
$outfile = dirname(__FILE__)."/output.flv";
$ofp = fopen($outfile, 'wb');
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) { echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; sleep (5); die; }
if (socket_bind($sock, $address, $port) === false) { echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; sleep (5); die; }
if (socket_listen($sock, 5) === false) { echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; sleep (5); die; }
if (($msgsock = socket_accept($sock)) === false) { echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n"; sleep (5); break; }
do {
$a = '';
socket_recv ($msgsock, $a, 65536, MSG_WAITALL);
fwrite ($ofp, $a);
//echo strlen($a)."\r\n";
} while (true);
it seems to save the stuff to the disk OK. Now here comes the html:
I dont really know how to do this, but based on an example:
<video src="/output.flv"></video>
but it doesn't do anything. And if I want to stream the live incoming stuff, then what's the matter?
HTML 5 Video will not support the Flv format HTML5 will be support the following format video only
.mp4 = H.264 + AAC
.ogg/.ogv = Theora + Vorbis
.webm = VP8 + Vorbis
study the HTML5 video basics in the following site
HTML5 video basics
if you want to play the flv you have to use the flash or Flex program or some flv players like flowplayer

Writing then reading back via sockets in PHP issues

I writing a command and then reading back from a server via sockets in PHP. We have 20 servers that all run a Node JS script which can receive these commands and execute them. The Node JS script will return "ok" which PHP reads back to confirm the command has gone through.
The Node JS script listens on port 9000 and is set to allow half open.
Most of the time this works fine, but when a high volume of commands are sent we get errors back occasionally that say this:
Contents: Message received back from socket was 'Unexpected token {'
Transport endpoint is not connected
The transport endpoint message suggests to me that it has not connected successfully.
I am no expert in sockets so I don't know whether the implementation I have used is "correct". It does work most of the time but I am aware that there are functions like socket_bind and socket_listen that may work better, though I am not sure what they do.
Here is the PHP code that we are using. Any suggestions would be most appreciated.
public function sendDaemonCommand($address, $template_id, $params = array()) {
$hostname = $this->getHostnameFromPrivateIP($address);
$port = 9000;
$command = array('template_id' => $template_id, 'params' => $params);
$command = json_encode($command);
// Create a TCP Stream socket
if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to create socket on " . $address . "\n\n" . socket_strerror(socket_last_error()) . "\n\nCommand:\n\n" . $command . "\n" . $this->functionTraceback());
return false;
}
// Connect to socket
if (socket_connect($sock, $address, $port) === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to connect to socket on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_close($sock);
return false;
}
// Write command to socket
$_command = $command;
$length = strlen($_command);
while (true) {
$sent = socket_write($sock, $_command, $length);
if ($sent === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to write command to socket on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_shutdown($sock, 2);
socket_close($sock);
return false;
}
if ($sent < $length) {
$_command = substr($_command, $sent);
$length -= $sent;
}
else {
break;
}
}
socket_shutdown($sock, 1);
// Read back from socket
if (($out = socket_read($sock, 1024)) !== false) {
#socket_shutdown($sock, 0);
$out = trim($out);
if ($out !== "ok") {
$this->mailError("Command Failed - " . $hostname, "Message received back from socket was '" . $out . "' on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_close($sock);
return false;
}
}
else {
$this->mailError("Command Failed - " . $hostname, "Failed to read from socket on " . $address . "\n\n" . socket_strerror(socket_last_error($sock)) . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
socket_shutdown($sock, 0);
socket_close($sock);
return false;
}
socket_close($sock);
return $out;
}
For a simple socket client such as this, I much prefer fsockopen() - it drastically reduces the complication of the client code and does not require the sockets extension, which is not available everywhere. The main disadvantage to this is that you lose the string error messages, but these are rarely that useful anyway - you still get an error string from creating the socket, and if read/write operations fail it's because the socket has become disconnected.
I'm also not sure how useful "allow half open" is in this context - I think it is likely to create more problems than it solves. The calls to socket_shutdown() are not doing anything useful here (and may be the source of your problems) - you would only use this if you are operating on a socket that will not be closed and may be operated on at a later point in time by some other code. Since you create a new socket and destroy it in the same routine, this is not the case.
Here is your code rewritten to use fsockopen() - as you can see, it is somewhat shorter and simpler. I have also modified it slightly so that it always returns a bool - your code returns either bool FALSE or string ok, and since there are only these two options it makes more sense for the function to always return a bool.
public function sendDaemonCommand($address, $template_id, $params = array()) {
// Prepare data
$hostname = $this->getHostnameFromPrivateIP($address);
$port = 9000;
$command = array('template_id' => $template_id, 'params' => $params);
$_command = $command = json_encode($command);
$length = strlen($_command);
// Connect to socket
if (!$sock = fsockopen($address, $port, $errNo, $errStr)) {
$this->mailError("Command Failed - " . $hostname, "Failed to connect to socket on " . $address . "\n\n" . $errStr . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
return false;
}
// Write command to socket
while (true) {
// Try and write data to socket
$sent = fwrite($sock, $_command, $length);
// If it failed, error out
if ($sent === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to write command to socket on " . $address . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
fclose($sock);
return false;
}
// If there is data left to send, try again
if ($sent < $length) {
$_command = substr($_command, $sent);
$length -= $sent;
continue;
}
// If we get here the write operation was successful
break;
}
// Read back from socket and close it
$out = fread($sock, 1024);
fclose($sock);
// Test the response from the server
if ($out === false) {
$this->mailError("Command Failed - " . $hostname, "Failed to read from socket on " . $address . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
return false;
} else if (($out = trim($out)) !== "ok") {
$this->mailError("Command Failed - " . $hostname, "Message received back from socket was '" . $out . "' on " . $address . "\n\nCommand:\n\n" . $command. "\n" . $this->functionTraceback());
return false;
} else {
return true;
}
}

Categories