Jump to content

Php Socket Server - Server Stops Responding


ev5unleash1

Recommended Posts

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 by ev5unleash1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.