Jump to content

@fsockopen not opening socket, or atleast correctly!?


Krisando

Recommended Posts

Hello, I'm trying to get a little piece of code working here.

What it does is connect to a program on a certain port "7171", that is the problem. It wont seem to initilize the connection.

 

Code:

$sock = @fsockopen("118.90.70.75", 7171, $errno, $errstr, 1);
if ($sock)
{
	fwrite($sock, $info); 
	$data=''; 
	while (!feof($sock))
		$data .= fgets($sock, 1024);
	fclose($sock);
	preg_match('/players online="(\d+)" max="(\d+)"/', $data, $matches);
	$config['status']['serverStatus_online'] = 1;
	$config['status']['serverStatus_players'] = $matches[1];
	$config['status']['serverStatus_playersMax'] = $matches[2];
	preg_match('/uptime="(\d+)"/', $data, $matches);
	$h = floor($matches[1] / 3600);
	$m = floor(($matches[1] - $h*3600) / 60);
	$config['status']['serverStatus_uptime'] = $h.'h '.$m.'m';
	preg_match('/monsters total="(\d+)"/', $data, $matches);
	$config['status']['serverStatus_monsters'] = $matches[1];
}
else
{
	$config['status']['serverStatus_online'] = 0;
	$config['status']['serverStatus_players'] = 0;
	$config['status']['serverStatus_playersMax'] = 0;
}

 

I'm not sure what "$errno, $errstr" means. Maybe this could help me or be my problem?


   if (! $sock = @fsockopen("118.90.70.75", "7171", $num, $error, 5))
   {
      fwrite($sock, $info);
      $data='';
      while (!feof($sock))
         $data .= fgets($sock, 1024);
      fclose($sock);
      preg_match('/players online="(\d+)" max="(\d+)"/', $data, $matches);
      $config['status']['serverStatus_online'] = 1;
      $config['status']['serverStatus_players'] = $matches[1];
      $config['status']['serverStatus_playersMax'] = $matches[2];
      preg_match('/uptime="(\d+)"/', $data, $matches);
      $h = floor($matches[1] / 3600);
      $m = floor(($matches[1] - $h*3600) / 60);
      $config['status']['serverStatus_uptime'] = $h.'h '.$m.'m';
      preg_match('/monsters total="(\d+)"/', $data, $matches);
      $config['status']['serverStatus_monsters'] = $matches[1];
   }
   else
   {
      $config['status']['serverStatus_online'] = 0;
      $config['status']['serverStatus_players'] = 0;
      $config['status']['serverStatus_playersMax'] = 0;
fclose($sock);
   }

That should work for you.

Opps sorry brain fart, had two minor erros.

 

if ( $sock = @fsockopen("118.90.70.75", "7171", $num, $error, 5))
   {
      fwrite($sock, $info);
      $data='';
      while (!feof($sock))
         $data .= fgets($sock, 1024);
      fclose($sock);
      preg_match('/players online="(\d+)" max="(\d+)"/', $data, $matches);
      $config['status']['serverStatus_online'] = 1;
      $config['status']['serverStatus_players'] = $matches[1];
      $config['status']['serverStatus_playersMax'] = $matches[2];
      preg_match('/uptime="(\d+)"/', $data, $matches);
      $h = floor($matches[1] / 3600);
      $m = floor(($matches[1] - $h*3600) / 60);
      $config['status']['serverStatus_uptime'] = $h.'h '.$m.'m';
      preg_match('/monsters total="(\d+)"/', $data, $matches);
      $config['status']['serverStatus_monsters'] = $matches[1];
     fclose($sock);
   }
   else
   {
      $config['status']['serverStatus_online'] = 0;
      $config['status']['serverStatus_players'] = 0;
      $config['status']['serverStatus_playersMax'] = 0;
   }

The last "fclose($sock);" errored it I think because it was tryign to close the socket after it had already been closed so I took that off and it seems to be doing exactly the same thing.

 

It works fine locally/on my webserver accessed by someone else but not on my domains webserver, so it's a very strange problem indeed.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.