Minecraft make the player connect to the game

Asked

Viewed 221 times

0

Hello I have been trying to get the client to connect to the server with sockets this site has an explanation http://wiki.vg/How_to_Write_a_Client#Login but I can’t find a way to login already tried out.writebyte, out.writeInt, etc.. someone knows how to do what they 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");
}
  • 1

    are you sure the server server server server is turned off? besides, after sending '0x02' Voce is not waiting for a '0xFD'.

  • yes authentication is off, and I don’t quite know how packets work so I’m asking for help here in stack overflow

  • someone knows the answer?

  • 1

    I don’t quite remember how sockets work, but you didn’t need to do out.flush() to "force" the data to go? (they may be stopped on buffer...)

1 answer

0


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

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

    out.writeByte(0x02);
    

    (don’t worry about converting the int for byte, the method writeByte accepts a int even, for convenience...)

  2. After sending this byte, "download" (flush) in stream to ensure that the data really were; they may be stopped at some buffer:

    out.flush();
    

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

  3. Before sending the next byte, wait for the server to give its answer, 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’re not interested in that answer, you can ignore it, but I don’t know what the consequence would be 0xCD before obtaining it (the server may even discard this data, so it is not a good idea to send it earlier).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.