title: Java send udp packet to dns server
body: <p>I am trying to send udp packet to specified dns server and receive a respone from dns server. </p>
  
  <p>The problems is that I don't dont get the response from the server. I trying to receive a packet with my client socket but don't have any success.</p>
  
  <p>Looking for help to fix this problem.</p>
  
  <p>This is my UdpClient.java</p>
  
  <pre><code>import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  import java.net.InetSocketAddress;
  import java.nio.charset.Charset;
  import java.nio.charset.StandardCharsets;
  import java.util.Scanner;
  
  class UdpClient
  {
      public static void main (String args[]) throws Exception
      {
          Scanner scan = new Scanner(System.in);
          System.out.print("Enter a hostname: ");
          // host name
          String hostname = scan.next();
          // socket
          DatagramSocket clientSocket = new DatagramSocket();
          InetAddress ipAddress = InetAddress.getByName(hostname);
          String message = "0471001000000";
          byte[] data = message.getBytes(StandardCharsets.UTF_8);
  
          // datagram packet
          DatagramPacket sendPacket = new DatagramPacket(data, data.length, ipAddress, 53);
          // send the packet
          clientSocket.send(sendPacket);
          // set a receive timeout, 2000 milliseconds
          // packet for receive
          byte[] rdata = new byte[1024];
          DatagramPacket receivePacket = new DatagramPacket(rdata, rdata.length, ipAddress, 53);
          // wait for a response from the server
          clientSocket.receive(receivePacket);
          System.out.println("Host Name: " + ipAddress.getHostName());
          System.out.println("Canonical Host Name: " + ipAddress.getCanonicalHostName());
          System.out.println("Local Host: " + ipAddress.getLocalHost());
          System.out.println("Loopback Address: " + ipAddress.getLoopbackAddress());
          System.out.println("IPv4: " + ipAddress.getHostAddress());
          String serverMessage = new String(receivePacket.getData(), 0, receivePacket.getLength());
          System.out.println("FROM SERVER: " + serverMessage);
      }
  }
  </code></pre>