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?
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...
– Jhonatan Pereira
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.
– wensiso
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.
– Jhonatan Pereira
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/
– Jhonatan Pereira
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.
– wensiso