• Check if server is online?
    12 replies, posted
How can I ping a server IP and check if it's online or not? I want to have a web page that shows the status of a Minecraft server to show whether it is online or not.
Open a socket on the port Minecraft is listening on
[php] <?php function serverAlive($serverLocation, $serverPort){ error_reporting(0); return !(!fsockopen($serverLocation, $serverPort)); } echo "server is " . ((serverAlive($_GET['s'], $_GET['p']))? "up" : "down") ; ?> // Provide server address with ?s= and port with &p=. [/php]
[QUOTE=andersonmat;25698434][php]!(!fsockopen[/php][/QUOTE] I'm a little puzzled by the significance of the double negate there :\
[QUOTE=Siemens;25698481]I'm a little puzzled by the significance of the double negate there :\[/QUOTE] fsockopen() returns a file pointer when the connection is opened properly. Rather than returning that, I simply check if it is valid based upon (!fsockopen()) and then reverse the negative with another !(). That makes it return a boolean vs a file pointer.
[QUOTE=andersonmat;25698540]fsockopen() returns a file pointer when the connection is opened properly. Rather than returning that, I simply check if it is valid based upon (!fsockopen()) and then reverse the negative with another !(). That makes it return a boolean vs a file pointer.[/QUOTE] Couldn't you just stick in a '!== FALSE'? Would be more readable IMO. Awesome code snippet regardless.
It's more fun my way. :wink: [editline]28th October 2010[/editline] It's also a few less bytes.
[QUOTE=andersonmat;25698563]It's also a few less bytes.[/QUOTE] optimized! :hurr:
It also looks better. :P
[QUOTE=andersonmat;25698563]It's also a few less bytes.[/QUOTE] Fewer. :eng101:
[QUOTE=Qombat;25698699]Fewer. :eng101:[/QUOTE] Technicalities. :derp:
[QUOTE=andersonmat;25698434][php] <?php function serverAlive($serverLocation, $serverPort){ error_reporting(0); return !(!fsockopen($serverLocation, $serverPort)); } echo "server is " . ((serverAlive($_GET['s'], $_GET['p']))? "up" : "down") ; ?> // Provide server address with ?s= and port with &p=. [/php][/QUOTE] This is perfect, thanks.
I made a configurable python script that you're free to use for this :v: It returns an image instead of a string, though. There's a few guides on minecraftforum.net, but here's an example since that tutorial still uses the link to the php one. Checking on 127.0.0.1 on port 9001: [img]http://res.public-craft.com/hb.pyc?ip=127.0.0.1&port=9001&on=INSERTURLHERE&off=INSERTURLHERE[/img] ( http://res.public-craft.com/hb.pyc?ip=127.0.0.1&port=9001&on=INSERTURLHERE&off=INSERTURLHERE )
Sorry, you need to Log In to post a reply to this thread.