-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARPClient.java
More file actions
28 lines (23 loc) · 990 Bytes
/
ARPClient.java
File metadata and controls
28 lines (23 loc) · 990 Bytes
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
import java.net.*;
import java.util.Scanner;
public class ARPClient {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
InetAddress serverAddr = InetAddress.getByName("localhost");
int serverPort = 9091;
Scanner sc = new Scanner(System.in);
System.out.print("Enter IP to resolve (e.g., 192.168.1.1): ");
String ip = sc.nextLine();
// Send ARP request as "REQUEST:<IP>"
byte[] request = ("REQUEST:" + ip).getBytes();
ds.send(new DatagramPacket(request, request.length, serverAddr, serverPort));
System.out.println("Sent ARP Request for " + ip);
// Receive reply
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String reply = new String(dp.getData(), 0, dp.getLength());
System.out.println("Received ARP Reply: " + reply);
ds.close();
}
}