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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.