Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Sockets is half-working

Tags:

php

sockets

chat

So, i have mIRC making a listen (bear with me on this one) on port 1235 and i tried to make a php script to connect to such port on my localhost server.

My localhost is a clean Apache+PHP with mIRC being run on another computer (inside lan).

The script works half-right as: 1) it connects to the 1235 port 2) it sends the $i 2.1) but on the other side no msg is recieved (aka I get a sock read event but no text pops up) 3) it reads all the incoming messages correclty 4) it closes when 'end' is the message drumroll 5) it only works IF the while function isn't present. 5.1) aka Inifi-loading. it only shows echo when socket is closed via 'end'

here is the code, which is a simple example i found on php.net, thanks in advance :)

    <?php
//The Client
error_reporting(E_ALL);

$address = "192.168.1.101";
$port = 1235;

/* 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 "socket successfully created.\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 "successfully connected to $address.\n<br>";
}
$allow = true;
$i = 0;
while ($allow == true)
{
    $i++;
    echo "Sending $i to server.\n<br>";
    socket_write($socket, $i, strlen($i));

    $input = socket_read($socket, 2048);
    if ($input == 'end') {
        $allow = false; 
    }
    echo "Response from server is: $input\n";
    sleep(5);
}

echo "Closing socket...";
socket_close($socket);
?>

this is what i get in browser, after saying 'end'

socket successfully created. Attempting to connect to '192.168.1.101' on port '1235'...successfully connected to 192.168.1.101. Sending 1 to server. Response from server is: ok Response from server is: end

this is in mIRC:

test: 89.152.172.21 is in! read close Blockquote

if i had left it working for an hour, multiply "read" for 60 and "sending N+1 to server" but this only shows up AFTER sock close (or by stoping the while)

like image 270
MoshMage Avatar asked Feb 04 '11 11:02

MoshMage


1 Answers

You have "plain" socket_write() and socket_read() calls. Unfortunately socket functions are unreliable by design (that's behavior inherited from BSD sockets). You can't just call socket_write() and expect it to actually write bytes (as odd as it sounds…)

PHP manual quote:

socket_write() does not necessarily write all bytes from the given buffer. It's valid that, depending on the network buffers etc., only a certain amount of data, even one byte, is written though your buffer is greater. You have to watch out so you don't unintentionally forget to transmit the rest of your data.

You have to check how many bytes have been written and keep retrying until all you wanted has been sent.

The same goes for reading. Read call can read as much as you requested or less and you're expected to retry the call.

Make sure to set sockets to blocking and then call read/write in a loop, or use (equally awful) select() call to find which sockets may have some data or may have finished sending.


If you want API that's not such PITA, then use streams instead, which have reliable reads/writes.

If you don't necessarily need PHP, then have a look at Node JS, which is specially designed server/environment for programs with long-lived network connections.

like image 164
Kornel Avatar answered Oct 07 '22 07:10

Kornel