0

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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Geekles
  • 37
  • 7
  • How do you this game is using UDP? – markspace Mar 07 '18 at 16:47
  • Actually, I'm not sure. I'm willing to try UDP first and see with which games it may work and later devote a class towards listening for TCP packets. – Geekles Mar 07 '18 at 16:49
  • 1
    So you're opening a port at random and are hoping that the server sends data there? Isn't that a bit optimistic? – Kayaman Mar 07 '18 at 16:52
  • I suppose my plan was to first be able to test to see if I could read packets by creating an application on my Linux server to send a packet to my client, and hopefully later then iterate through different ports until I'm able to read a packet on specific port while actively playing a game to see what port it's sending the packets to. I suppose I was being a bit optimistic but I'm aware of how this may not work. – Geekles Mar 07 '18 at 16:59

1 Answers1

2

What you are trying to do is usually done with tcpdump or Wireshark. Both are tools to capture network packets as they pass the wire. If you want capture from Java there is e.g. the library pcap4j. Pcap is one common file format for captured packets.

Your code above does not work, because it tries to receive the incoming UPD packages instead of your game. I assume that this would break the communication of the local game software and its server component. The above mentioned capturing tools don't interfere with the usual transmission of the packets.

jmg
  • 7,308
  • 1
  • 18
  • 22