Search code examples
javadatagram

Function hangs? on packet.setAddress()


Ok so the program is designed to take in connections, validate them, and resend that validation code. Before anyone get's angry it's just a simple little project and is not designed to be overly complex haha. However for some very strange reason the function is hanging on send.setAddess(packet.getAddress); I know this because I have commented out each individual line of code that deals with the Datagram packet "send" and have found that it "hangs" (or never progresses forward in the method again) on that particular line. Any thoughts? Am I doing something cluelessly wrong? I tried it on a linux server as well to make sure it didn't have anything to do with me and the same crap happened.

    public static boolean authorize(String n, DatagramPacket packet) {
    DatagramPacket send = new DatagramPacket(new byte[4096], 4096);
    try {
      System.out.println("in auth");
      String[] t1 = n.split("%@");
      String name = t1[1];
      int k = genKey(name);
      clients.put(name, k);
      send.setAddress(packet.getAddress());
      System.out.println("set add");
      send.setPort(packet.getPort());
      System.out.println("set port");
      send.setData(("l-succeed%@" + Integer.toString(k)).getBytes());
      System.out.println("set data");
      main.dispathcer(send);
      System.out.println("called send");
      return true;
    } catch(Exception e) {
      send.setData("l-failed".getBytes());
      main.dispathcer(send);
      return false;
    }
  }

EDIT: it took 6 minutes before the authorization token was received by the client. So obviously the setAddress() works but is taking far too long...


Solution

  • It's possible that the process is hanging because there's an issue doing DNS resolution on the address for packet when you call .getAddress(). A few DNS calls are made in order to create the InetAddress object. On these machines, are you able to do a reverse DNS lookup on the IP that the packet packet came from? Try setting an entry for this IP in your /etc/hosts file.