Minecraft make the player connect to the game

0

Hello I have tried to make the client connect to the server with sockets this site has an link explanation but I can not find a way I already tried out.writebyte , out.writeInt , etc. .. does anyone know how to do this that you are explaining?

I have this code so far:

public static void main(String[] args){
    new Main();
}

public Main(){
    try {
        InetSocketAddress address = new InetSocketAddress("localhost", 25565);
        Socket socket = new Socket();
        socket.connect(address, 1000);
        DataInputStream in = new DataInputStream(socket.getInputStream());
        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
        out.writeUTF("0x02");
        out.writeUTF("0xCD");
        //..... nao faz nada

    } catch (Exception e) {
        e.printStackTrace();
    }       
}

EDITED:

public static void main(String args[]) throws IOException {
    InetSocketAddress address = new InetSocketAddress("localhost", 25565);
    System.out.println("Creating socket to '" + "localhost" + "' on port " + "25565");
    Socket socket = new Socket();
    socket.connect(address, 2000);
    DataInputStream in = new DataInputStream(socket.getInputStream());
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());

    out.writeByte(0x02);
    out.flush();

    System.out.println(in.read()); //read int, repost é -1

    byte answer = in.readByte(); //erro, aqui diz java.io.EOFException
    if (answer != 0xFD) {
        System.out.println("server says:" + in.readByte());
    }

    System.out.println("Successfully sent 0x02 & received 0xFD");

    out.writeByte(0xCD);
    out.flush();    
    System.out.println("Connection was completed successfully");
}
    
asked by anonymous 05.04.2015 / 03:21

1 answer

0

I see at least 3 [potential] problems with your code:

  • When the linked documentation says to "send a 0x02", it probably means the byte 0x02 , not the string "0x02" . Change your code to:

    out.writeByte(0x02);
    

    (Do not worry about converting int to byte , the writeByte accepts int itself, for convenience ...)

  • After sending this byte, flush in the stream to ensure that the data actually was; they may be stuck in some buffer :

    out.flush();
    

    (I'm not sure if this step is even necessary with the InputStream returned by a Socket , but in the absence of information to the contrary I would say yes.)

  • Before sending the next byte, wait for the server to give you its response, such as pointed out by Olimon F. in the comments :

    byte resposta = in.readByte();
    if ( resposta != 0xFD ) {
        // O servidor retornou algo inesperado, faça alguma coisa a respeito
    }
    
    out.writeByte(0xCD);
    ...
    

    If you are not interested in this answer, you can ignore it, but I do not know what would be the consequence of sending 0xCD before getting it (the server can even discard this data, so it is not good idea to send it earlier).

  • 06.04.2015 / 01:02