Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ServerPing;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import java.net.SocketException;
- import java.nio.charset.Charset;
- public class Server {
- private static String address;
- private static int port;
- private static int timeout;
- private static boolean online;
- private static int playercount;
- private static int maxplayers;
- private static String motd;
- public Server(String address, int port, int timeout){
- Server.address = address;
- Server.port = port;
- Server.timeout = timeout;
- }
- public static String getAddress(){
- return Server.address;
- }
- public static void setAddress(String address){
- Server.address = address;
- }
- public static int getPort(){
- return Server.port;
- }
- public static void setPort(int port){
- Server.port = port;
- }
- public static int getTimeOut(){
- return Server.timeout;
- }
- public static void setTimeOut(int timeout){
- Server.timeout = timeout;
- }
- public static boolean isOnline(){
- return Server.online;
- }
- public static void setOnline(boolean online){
- Server.online = online;
- }
- public static int getPlayerCount(){
- return Server.playercount;
- }
- public static void setPlayerCount(int playercount){
- Server.playercount = playercount;
- }
- public static int getMaxPlayers(){
- return Server.maxplayers;
- }
- public static void setMaxPlayers(int maxplayers){
- Server.maxplayers = maxplayers;
- }
- public static String getMotd(){
- return Server.motd;
- }
- public static void setMotd(String motd){
- Server.motd = motd;
- }
- @SuppressWarnings("resource")
- public static void ping(){
- try{
- Socket socket = new Socket();
- OutputStream outputStream;
- DataOutputStream dataOutputStream;
- InputStream inputStream;
- InputStreamReader inputStreamReader;
- socket.setSoTimeout(Server.timeout);
- socket.connect(new InetSocketAddress(Server.getAddress(), Server.getPort()), Server.getTimeOut());
- outputStream = socket.getOutputStream();
- dataOutputStream = new DataOutputStream(outputStream);
- inputStream = socket.getInputStream();
- inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-16BE"));
- dataOutputStream.write(new byte[]{(byte) 0xFE, (byte) 0x01});
- int packetID = inputStream.read();
- if(packetID == -1){
- dataOutputStream.close();
- outputStream.close();
- inputStream.close();
- inputStreamReader.close();
- socket.close();
- throw new IOException("Premature end of stream.");
- }
- if(packetID != 0xFF){
- dataOutputStream.close();
- outputStream.close();
- inputStreamReader.close();
- inputStream.close();
- throw new IOException("Invalid packet ID (" + packetID + ").");
- }
- int length = inputStreamReader.read();
- if(length == -1){
- dataOutputStream.close();
- outputStream.close();
- inputStreamReader.close();
- inputStream.close();
- socket.close();
- throw new IOException("Premature end of stream.");
- }
- if(length == 0){
- dataOutputStream.close();
- outputStream.close();
- inputStream.close();
- inputStreamReader.close();
- socket.close();
- throw new IOException("Invalid string length.");
- }
- char[] chars = new char[length];
- if(inputStreamReader.read(chars, 0, length) != length){
- dataOutputStream.close();
- outputStream.close();
- inputStream.close();
- inputStreamReader.close();
- socket.close();
- throw new IOException("Premature end of stream.");
- }
- String string = new String(chars);
- if(string.startsWith("§")){
- String[] data = string.split("\0");
- Server.setMotd(data[3]);
- Server.setPlayerCount(Integer.parseInt(data[4]));
- Server.setMaxPlayers(Integer.parseInt(data[5]));
- }else{
- String[] data = string.split("§");
- Server.setMotd(data[0]);
- Server.setPlayerCount(Integer.parseInt(data[1]));
- Server.setMaxPlayers(Integer.parseInt(data[2]));
- }
- dataOutputStream.close();
- outputStream.close();
- inputStream.close();
- inputStreamReader.close();
- socket.close();
- }catch(SocketException e){
- Server.setOnline(false);
- }catch(IOException e){
- Server.setOnline(false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement