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");
}