I'm curious as to how I would listen to all incoming packets being sent from a server to my client. My goal is to be able to create a bot that would be capable of learning how to play a basic game online. I originally had my application take a screenshot, crop it, and check for a certain range of colors within the image to locate where certain objects may be based on the appropriate pixels' location, however this isn't an ideal way to go about creating an efficient bot, so I decided I'd try to figure out how to directly read information being sent from the game's server and my client to understand where the location of certain objects are.
Perhaps there's something I don't understand however I'm wondering if there's a way to read packets that are sent from a server to my localhost IP. I attempted to listen to packets by using the following code and fiddling around with it a bit:
public class Receiver {
public static void main(String[] args) {
int port = args.length == 0 ? 57 : Integer.parseInt(args[0]);
new Receiver().run(port);
}
public void run(int port) {
try {
DatagramSocket serverSocket = new DatagramSocket(port);
byte[] receiveData = new byte[8];
System.out.printf("Listening on udp:%s:%d%n",
InetAddress.getLocalHost().getHostAddress(), port);
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
while(true)
{
serverSocket.receive(receivePacket);
String sentence = new String( receivePacket.getData(), 0,
receivePacket.getLength() );
System.out.println("RECEIVED: " + sentence);
// now send acknowledgement packet back to sender
InetAddress IPAddress = receivePacket.getAddress();
String sendString = "polo";
byte[] sendData = sendString.getBytes("UTF-8");
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
IPAddress, receivePacket.getPort());
serverSocket.send(sendPacket);
}
} catch (IOException e) {
System.out.println(e);
}
// should close serverSocket in finally block
}
}
Credit: sending and receiving UDP packets using Java?
I haven't yet been able to read any incoming UDP packet. I've tried listening for packets on several different ports, however I'm not sure on how I would even test to see if my application is working without setting up a sever application to specifically send a packet to the specific port I'm listening to). I'm not entirely sure where to start with this personal project of mine, however any help/guidance would be most appreciated.