7

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)

2
  • 1
    Can you post what echos it's spitting out when you run that? Could be helpful in debugging it.
    – xil3
    Commented Feb 4, 2011 at 11:07
  • updated the post to have the echos spat out :)
    – MoshMage
    Commented Feb 4, 2011 at 11:46

3 Answers 3

2

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.

1
  • That explains alot. Also, thanks for such a detailed answer - i had lost hope to ever get this functioning :) Will do and check streams, as it seems to be easier and will also check Node.js because it sounds like exactly what i want
    – MoshMage
    Commented Mar 28, 2011 at 14:15
0

Change

socket_write($socket, $i, strlen($i));

To:

socket_write($socket, strval($i), strlen(strval($i)));

You are providing an integer where PHP expects a string.

1
  • even if it did, i still get 0Rcvd bytes on mIRC
    – MoshMage
    Commented Feb 5, 2011 at 0:01
0

From one of the notes in the documentation: http://php.net/manual/en/function.socket-write.php

Some clients (Flash's XMLSocket for example) won't fire a read event until a new line is recieved.

So try changing your code:

socket_write($socket, $i, strlen($i)); to: socket_write($socket, $i.'\n', strlen($i)+1);

1
  • Double quotes for a real newline.
    – igorw
    Commented Mar 26, 2011 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.