Jump to content

Ping ip+port to see if server is online


hlstriker

Recommended Posts

Heres an example

 

$ip = "example.com"; //IP or web addy
$port = "80"; //Port

$sock = @fsockopen( $ip, $port, $num, $error, 2 ); //2 is the ping time, you can sub what you need

//Since fsockopen function returns TRUE or FALSE we can just plug it into the IF/THEN statement. An IF/ELSE would have worked to

if( !$sock ){

//Do this if it is closed
echo( "It appears to be closed" );

}

if( $sock ){

//Do this if it is open
echo( "It appears to be open" );
fclose($sock);

}

 

Hope this helps :)

Ok thanks alot :)

 

Also, even when my server is up, it displays as down. I added the error into the string and it says "Connection refused". I then changed the port to something completely wrong and it still said "Connection refused".

 

I'm testing this on a game server btw.

No, careful!

 

This is in big bold letters on the PHP site:

UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data.

Well we determined on another thread that it is a server problem. The only two ways to do it are eitheir to use a proxy server that is enabled to open a socket to non-standard ports, or to just ping the server on the standard port of 80 to see if the server itself is online.

 

Best thing I can suggest :(

Aha, then fsockopen on its own will not be helpful.  Opening a udp connection does absolutely nothing.  The only way to ping a udp port is to send something and check the response you get back.  So you need to capture a packet from the game protocol and send that over the udp port, and see if you get a response.

 

Or, you can check port 80 like smc suggested, and just assume that if that responds, then the game server will be running :)  That's less reliable but considerably easier.

 

Another option is to use true "ping", which uses ICMP rather than TCP or UDP.  That has the same drawbacks as checking port 80, which is that you don't really know if the game server is running or not.  It probably has a higher risk of being firewalled than port 80.

So long story short chances are the only way you can fix this problem is to do packet monitoring but that will probably be blocked by your hosting provider as it is even more high-risk than the port ping and you'll be stuck in the same boat.

 

You can try packets, but if your willing to live with the consolation of non-100% accuracy then going for a ping on port 80 would save you a considerable amount of hassle.

Well, there are many game servers on the same ip so this wouldn't be reliable what so ever.

 

The packet suggestion might work though. Currently I have a script that gets data (player names, kills, deaths, etc..) from the server and displays it on my website with packets, so it must be able to do that.

 

The only problem with that script is... if the server is down then it takes the page ages on end to load, then displays errors.

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.