i am trying to fetch server's info by sending packets and fetching, but it doesn't seem to work, using --> Code: public void send() { Runnable myRunnable = new Runnable() { @Override public void run(){ DatagramSocket socket; String serverMotd = "MCCPP;Demo;Test"; try { socket = new DatagramSocket(19132); socket.setBroadcast(true); } catch (Exception e) { e.printStackTrace(); return; } while (true) { try { InetAddress address = InetAddress.getByName("play.lbsg.net"); byte[] buf = Util.hexStringToByteArray("01" + "000000001462D2EC" + "00FFFF00FEFEFEFEFDFDFDFD12345678"); DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 19132); socket.send(packet); packet = new DatagramPacket(buf, buf.length); socket.receive(packet); System.out.println("Recieved a packet from " + packet.getAddress().toString()); byte[] returnBuf = buildReturnPacket(buf, serverMotd); DatagramPacket pongpacket = new DatagramPacket(returnBuf, returnBuf.length, packet.getAddress(), 19132); DatagramSocket sendSocket = new DatagramSocket(); sendSocket.send(pongpacket); sendSocket.close(); System.out.println("Sent packet in response"); Thread.sleep(500); } catch (Exception e) { e.printStackTrace(); } } } }; Thread myThread = new Thread(myRunnable); myThread.start(); } private byte[] buildReturnPacket(byte[] pingBuffer, String serverMotd) throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(bout); out.write(0x1c); out.write(pingBuffer, 1, 8); out.write(new byte[8], 0, 8); writeMagic(out); out.writeUTF(serverMotd); return bout.toByteArray(); } private static void writeMagic(DataOutputStream out) throws IOException { out.write(0); out.write(0xff);out.write(0xff); out.write(0); out.writeInt(0xfefefefe); out.writeInt(0xfdfdfdfd); out.writeInt(0x12345678); } This sends and recieves packets but how do i get server info like player count, motd etc.?
The output i get from Code: new String(packet.getData(), 0, packet.getLength()); why the unknow characters? @SOFe
Because it is binary. In Java, String means a human-readable string, unlike in PHP where it is just an array of bytes. You should directly handle the byte array from packet.getData() (and if you want to print it, print it byte by byte, or just use Hex.encode(byte[]) from Apache commons codec.
i get this when i convert it to hex -> 1C000000001462D2EC100547390000000000FFFF00FEFEFEFE Code: public static String bytesToHex(byte[] bytes) { final char[] hexArray = "0123456789ABCDEF".toCharArray(); char[] hexChars = new char[bytes.length * 2]; for ( int j = 0; j < bytes.length; j++ ) { int v = bytes[j] & 0xFF; hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2 + 1] = hexArray[v & 0x0F]; } return new String(hexChars); }
You constructed the receive packet incorrectly. You are receiving data in these two lines: Why did you pass buf? buf is the byte[] data you sent. Why reuse it in receive? Indeed, you can reuse it, but the problem is that buf is only 25 bytes long, so using it to construct the receive DatagramPacket, you only read 25 bytes from the server. If you use a bigger byte array (e.g. 1024), you can receive more data. According to @jasonwynn10's libpmquery, a maximum of 4096 bytes is expected, and you may start reading from byte 35. https://github.com/jasonwynn10/libpmquery/blob/master/src/libpmquery/PMQuery.php?utf8=✓#L42
Holy, it worked!!111! thanks @SOFe youre a legenddddddd ilyyy, didnt need to covert to hex btw, packet->getData() in string worked
The problem is that it is not a string at all. Your code shouldn't involve any String objects until you have gotten rid of the first 35 bytes of handshake data.