hlstriker Posted June 11, 2007 Share Posted June 11, 2007 Is there a way to ping an ip and the port to see if that server is online or offline? Example: $check = pingfunction() if(check == 1) { // server is online } else { // server is offline } Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/ Share on other sites More sharing options...
smc Posted June 11, 2007 Share Posted June 11, 2007 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 https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272430 Share on other sites More sharing options...
hlstriker Posted June 11, 2007 Author Share Posted June 11, 2007 Thanks, should "fclose($sock);" be run even if the sock is closed? Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272501 Share on other sites More sharing options...
smc Posted June 11, 2007 Share Posted June 11, 2007 Nope, if it was closed it wasen't able to open it in the first place thus there is nothing to close! Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272502 Share on other sites More sharing options...
hlstriker Posted June 11, 2007 Author Share Posted June 11, 2007 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 https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272506 Share on other sites More sharing options...
smc Posted June 11, 2007 Share Posted June 11, 2007 Well if the port is totally wrong then it will definitly have connection refused. Make sure the port and address are correct. I've tested this on my web server and it works. Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272509 Share on other sites More sharing options...
hlstriker Posted June 11, 2007 Author Share Posted June 11, 2007 I first tested on the correct ip and port and it said "connection refused". That's why I also tested on a wrong port to see if it was the same error. Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272510 Share on other sites More sharing options...
hlstriker Posted June 11, 2007 Author Share Posted June 11, 2007 Some reason it can connect to google, but not the game servers I try : / Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272594 Share on other sites More sharing options...
chigley Posted June 11, 2007 Share Posted June 11, 2007 Do you change the port to that of the gameserver? Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272595 Share on other sites More sharing options...
hlstriker Posted June 11, 2007 Author Share Posted June 11, 2007 Yep, it's a half-life type game server. The port is 27015. Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272602 Share on other sites More sharing options...
hlstriker Posted June 11, 2007 Author Share Posted June 11, 2007 Could this somehow be the game servers blocking the connection or something? Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272711 Share on other sites More sharing options...
smc Posted June 11, 2007 Share Posted June 11, 2007 Its possible they might have a general generic ping firewall, which would actually be a good thing. I'm not sure of a way to get around that Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272714 Share on other sites More sharing options...
hlstriker Posted June 11, 2007 Author Share Posted June 11, 2007 When I ping the ip through the command prompt it works perfect. Every game server ip I try (even ones that aren't mine) to ping with the sockets fail. Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272725 Share on other sites More sharing options...
hlstriker Posted June 12, 2007 Author Share Posted June 12, 2007 I found the problem. I had to add "udp://" in with the ip. Thanks for all your help. Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272796 Share on other sites More sharing options...
smc Posted June 12, 2007 Share Posted June 12, 2007 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 https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272799 Share on other sites More sharing options...
hlstriker Posted June 12, 2007 Author Share Posted June 12, 2007 Bah, you are right. I thought I hit the jackpot, so disappointing I'm stumped at why this isn't working properly. Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272859 Share on other sites More sharing options...
smc Posted June 12, 2007 Share Posted June 12, 2007 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 https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272864 Share on other sites More sharing options...
btherl Posted June 12, 2007 Share Posted June 12, 2007 Is the service you are pinging running on tcp or udp? Or both? Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272867 Share on other sites More sharing options...
hlstriker Posted June 12, 2007 Author Share Posted June 12, 2007 udp I'm 99% sure. Link to comment https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272881 Share on other sites More sharing options...
btherl Posted June 12, 2007 Share Posted June 12, 2007 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 https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272888 Share on other sites More sharing options...
smc Posted June 12, 2007 Share Posted June 12, 2007 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 https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-272896 Share on other sites More sharing options...
hlstriker Posted June 13, 2007 Author Share Posted June 13, 2007 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 https://forums.phpfreaks.com/topic/55105-ping-ipport-to-see-if-server-is-online/#findComment-273639 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.