Hello i made a php socket server to get data from plc, plc is configured as tcp socket client. I've a seriuous problem, if local net go down seems that function socket_accept stalled, plc don'be able to send me data. If i restart my server plc reconnect correctly.
Can someone hel me? My server code:
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 = ipserver;
$port = 10001;
if (($sock = socket_create(AF_INET, SOCK_STREAM, getprotobyname("TCP"))) === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "
";
exit;
}
if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
echo socket_strerror(socket_last_error($sock));
exit;
}
if (socket_bind($sock, $address, $port) === false) {
echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "
";
exit;
}
if (socket_listen($sock, 5) === false) {
echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "
";
exit;
}
while(true)
{
//$remote_fd = socket_accept($sock);
if (($remote_fd = socket_accept($sock)) === false) {
echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "
";
break ;
}
do {
$recv = "";
if (false === ($recv = socket_read($remote_fd, 128, PHP_BINARY_READ))) {
echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($remote_fd)) . "
";
#break 2;
}
if($recv != "") {
echo $recv."
";
inserisci_letture("t_letture",trim($recv));
if($sent=socket_write($remote_fd,"1",1)===false)
{
echo "socket_write() failed: reason: " . socket_strerror(socket_last_error($remote_fd)) . "
";
}
}
}
while($recv != "");
}
socket_shutdown($sock);
socket_close($sock);
?>