Advertisement
iPeer

Untitled

May 12th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1.  
  2. package com.ipeer.minecraft.servers;
  3.  
  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;
  7. import java.net.InetSocketAddress;
  8. import java.net.Socket;
  9. import java.net.UnknownHostException;
  10.  
  11. import com.ipeer.ytua.engine.Engine;
  12.  
  13. public class MCServer {
  14.  
  15.     public static void main(String[] args) {
  16.         try {
  17.             pollServer(null, 0, "127.0.0.1", 7778, null);
  18.         }
  19.         catch (Exception e) {
  20.             e.printStackTrace();
  21.         }
  22.     }
  23.    
  24.     public static void pollServer(String channel, int mode, String addr, int port, Engine engine) throws IOException {
  25.         long ping1 = System.nanoTime();
  26.         System.out.println("Polling "+addr+":"+port);
  27.         Socket s = null;
  28.         DataInputStream in = null;
  29.         DataOutputStream out = null;
  30.         try {
  31.             s = new Socket();
  32.             s.setSoTimeout(3000);
  33.             s.setTcpNoDelay(true);
  34.             s.setTrafficClass(18);
  35.             s.connect(new InetSocketAddress(addr, port));
  36.             in = new DataInputStream(s.getInputStream());
  37.             out = new DataOutputStream(s.getOutputStream());
  38.             out.write(254);
  39.            
  40.             if (in.read() != 255) { // Bad response
  41.                 if (channel != null)
  42.                     if (mode == 1)
  43.                         engine.msg(channel, "ERROR: Bad response");
  44.                     else     
  45.                         engine.notice(channel, "ERROR: Bad response");
  46.                 System.out.println("Bad response!");
  47.                 return;
  48.             }
  49.            
  50.             String data = Packet.readLine(in, 256);
  51.             char[] chars = data.toCharArray();
  52.             data = new String(chars);
  53.             String[] data1 = data.split("\247");
  54.             String motd = data1[0];
  55.             int playerCount = Integer.parseInt(data1[1]);
  56.             int maxPlayers = Integer.parseInt(data1[2]);
  57.             char c = Engine.colour;
  58.             long ping2 = System.nanoTime();
  59.             long ping = (ping2 - ping1) / 0xf4240L;
  60.             String out1 = c+"14["+c+"13"+addr+c+"14 ("+c+"13"+port+c+"14)] MOTD: "+c+"13"+motd+c+"14 Players: "+c+"13"+playerCount+c+"14/"+c+"13"+maxPlayers+c+"14 Ping: "+c+"13"+ping+"ms";
  61.             if (channel != null)
  62.                 if (mode == 1)
  63.                     engine.msg(channel, out1);
  64.                 else     
  65.                     engine.notice(channel, out1);
  66.            
  67.         }
  68.         catch (Exception e) {
  69.             String out1 = "Unable to connect to server "+addr+":"+port+": "+e.getMessage();
  70.             if (e instanceof UnknownHostException)
  71.                 out1 = "Unable to connect to server "+addr+":"+port+": Unknown host \""+addr+"\"";
  72.             if (channel != null)
  73.                 if (mode == 1)
  74.                     engine.msg(channel, out1);
  75.                 else     
  76.                     engine.notice(channel, out1);
  77.             System.out.println(out1);
  78.         }
  79.         finally {
  80.             if (in != null)
  81.                 in.close();
  82.             if (out != null)
  83.                 out.close();
  84.             if (s != null)
  85.                 s.close();
  86.         }
  87.        
  88.     }
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement