Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. --------------- CLASE ServerPinger ---------------
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.net.InetSocketAddress;
  7. import java.net.Socket;
  8. import java.nio.charset.Charset;
  9.  
  10. import com.google.gson.JsonObject;
  11. import com.google.gson.JsonParser;
  12.  
  13. public class ServerPinger {
  14.    
  15.     public static ConnectionData ping(String ip, int port) {
  16.         try {
  17.             InetSocketAddress address = new InetSocketAddress(ip, port);
  18.            
  19.             Socket socket = new Socket();
  20.            
  21.             socket.setSoTimeout(1200);
  22.            
  23.             socket.connect(address, 1200);
  24.            
  25.             ByteArrayOutputStream bs = new ByteArrayOutputStream();
  26.            
  27.             DataOutputStream out = new DataOutputStream(bs);
  28.            
  29.             out.write(0x00); // ID de paquete
  30.            
  31.             int version = 4; // ID de versión
  32.            
  33.             writeInt(version, out);
  34.            
  35.             writeString(address.getHostString(), out);
  36.            
  37.             out.writeShort(address.getPort());
  38.            
  39.             writeInt(1, out);
  40.            
  41.             DataOutputStream out2 = new DataOutputStream(socket.getOutputStream());
  42.            
  43.             sendPacket(bs.toByteArray(), out2);
  44.            
  45.             out.close();
  46.            
  47.             bs.close();
  48.            
  49.             sendPacket(new byte[]{ 0x00 }, out2);
  50.            
  51.             DataInputStream in = new DataInputStream(socket.getInputStream());
  52.            
  53.             readInt(in); // Esto es algo que realmente ni utilizamos, pero hay que leerlo, porque si no falla
  54.            
  55.             int packetId = readInt(in);
  56.            
  57.             if (packetId != 0x00) {
  58.                 socket.close();
  59.                
  60.                 throw new RuntimeException("PacketId incorrecto.");
  61.             }
  62.            
  63.             int strLength = readInt(in);
  64.            
  65.             if (strLength < 1) {
  66.                 socket.close();
  67.                
  68.                 throw new RuntimeException("Tamaño de string demasiado corto.");
  69.             }
  70.            
  71.             byte[] responseData = new byte[strLength];
  72.            
  73.             in.readFully(responseData);
  74.            
  75.             in.close();
  76.            
  77.             out2.close();
  78.            
  79.             socket.close();
  80.            
  81.             String json = new String(responseData, Charset.forName("utf-8"));
  82.            
  83.             JsonObject object = new JsonParser().parse(json).getAsJsonObject();
  84.            
  85.             String motd = object.get("description").getAsString();
  86.            
  87.             int players = object.get("players").getAsJsonObject().get("online").getAsInt();
  88.            
  89.             int max = object.get("players").getAsJsonObject().get("max").getAsInt();
  90.            
  91.             return new ConnectionData(Status.ONLINE, motd, players, max);
  92.         } catch (IOException ex) {
  93.             return new ConnectionData(Status.OFFLINE);
  94.         }
  95.     }
  96.    
  97.     static void writeInt(int i, DataOutputStream out) throws IOException {
  98.         while (true) {
  99.             if ((i & 0xFFFFFF80) == 0) {
  100.                 out.write(i);
  101.                
  102.                 return;
  103.             }
  104.            
  105.             out.write(i & 0x7F | 0x80);
  106.            
  107.             i >>>= 7;
  108.         }
  109.     }
  110.    
  111.     static void writeString(String str, DataOutputStream out) throws IOException {
  112.         writeInt(str.length(), out);
  113.        
  114.         out.write(str.getBytes(Charset.forName("utf-8")));
  115.     }
  116.    
  117.     static void sendPacket(byte[] data, DataOutputStream out) throws IOException {
  118.         writeInt(data.length, out);
  119.        
  120.         out.write(data);
  121.     }
  122.    
  123.     static int readInt(DataInputStream in) throws IOException {
  124.         int i = 0;
  125.        
  126.         int j = 0;
  127.        
  128.         while (true) {
  129.             int k = in.readByte();
  130.            
  131.             i |= (k & 0x7F) << j++ * 7;
  132.            
  133.             if (j > 5) {
  134.                 throw new RuntimeException("Int demasiado grande.");
  135.             }
  136.            
  137.             if ((k & 0x80) != 128) {
  138.                 break;
  139.             }
  140.         }
  141.        
  142.         return i;
  143.     }
  144.  
  145.     public static class ConnectionData {
  146.         Status status;
  147.        
  148.         String motd;
  149.        
  150.         int players, max;
  151.        
  152.         public ConnectionData(Status status) {
  153.             this.status = status;
  154.         }
  155.        
  156.         public ConnectionData(Status status, String motd, int players, int max) {
  157.             this.status = status;
  158.             this.motd = motd;
  159.             this.players = players;
  160.             this.max = max;
  161.         }
  162.        
  163.         public Status getStatus() {
  164.             return status;
  165.         }
  166.        
  167.         public String getMotd() {
  168.             return motd;
  169.         }
  170.        
  171.         public int getPlayers() {
  172.             return players;
  173.         }
  174.        
  175.         public int getMaxPlayers() {
  176.             return max;
  177.         }
  178.     }
  179.    
  180.     public enum Status {
  181.         ONLINE, OFFLINE;
  182.     }
  183. }
  184.  
  185. --------------- Ejemplo ---------------
  186.  
  187. public class Prueba {
  188.  
  189.     public static void main(String[] args) {
  190.         ConnectionData data = ServerPinger.ping("192.99.4.13", 11701);
  191.        
  192.         System.out.println(data.getMotd());
  193.         System.out.println(data.getPlayers());
  194.         System.out.println(data.getMaxPlayers());
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement