Cant query another server...

Discussion in 'Plugin Development' started by CrazyGuy3000, Jan 3, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    CrazyGuy3000

    I created this earlier and I need help fixing it,
    it will only show null in the console whenever I run it

    Game.java
    Code:java
    1. package me.Twipply.Grocery;
    2.  
    3. import org.bukkit.event.Listener;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. import me.Twipply.Grocery.Server.Connection;
    7.  
    8. public class Game extends JavaPlugin implements Listener {
    9. public static void main(String[] args){
    10. Server server = new Server("127.0.0.1", 25565);
    11. System.out.println(server.parseData(Connection.PLAYERS_ONLINE));
    12. }
    13. }
    14.  


    Server.java
    Code:java
    1. package me.Twipply.Grocery;
    2.  
    3. import java.io.DataOutputStream;
    4. import java.io.InputStream;
    5. import java.io.InputStreamReader;
    6. import java.i:confused:utputStream;
    7. import java.net.InetSocketAddress;
    8. import java.net.Socket;
    9. import java.nio.charset.Charset;
    10.  
    11. public class Server {
    12. private String host;
    13. private int port;
    14.  
    15. public Server(String host, int port){
    16. this.host = host;
    17. this.port = port;
    18. }
    19.  
    20. public Server(String host){
    21. this.host = host;
    22. this.port = 25565;
    23. }
    24.  
    25. public Server(){
    26. this.host = "127.0.0.1";
    27. this.port = 25565;
    28. }
    29.  
    30. public String parseData(Connection connection) {
    31. try {
    32. Socket socket = new Socket();
    33.  
    34. socket.setSoTimeout(2500);
    35. socket.connect(new InetSocketAddress(host, port), 2500);
    36.  
    37. os = socket.getOutputStream();
    38. dos = new DataOutputStream(os);
    39.  
    40. is = socket.getInputStream();
    41. isr = new InputStreamReader(is, Charset.forName("UTF-16BE"));
    42.  
    43. dos.write(new byte[] { (byte) 0xFE, (byte) 0x01 });
    44.  
    45. int packetID = is.read();
    46.  
    47. if(packetID == -1)
    48. System.out.println("EOS");
    49.  
    50. if(packetID == 0xFF)
    51. System.out.println("Invalid Packet ID" + packetID);
    52.  
    53. int length = isr.read();
    54.  
    55. if(length == -1)
    56. System.out.println("EOS");
    57.  
    58. if(length == 0)
    59. System.out.println("Invalid Length");
    60.  
    61. char[] chars = new char[length];
    62.  
    63. if(isr.read(chars, 0, length) != length)
    64. System.out.println("EOS");
    65.  
    66. String string = new String(chars);
    67. String[] data = string.split("\0");
    68.  
    69. if(connection == Connection.PLAYERS_ONLINE){
    70. return Integer.parseInt(data[4])+"/"+(data[5]);
    71. } else if(connection == Connection.MOTD){
    72. return data[3];
    73. } else if(connection == Connection.SERVER_VERSION){
    74. return data[2];
    75. } else {
    76. System.out.println("Error!");
    77. }
    78. }catch(Exception e){
    79. return null;
    80. }
    81. return host;
    82. }
    83.  
    84. public enum Connection {
    85. PLAYERS_ONLINE,SERVER_VERSION,MOTD;
    86. }
    87. }
    88.  
     
  2. Offline

    CrazyGuy3000

  3. Offline

    kumpelblase2

    Because the port you're trying to bind is already used by the bukkit server, if you didn't change anything.
     
  4. Offline

    CrazyGuy3000

  5. Offline

    BillyGalbreath

  6. Offline

    Jalau

    BillyGalbreath The problem is in 1.7 most of the server packets changed so i think that needs to be updatet?!
     
Thread Status:
Not open for further replies.

Share This Page