Check if server is online

Discussion in 'BungeeCord Plugin Development' started by iRaphi, Oct 28, 2013.

  1. Hey,

    is there a code where i can simply check if the server "xy" online is?

    thanks
     
  2. http://is.gd/s1K03M
     

  3. Examle,

    in my Bungee Command /status

    Lobby Server: ONLINE / OFFLINE
    Minigames: ONLINE / OFFLINE
     
  4. You seem to be just asking for _THE_ code.

    Code something first and we'll help you if you're stuck at something.
     
    • Agree Agree x 1

  5. No i didn't mean just to get the Code, sorry.

    I don't know how to get the server status.
     
  6. I was literally looking for the same thing yesterday, and still don't have the solution.
    Maybe if you tried to get the player count it would return something funky like -1 if the server was down.
     

  7. I'll try something like that. :)
     
  8. Outlaw11A

    Supporter

    If you are asking about something that will check a server through a website, I can help you out. Just PM me.

    Edit: just realised you are referring to a plugin. Ignore me.
     
  9. Nah, we're talking about Bungee pinging a server to see if its online (ideally without using sockets, just bungee code)
     
  10. Why don't use the plugin messaging channel?
     
  11. I was just talking about this on the IRC, and really the best way it to do what @zearthur99 said. There is no practical function with the plugin message channel.
     
  12. You can try something like this :)
    PHP:
    <?php
    $statut = @fsockopen("YOURSERVER", 25565, $errno, $errstr, 1);
        if($statut) {
            fwrite($statut, "\xFE\x01");

            $d = fread($statut, 1024);
            if ($d != false AND substr($d, 0, 1) == "\xFF") {
                $d = substr($d, 3);
                $d = mb_convert_encoding($d, 'auto', 'UCS-2');
                $d = explode("\x00", $d);
                fclose($statut);
                    $d = str_replace('§e','',$d);
                    $d = str_replace('§b','',$d);
                    echo 'Version: '.$d[2];
                    echo '<br/>';
                    echo 'MOTD: '.$d[3];
                    echo '<br/>';
                    echo 'Players online: '.$d[4];
                    echo '<br/>';
                    echo 'Slots number '.$d[5];
                    echo '<br/>';
                    echo '<br/>';
                    echo 'Players online slot / Slots: '.$d[4].'/'.$d[5];
            }
        }
    ?>
     
  13. This might work. Try it and see :D

    PHP:
    import java.util.HashMap;
    import java.util.concurrent.TimeUnit;

    import net.md_5.bungee.api.Callback;
    import net.md_5.bungee.api.ServerPing;
    import net.md_5.bungee.api.config.ServerInfo;
    import net.md_5.bungee.api.plugin.Plugin;

    public class ServerStuff extends Plugin {
       
        private HashMap<String, Boolean> serverStatus = new HashMap<String, Boolean>();
       
        @Override
        public void onEnable() {
            getProxy().getScheduler().schedule(this, new TestRunnable(this), 0L, 5L, TimeUnit.SECONDS);
        }
       
        public final boolean getServerStatus(String name){
           
            return serverStatus.get(name);
           
        }
       
        public final void setServerStatus(String name, boolean online){
           
            serverStatus.put(name, online);
           
        }

    }

    class TestRunnable implements Runnable {
       
        private ServerStuff plugin;

        public TestRunnable(ServerStuff plugin) {
            this.plugin = plugin;
        }

        @Override
        public void run() {
           
            ServerInfo si = plugin.getProxy().getServerInfo("Lobby");
            si.ping(new TestCallback(plugin, si.getName()));
           
        }
       
    }

    class TestCallback implements Callback<ServerPing> {

        private ServerStuff plugin;
        private String name;

        public TestCallback(ServerStuff plugin, String name) {
            this.plugin = plugin;
            this.name = name;
        }

        @Override
        public void done(ServerPing ping, Throwable arg1) {
           
            plugin.setServerStatus(name, (ping.getFaviconObject() == null));
           
        }
       
    }
     
  14. This might not be what you're looking for, but you could use "UptimeRobot" (google).
    It basically sends a text or emails you ever time your server goes offline.

    Hope this helps,
     
  15. Here you go:
    Code (Text):

        public static boolean isOnline(String ipandport)
        {
            String[] addr = ipandport.split(":");
            try
            {
                Socket s = new Socket();
                s.connect(new InetSocketAddress(addr[0], Integer.parseInt(addr[1])));
            //    Bukkit.getServer().broadcastMessage(addr[0] + ":" + addr[1]);
                s.close();
                return true;
            }
            catch (NumberFormatException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (UnknownHostException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return false;
        }
     
    Regards,
    Matt