2

I have a PHP socket server who needs to communicate with multiple clients. I know if(socket_listen($sock, 5) listens for 5 clients. But when I try to connect 2nd device it doesn't connect, it's like a connection with the first device blocking other connection.

I also have the problem when Server and Client communicate and for some reason connection is closed, then if I try to connect the same device again or other it won't work. Then I need to restart the server so it is able to accept connections again.

<?php
require "init.php";
$sql = "SELECT * FROM `info` where sent = 0 order by id desc";

error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting
 * as it comes in. */
ob_implicit_flush();

$address = '192.168.50.30';
$port = 3005;
if(($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if(socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if(socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
        break;
    }
    /* Send Welcome. */
    $msg = "HELLO".chr(0);
    socket_write($msgsock, utf8_encode ($msg), strlen($msg));


    do {

        $result = $con->query($sql);
        $row = $result->fetch_assoc();

        if(!empty($row["Token"])){
            $msg = $row["Token"]. chr(0);

            socket_write($msgsock, utf8_encode($msg), strlen($msg));
            $result = $con->query("update info set sent = 1 where id = ".$row["id"]);
        }


//        $talkback = $_SERVER['REMOTE_ADDR']." Said: '$buf'.\n".chr(0);
//        socket_write($msgsock, $talkback, strlen($talkback));
    } while (true);
    socket_close($msgsock);
} while (true);

socket_close($sock);
?>
SaschaM78
  • 4,376
  • 4
  • 33
  • 42
Andry Jhonas
  • 379
  • 2
  • 13
  • PHP is single threaded, so you cannot service multiple sockets concurrently. You can try something with [select](https://www.php.net/manual/en/function.socket-select.php) or [fork](https://www.php.net/manual/en/function.pcntl-fork.php). But you should seriously consider doing this in another language that does support concurrency. PHP is really designed around request/response cycles and not long running servers. If all you do is put database values on a TCP connection you have lots and lots of options. – Peter Apr 18 '19 at 06:45
  • @Peter You telling me that I need to make Server with another programming language so I can be able to service multiple sockets? Do yo have any suggestion or some Sample server demo source code. I know only PHP and a bit Java at this time. – Andry Jhonas Apr 18 '19 at 07:12
  • 1
    You can have a look at reactphp, this could solve your problem – Eakethet Apr 18 '19 at 07:19
  • 1
    No, you do not *have to* use another language. I'm just saying that PHP isn't well suited for this task. In my earlier comment I suggested two methods of making it work in PHP. Reactphp is another one, although as far as I remember the database queries will still block. – Peter Apr 18 '19 at 08:41

0 Answers0