4

I am writing to a server using the following snippet.

$fp = connect();
$sent_requests = 0;
function connect() {
    $addr = gethostbyname("example.com");
    $fp = fsockopen("$addr", 80, $errno, $errstr);
    socket_set_blocking( $fp, false );
    if (!$fp) {
        echo "$errstr ($errno)<br />\n";
        exit(1);
    } else{
        echo "Connected\n";
        return $fp;
    }
}


function sendTestCalls($load){
    global $fp, $sent_requests;
    if(!$fp){
        echo "reconnecting";
        $sent_requests = 0;
        //echo stream_get_contents($fp) . "\n";
        fclose($fp);
        $fp = connect();
    }
    $data = "POST /test HTTP/2.0\r\n";
    $data.= "Host: example.com\r\n";
    $data.= "Content-Type: application/json\r\n";
    $data.= "Content-Length: ".strlen($load)."\r\n";
    $data.= "Connection: Keep-Alive\r\n";
    $data.= "xYtU87BVFluc6: 1\r\n";
    $data.= "\r\n" . $load;

    $bytesToWrite = strlen($data);
    $totalBytesWritten = 0;

    while ($totalBytesWritten < $bytesToWrite) {
        $bytes = fwrite($fp, substr($data, $totalBytesWritten));
        $totalBytesWritten += $bytes;
    }

    $sent_requests++;
}
$time = time();
for($i=0; $i<1000; $i++) {
    sendTestCalls('{"justtesting": "somevalue"}');
}
fclose($fp);
$time_taken = time() - $time;//might be a bit inaccurate
echo "Time Taken: " . $time_taken . "\n";

When I check my access logs on my server less than 1000 post requests are received (in the range of 0 to 900). What am I doing wrong here?

EDIT1 I suppose my socket is timing out. What should I do to check if it has disconnected in such a scenario reconnect. I tried using stream_get_meta_data($fp) but it had no effect.

Sohaib
  • 4,556
  • 8
  • 40
  • 68
  • Did you tried to use socket_set_timeout? Pleas see example here http://php.net/manual/en/function.stream-set-timeout.php for checking socket timeout – Fedir Petryk Oct 22 '15 at 13:34
  • Yes but that too works only for a certain time until the server closes the connection. After which everything goes along the same way. – Sohaib Oct 23 '15 at 07:23
  • Have you looked into example for handling timeouts which I provided link for? – Fedir Petryk Oct 23 '15 at 12:13

2 Answers2

0

Try to insert this before each request:

$info = stream_get_meta_data($fp); if ($info['timed_out']) { fclose($fp); $fp = connect(); }

Fedir Petryk
  • 497
  • 3
  • 16
0

I had found a solution to this at that point using php_curl and KEEP-ALIVE

Here is my updated version:

function sendCall(&$curl_handle, $data){
   curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "POST");
   curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
   curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
   curl_setopt(
       $curl_handle,
       CURLOPT_HTTPHEADER,
       array(
           'Content-Type: application/json',
           'Connection: Keep-Alive',
           'Content-Length: ' . strlen($data)
       )
   );
   $response = curl_exec($curl_handle); //Check response?
   if($err = curl_error($curl_handle)) {
       error_log("Error - $err Status - Reconnecting" );
       $curl_handle = curl_init(curl_getinfo($curl_handle, CURLINFO_EFFECTIVE_URL));
       sendCall($curl_handle, $data);
   }
}

This function gives me an almost always alive connection. (Never got the error log in more than a week). Hope it helps anyone looking for the same.

Sohaib
  • 4,556
  • 8
  • 40
  • 68