Bittorrent UDP tracker protocol JAVA test 时间: 2021-04-16 13:04 分类: JAVA ``` InetSocketAddress server_addr = new InetSocketAddress(InetAddress.getByName("tracker.torrent.eu.org").getHostAddress(), 451); DatagramSocket socket = new DatagramSocket(); //connect String hexData = "00000417271019800000000000000001"; byte[] buf = Util.convertHexStringToByteNoSpace(hexData); DatagramPacket sendPacket = new DatagramPacket(buf, 0, buf.length, server_addr); socket.send(sendPacket); byte[] recvBuf = new byte[2048]; DatagramPacket recvPacket = new DatagramPacket(recvBuf, 2048); socket.receive(recvPacket); System.out.println(Util.convertByteToHexStringWithoutSpace(recvPacket.getData())); byte[] connection_id = new byte[8]; System.arraycopy(recvPacket.getData(), 8, connection_id, 0, 8); System.out.println("connection_id:" + Util.convertByteToHexStringWithoutSpace(connection_id)); //announce ByteBuffer byteBuffer = ByteBuffer.allocate(98); byteBuffer.put(connection_id); //connection id byteBuffer.putInt(1); //action: announce byteBuffer.putInt(2); //transaction id(Randomized by client) byteBuffer.put(Util.convertHexStringToByteNoSpace("81eecacb7dca9fcd8dbfd4baf9c92de27d0e4c20")); //info hash byteBuffer.put(Util.convertHexStringToByteNoSpace("81eecacb7dca9fcd8dbfd4baf9c92de27d0e4c20")); //peer id byteBuffer.putLong(0); //downloaded byteBuffer.putLong(1111110); //left byteBuffer.putLong(0); //uploaded byteBuffer.putInt(0); byteBuffer.putInt(0); byteBuffer.putInt(520); byteBuffer.putInt(200); byteBuffer.putShort((short) 6889); sendPacket = new DatagramPacket(byteBuffer.array(), 0, 98, server_addr); socket.send(sendPacket); socket.receive(recvPacket); System.out.println(Util.convertByteToHexStringWithoutSpace(recvPacket.getData())); byteBuffer = ByteBuffer.wrap(recvPacket.getData()); byteBuffer.getInt(); byteBuffer.getInt(); byteBuffer.getInt(); byteBuffer.getInt(); int peers = byteBuffer.getInt(); for (int i = 0; i < 200; i++) { String ip = Util.longToIp(byteBuffer.getInt()); byte[] port = new byte[2]; byteBuffer.get(port); System.out.println("peer " + (i + 1) + ": " + ip + ":" + ((0x0000FF00 & (port[0] << 8)) | (0x000000FF & port[1]))); } ``` 参考:[https://libtorrent.org/udp_tracker_protocol.html]() 标签: 无