0

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...

Bjornke
  • 3
  • 2
  • Can you post an [SSCCE](http://sscce.org) that exhibits the behavior you're talking about? – obataku Aug 26 '12 at 05:10
  • Got to have some humor mate :P – Bjornke Aug 26 '12 at 05:21
  • `DatagramPacket.getAddress` shouldn't be doing a DNS lookup... but anyways, have you tried `send.setSocketAddress(packet.getSocketAddress());`? This should cover both the IP and port issue. – obataku Aug 26 '12 at 05:49
  • Does setAddress() do a DNS lookup by any chance? My money is that it doesn't. SocketAddress that's what I was using before, thought that was the issue. – Bjornke Aug 26 '12 at 05:58
  • Voted to close as "Too localized". The OP's latest comments make it clear that the real problem was unrelated to the code in the question. The Q&A is therefore of no help to future readers. – Stephen C Aug 26 '12 at 08:03

1 Answers1

0

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.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • You're probably correct about this as doing a reverse DNS look up on both the linux box and the windows server take respectively 6 minutes on linux and 8 on windows. (Different DNS and ISP providers by the way) – Bjornke Aug 26 '12 at 05:15
  • @Bjornke try adding an entry in your local hosts file for that IP so the lookup happens quickly. – Jon Lin Aug 26 '12 at 05:16
  • that would be nice however the server handles requests from multiple IPs at the same time, and over the course of a day will probably go through 200 or so requests. Is there a possible way of disabling the DNS look up on the getAddress() method? Would be nice if I could just take the IP in string or byte form and use it without waiting for a middleman DNS. – Bjornke Aug 26 '12 at 05:20
  • @Bjornke I don't know of a way to make java not do any reverse lookups, maybe there's a question on SO about that. I guess you could try disabling DNS lookups entirely on the server by removing the DNS server entries in your networking or /etc/resolv.conf. Or setup a local instance of BIND where all forward lookups resolve fine, but reverse lookups all return "rev-ip" or something – Jon Lin Aug 26 '12 at 05:32
  • turns out it was caused not by the getAddress() or setAddres() but rather by a race condition from threads in the genKey() method not posted. – Bjornke Aug 26 '12 at 06:21