ev5unleash1 Posted December 4, 2012 Share Posted December 4, 2012 (edited) Hi all, I'm currently trying to run a PHP Socket Server though I'm having trouble with responding after so many reconnects. It would seem after 3-5 reconnects that it will just stop taking connections altogether. Any ideas? Basically I want the script to do as it does right now, but be able to accept a new connection at any time. Though it only has to be one connection, so kicking the previous connection off doesn't matter to me. Thanks, <?php echo "Web Socket Script = Coded in PHP" . PHP_EOL; echo $custom; error_reporting(0); function action_put($probe) { $memcache = new Memcache; $memcache->connect('localhost', 11211) or $error = "Memcache: Could not connect to Cache server"; $memcache->set('key', $probe, false) or $error = $error . ". Failed to save data at the server."; if(isset($error)) { echo "Data not stored: " . $error; } else { } $respond = $memcache->get('key'); } function action_get() { $memcache = new Memcache; $memcache->connect('localhost', 11211) or die ("Memcache: Could not connect to Cache server"); $respond = $memcache->get('key'); return $respond; } function cachetodisk() { $cache = action_get(); file_put_contents('probe.txt', $cache); } function disktocache() { $disk = file_get_contents('probe.txt', 'r'); action_put($disk); } /* Register Shutdown on close */ register_shutdown_function('cachetodisk'); $respond = disktocache(); /* Allow the script to hang around waiting for connections. */ set_time_limit(0); ini_set('default_socket_timeout', '0'); /* Turn on implicit output flushing so we see what we're getting * as it comes in. */ ob_implicit_flush(); $address = '10.10.2.4'; //$port = 90; 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; } /* Set Default Data */ /* Send instructions. */ //echo "Client connected from: "; if ($_SERVER['HTTP_CLIENT_IP']) $visitorip = socket_getpeername($sock, $address, $port); echo $visitorip; $msg = "Welcome to the PHP Test Server." . PHP_EOL; socket_write($msgsock, $msg, strlen($msg)); $echo = "000000"; if($probetest != true) { action_put($echo); } $respond = action_get(); socket_write($msgsock, $respond, strlen($respond)); do{ //Respond once $respond = action_get(); if($respond == $respondnew) { usleep(1000); //1ms responce time } else { //Removed bunch of if else statements that don't really matter. $echo = $echo . PHP_EOL; $respondnew = $respond; if(socket_write($msgsock, $echo, strlen($echo)) === FALSE) { $connection = "closed"; } socket_close($socket); usleep(1000); //1ms responce time } } while (isset($connection) != TRUE); unset($connection); // if($probetest == TRUE) { shell_exec('C:\wamp\bin\php\php5.4.3\php.exe "C:\wamp\wwwshare\car\sockettest2.php" > NUL'); die();} } while (true); socket_close($msgsock); socket_close($sock); while($i = 1); ?> Edited December 4, 2012 by ev5unleash1 Quote Link to comment https://forums.phpfreaks.com/topic/271572-php-socket-server-server-stops-responding/ Share on other sites More sharing options...
requinix Posted December 4, 2012 Share Posted December 4, 2012 http://forums.phpfreaks.com/topic/271512-php-telnet-script-timeout/ Quote Link to comment https://forums.phpfreaks.com/topic/271572-php-socket-server-server-stops-responding/#findComment-1397379 Share on other sites More sharing options...
ev5unleash1 Posted December 4, 2012 Author Share Posted December 4, 2012 @requinix That's my old script regarding a socket server. This is a completely different where here I'm accepting socket connections as opposed to connecting to one. Quote Link to comment https://forums.phpfreaks.com/topic/271572-php-socket-server-server-stops-responding/#findComment-1397380 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.