Multiple connections with PHP Socket to receive HTTP messages and save to Mysql

Asked

Viewed 844 times

2

I need to set up a port on my dedicated server that runs the Centos 6.10 kvm OS, so that it receives messages (geolocation) from some trackers via TCP (IP:PORT), and saves this data in the Mysql database by connecting a PHP script.

What has already been done:

1) I opened the TCP port 49100 using the terminal with the following commands:

> iptables -I INPUT -p tcp --dport 49100 -j ACCEPT
> iptables -nL |grep 49100
# ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:49100

2) I used a PHP script to connect to the port and accept multiple connections.

php server.

#!/usr/local/bin/php -q
<?php
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 = getHostByName(getHostName()); //IP LOCAL;
$port = 49100;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
}

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

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

do {
    if (($msgsock = socket_accept($sock)) === false) {
        echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "<br>";
        break;
    }
    /* Send instructions. */
    $msg = "<br>Welcome to the PHP Test Server. <br>" .
        "To quit, type 'quit'. To shut down the server type 'shutdown'.<br>";
    socket_write($msgsock, $msg, strlen($msg));

    do {
        if (false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))) {
            echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($msgsock)) . "<br>";
            break 2;
        }
        if (!$buf = trim($buf)) {
            continue;
        }
        if ($buf == 'quit') {
            break;
        }
        if ($buf == 'shutdown') {
            socket_close($msgsock);
            break 2;
        }
        $talkback = "PHP: You said '$buf'.<br>";
        socket_write($msgsock, $talkback, strlen($talkback));
        echo "$buf<br>";
    } while (true);
    socket_close($msgsock);
} while (true);

socket_close($sock);
?>

client.php

<?php
error_reporting(E_ALL);
$host = getHostByName(getHostName()); //IP LOCAL;
$port = 49100;
// No Timeout 
set_time_limit(0);

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

$message = 'Olá, mundo!';

//Create Socket
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");

//Connect to the server
$result = socket_connect($socket, $host, $port) or die("Could not connect toserver\n");

//Write to server socket
socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");

//Read the response from the server
$result = socket_read ($socket, 1024) or die("Could not read server response\n");
echo "Reply From Server  :" . $result;

//Close socket
socket_close($socket);
?>

I opened the browser and opened server.php and client.php, in that order.

client.php returned:

Reply From Server : Welcome to the PHP Test Server. To quit, type 'quit'. To Shut down the server type 'shutdown'.

server.php returned:

Warning: socket_read(): Unable to read from socket [104]: Connection reset by peer in /home/usuario/public_html/server.php on line 37 socket_read() failed: Reset connection by peer


The process of opening door 49100 was apparently successful. I have the following questions:

1) What is causing this Warning on the server.php?

2) How can I associate port 49100 to the PHP script so that every connection made by this port runs the script and receives the data sent?

1 answer

1


The problem is that as soon as the client receives the welcome message from the server he is running the socket_close function and terminating the connection.

This way, when the server tries to send a second message to the client the connection has already been terminated.

What you should do is use a while on your client so that it waits for the server to reply, instead of closing the connection.

That is, both client and server need to be in a while to be able to send several messages to each other... only after the last message is that the client must terminate the while and close the connection.

  • I get it, it makes sense. But can you tell me how I can keep this server.php script waiting for "all the time" messages? Type associate it to the port, and then when the port is accessed it runs and receives the data. Because the client will be a hardware accessing IP and Port...

  • But your server is already doing this. Note that the message that the server returns to you is just a Warning, ie a warning... The client closed the connection but the server keeps running waiting for new connections. Try starting multiple clients in sequence by connecting to this server. Clients will receive the message is close, but the server will continue running.

  • But for example, in the question code it only ran because I opened server.php and left it waiting for the client. If I open only the client.php it does not connect, because the server.php is stopped. (socket_connect(): Unable to connect [111]: Connection refused). Making a remote connection, it will not be possible to run the server manually.

  • I can add the script as a server service :) http://www.funphp.com/2010/12/01/create-a-socket-server-in-php-and-run-as-a-service/

  • 1

    In fact, in my reply and in the comments when I wrote "server" I was all the time referring to "server.php". The.php server needs to be running active on the web server in order for clients (in your case, "client.php") to connect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.