0

I work with two servers which send data to each other using PHP fsockopen(). The code has been working fine for years on Windows Server 2008 R2. We recently set up a new server on Windows Server 2016. On this server, things work fine most of the time, but intermittently the socket error pops up: send of X bytes failed with errno=10054 An existing connection was forcibly closed by the remote host.

We initially thought there might be a network issue with the new server, and set up another one (also Windows Server 2016) with a different provider, only to have the same intermittent issue.

As it is intermittent, and only experienced on Windows Server 2016, I am assuming there has been some change in the newer versions of Windows Server which impacts sockets/TCP connections, but I cannot work out what! I'm not a socket expert, and any suggestions gratefully received.

FYI - the servers are fairly low traffic, and using TCPView I can see that there are usually about 100 TCP connections establisted at any one time (20ish Established, 40ish Listening, and 25ish in Time Wait). This doesn't seem like enough to me to cause issues with running out of ports/maxing out TCP connections, but I don't know enough on the topic to be sure!

This is for Windows Server 2016, running PHP 5.3 (old, I know).

$fp=fsockopen("ssl://$server1_IP", 443, $errno, $errstr, 10); 

if($fp)

{   

$eol="\r\n";

fputs($fp, "POST example.php HTTP/1.1$eol");

fputs($fp, "HOST: $server1_IP$eol");

fputs($fp, "Connection: close$eol");

fputs($fp, "Content-Type: application/x-www-form-urlencoded$eol");

fputs($fp, 'Content-Length: '.strlen($data).$eol);

fputs($fp, $eol);

fputs($fp, $data);

fputs($fp, $eol);   

while(!feof($fp))

{

    $response=fgets($fp, 4000);

}

fclose($fp);

}
Dave
  • 5,108
  • 16
  • 30
  • 40
lanna
  • 1
  • You're using a PHP version that was introduced a decade ago and has been end-of-life for 5 years. Upgrade! – miken32 Apr 12 '19 at 19:18
  • Also, this really seems to be a case of reinventing the wheel. Making a POST request can be done in any number of libraries or just using PHP built-in functions, and under no circumstances I can think of should it involve manually opening and closing socket handles. – miken32 Apr 12 '19 at 19:21
  • See here for some suggestions: https://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php – miken32 Apr 12 '19 at 19:22
  • Thanks for the response. Agreed re PHP version - we are currently in the process of upgrading. But do you really think the PHP version could explain the issue we are facing? Also, we have tried various methods of POST request (curl etc), and all have had the same issue, so while it may be worth us changing the way we make POST requests, that doesn't seem to be the issue here. – lanna Apr 23 '19 at 09:00

0 Answers0