Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. package ServerPing;
  2.  
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.net.InetSocketAddress;
  9. import java.net.Socket;
  10. import java.net.SocketException;
  11. import java.nio.charset.Charset;
  12.  
  13. public class Server {
  14.  
  15. private static String address;
  16. private static int port;
  17. private static int timeout;
  18.  
  19. private static boolean online;
  20. private static int playercount;
  21. private static int maxplayers;
  22. private static String motd;
  23.  
  24.  
  25. public Server(String address, int port, int timeout){
  26.  
  27. Server.address = address;
  28. Server.port = port;
  29. Server.timeout = timeout;
  30.  
  31. }
  32.  
  33.  
  34. public static String getAddress(){
  35. return Server.address;
  36. }
  37. public static void setAddress(String address){
  38. Server.address = address;
  39. }
  40.  
  41. public static int getPort(){
  42. return Server.port;
  43. }
  44. public static void setPort(int port){
  45. Server.port = port;
  46. }
  47.  
  48. public static int getTimeOut(){
  49. return Server.timeout;
  50. }
  51. public static void setTimeOut(int timeout){
  52. Server.timeout = timeout;
  53. }
  54.  
  55. public static boolean isOnline(){
  56. return Server.online;
  57. }
  58. public static void setOnline(boolean online){
  59. Server.online = online;
  60. }
  61.  
  62. public static int getPlayerCount(){
  63. return Server.playercount;
  64. }
  65. public static void setPlayerCount(int playercount){
  66. Server.playercount = playercount;
  67. }
  68.  
  69. public static int getMaxPlayers(){
  70. return Server.maxplayers;
  71. }
  72. public static void setMaxPlayers(int maxplayers){
  73. Server.maxplayers = maxplayers;
  74. }
  75.  
  76. public static String getMotd(){
  77. return Server.motd;
  78. }
  79. public static void setMotd(String motd){
  80. Server.motd = motd;
  81. }
  82.  
  83. @SuppressWarnings("resource")
  84. public static void ping(){
  85. try{
  86. Socket socket = new Socket();
  87. OutputStream outputStream;
  88. DataOutputStream dataOutputStream;
  89. InputStream inputStream;
  90. InputStreamReader inputStreamReader;
  91. socket.setSoTimeout(Server.timeout);
  92. socket.connect(new InetSocketAddress(Server.getAddress(), Server.getPort()), Server.getTimeOut());
  93. outputStream = socket.getOutputStream();
  94. dataOutputStream = new DataOutputStream(outputStream);
  95. inputStream = socket.getInputStream();
  96. inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-16BE"));
  97. dataOutputStream.write(new byte[]{(byte) 0xFE, (byte) 0x01});
  98. int packetID = inputStream.read();
  99. if(packetID == -1){
  100. dataOutputStream.close();
  101. outputStream.close();
  102. inputStream.close();
  103. inputStreamReader.close();
  104. socket.close();
  105. throw new IOException("Premature end of stream.");
  106. }
  107. if(packetID != 0xFF){
  108. dataOutputStream.close();
  109. outputStream.close();
  110. inputStreamReader.close();
  111. inputStream.close();
  112. throw new IOException("Invalid packet ID (" + packetID + ").");
  113. }
  114. int length = inputStreamReader.read();
  115. if(length == -1){
  116. dataOutputStream.close();
  117. outputStream.close();
  118. inputStreamReader.close();
  119. inputStream.close();
  120. socket.close();
  121. throw new IOException("Premature end of stream.");
  122. }
  123. if(length == 0){
  124. dataOutputStream.close();
  125. outputStream.close();
  126. inputStream.close();
  127. inputStreamReader.close();
  128. socket.close();
  129. throw new IOException("Invalid string length.");
  130. }
  131. char[] chars = new char[length];
  132. if(inputStreamReader.read(chars, 0, length) != length){
  133. dataOutputStream.close();
  134. outputStream.close();
  135. inputStream.close();
  136. inputStreamReader.close();
  137. socket.close();
  138. throw new IOException("Premature end of stream.");
  139. }
  140. String string = new String(chars);
  141. if(string.startsWith("§")){
  142. String[] data = string.split("\0");
  143. Server.setMotd(data[3]);
  144. Server.setPlayerCount(Integer.parseInt(data[4]));
  145. Server.setMaxPlayers(Integer.parseInt(data[5]));
  146.  
  147. }else{
  148. String[] data = string.split("§");
  149. Server.setMotd(data[0]);
  150. Server.setPlayerCount(Integer.parseInt(data[1]));
  151. Server.setMaxPlayers(Integer.parseInt(data[2]));
  152. }
  153.  
  154. dataOutputStream.close();
  155. outputStream.close();
  156. inputStream.close();
  157. inputStreamReader.close();
  158. socket.close();
  159.  
  160. }catch(SocketException e){
  161. Server.setOnline(false);
  162. }catch(IOException e){
  163. Server.setOnline(false);
  164. }
  165.  
  166. }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement