Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------- CLASE ServerPinger ---------------
- import java.io.ByteArrayOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import java.nio.charset.Charset;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- public class ServerPinger {
- public static ConnectionData ping(String ip, int port) {
- try {
- InetSocketAddress address = new InetSocketAddress(ip, port);
- Socket socket = new Socket();
- socket.setSoTimeout(1200);
- socket.connect(address, 1200);
- ByteArrayOutputStream bs = new ByteArrayOutputStream();
- DataOutputStream out = new DataOutputStream(bs);
- out.write(0x00); // ID de paquete
- int version = 4; // ID de versión
- writeInt(version, out);
- writeString(address.getHostString(), out);
- out.writeShort(address.getPort());
- writeInt(1, out);
- DataOutputStream out2 = new DataOutputStream(socket.getOutputStream());
- sendPacket(bs.toByteArray(), out2);
- out.close();
- bs.close();
- sendPacket(new byte[]{ 0x00 }, out2);
- DataInputStream in = new DataInputStream(socket.getInputStream());
- readInt(in); // Esto es algo que realmente ni utilizamos, pero hay que leerlo, porque si no falla
- int packetId = readInt(in);
- if (packetId != 0x00) {
- socket.close();
- throw new RuntimeException("PacketId incorrecto.");
- }
- int strLength = readInt(in);
- if (strLength < 1) {
- socket.close();
- throw new RuntimeException("Tamaño de string demasiado corto.");
- }
- byte[] responseData = new byte[strLength];
- in.readFully(responseData);
- in.close();
- out2.close();
- socket.close();
- String json = new String(responseData, Charset.forName("utf-8"));
- JsonObject object = new JsonParser().parse(json).getAsJsonObject();
- String motd = object.get("description").getAsString();
- int players = object.get("players").getAsJsonObject().get("online").getAsInt();
- int max = object.get("players").getAsJsonObject().get("max").getAsInt();
- return new ConnectionData(Status.ONLINE, motd, players, max);
- } catch (IOException ex) {
- return new ConnectionData(Status.OFFLINE);
- }
- }
- static void writeInt(int i, DataOutputStream out) throws IOException {
- while (true) {
- if ((i & 0xFFFFFF80) == 0) {
- out.write(i);
- return;
- }
- out.write(i & 0x7F | 0x80);
- i >>>= 7;
- }
- }
- static void writeString(String str, DataOutputStream out) throws IOException {
- writeInt(str.length(), out);
- out.write(str.getBytes(Charset.forName("utf-8")));
- }
- static void sendPacket(byte[] data, DataOutputStream out) throws IOException {
- writeInt(data.length, out);
- out.write(data);
- }
- static int readInt(DataInputStream in) throws IOException {
- int i = 0;
- int j = 0;
- while (true) {
- int k = in.readByte();
- i |= (k & 0x7F) << j++ * 7;
- if (j > 5) {
- throw new RuntimeException("Int demasiado grande.");
- }
- if ((k & 0x80) != 128) {
- break;
- }
- }
- return i;
- }
- public static class ConnectionData {
- Status status;
- String motd;
- int players, max;
- public ConnectionData(Status status) {
- this.status = status;
- }
- public ConnectionData(Status status, String motd, int players, int max) {
- this.status = status;
- this.motd = motd;
- this.players = players;
- this.max = max;
- }
- public Status getStatus() {
- return status;
- }
- public String getMotd() {
- return motd;
- }
- public int getPlayers() {
- return players;
- }
- public int getMaxPlayers() {
- return max;
- }
- }
- public enum Status {
- ONLINE, OFFLINE;
- }
- }
- --------------- Ejemplo ---------------
- public class Prueba {
- public static void main(String[] args) {
- ConnectionData data = ServerPinger.ping("192.99.4.13", 11701);
- System.out.println(data.getMotd());
- System.out.println(data.getPlayers());
- System.out.println(data.getMaxPlayers());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement