-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDNSClient.java
More file actions
37 lines (27 loc) · 1.23 KB
/
Copy pathDNSClient.java
File metadata and controls
37 lines (27 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class DNSClient {
public static void main(String[] args) {
try {
DatagramSocket socket = new DatagramSocket();
InetAddress serverAddress = InetAddress.getByName("localhost");
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter domain name or URL: ");
String domainName = userInput.readLine().trim();
byte[] sendData = domainName.getBytes();
byte[] receiveData = new byte[1024];
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, 9876);
socket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket);
String ipAddress = new String(receivePacket.getData(), 0, receivePacket.getLength());
System.out.println("IP Address: " + ipAddress);
socket.close();
} catch (Exception e) {
System.out.println(e);
}
}
}