Connecting to a minecraft server

Discussion in 'Plugin Development' started by V10lator, Mar 2, 2012.

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

    V10lator

    I know this is a pretty advanced question, but I hope somebody is able to answer it:
    This is my code:
    Code:java
    1.  
    2. ServerConnector(String[] names)
    3. {
    4. this.name = name;
    5. longName = name+";"+MainClass.server+":"+MainClass.port;
    6. start();
    7. }
    8. ...
    9. Socket s = new Socket();
    10. try
    11. {
    12. s.connect(new InetSocketAddress(MainClass.server, MainClass.port));
    13. DataInputStream in = new DataInputStream(s.getInputStream());
    14. DataOutputStream out = new DataOutputStream(s.getOutputStream());
    15. out.writeByte(0x02);
    16. out.writeUTF(longName);
    17. out.flush();
    18. if(in.readByte() != 0x02)
    19. throw new Exception("Wrong packet received!");
    20. in.readUTF();
    21. System.out.println("handshake!");

    But all that happens is that the server closes the connection cause it reaches the end of the stream after some time. Of course I'm never able to read "handshake!". :(
     
    Creeper96 likes this.
  2. Offline

    Lolmewn Retired Staff

    does the in.readUTF() at line 20 do anything? Otherwise test it without it.
     
  3. Offline

    V10lator

    Lolmewn Yes, it reads out some not needed hash. But that's not the point, cause it never reaches the point. The code causes an exception at line 18 cause the server closes the connection.
    The server tells me it reached the end of the stream (after some time, so I think there's something missing at my packet, but I don't know what).
     
  4. Offline

    Lolmewn Retired Staff

    Hmm..
    I mostly do while(!in.ready()){} to make sure the inputstream is ready to be read.
     
  5. Offline

    V10lator

    Lolmewn not needed as read*() simply freezes the task if there's nothing to read. Also, again: Not the problem here as the problem appears before I read anything from the server.
     
  6. Offline

    Lolmewn Retired Staff

    Hmm.. that's weird. Just interested, but what does the 0x02 byte mean?
     
  7. Offline

    V10lator

  8. Offline

    Lolmewn Retired Staff

    V10lator I see. I'm sorry to say this, but I've got no clue why it's not working. Maybe epic ppl like Dinnerbone know this, but I can't help you.
     
  9. Offline

    ferrybig

    its not writeUTF() for an minecraft connection, from what I learned, try this:
    Code:java
    1. public void writeString(String msg) throws IOException
    2. {
    3. out.writeShort(msg.length());
    4. out.writeChars(msg);
    5. }
     
  10. Offline

    V10lator

    ferrybig Also wrong, it has to be UTF-16. ;)

    out.writeShort(msg.length());
    out.write(msg.getBytes(Charset.forName("UTF-16"));

    works.
     
  11. Offline

    nisovin

  12. Offline

    V10lator

    nisovin Yes, it's fixed. The Charset has to be UTF-16BE

    //EDIT: And the length, sorry, a bit tired atm. ;)
     
  13. Offline

    ferrybig

    I used the way I showed to create an custom server, so how did it worked then?
     
  14. Offline

    V10lator

    Well, UTF-16BE works for me. Maybe both methods work, but I don't want to test that. :)
     
Thread Status:
Not open for further replies.

Share This Page