-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARPServer.java
More file actions
30 lines (26 loc) · 1.18 KB
/
ARPServer.java
File metadata and controls
30 lines (26 loc) · 1.18 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
import java.net.*;
import java.util.HashMap;
public class ARPServer {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket(9091);
HashMap<String, String> arpTable = new HashMap<>();
// Sample ARP Cache: IP -> MAC
arpTable.put("192.168.1.1", "00:11:22:33:44:55");
arpTable.put("192.168.1.2", "AA:BB:CC:DD:EE:FF");
arpTable.put("192.168.1.3", "11:22:33:44:55:66");
System.out.println("ARP Server running...");
while (true) {
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
ds.receive(dp);
String request = new String(dp.getData(), 0, dp.getLength());
if (request.startsWith("REQUEST:")) {
String ip = request.substring(8);
String mac = arpTable.getOrDefault(ip, "Not Found");
byte[] reply = ("REPLY: MAC for " + ip + " is " + mac).getBytes();
ds.send(new DatagramPacket(reply, reply.length, dp.getAddress(), dp.getPort()));
System.out.println("Sent reply for " + ip + ": " + mac);
}
}
}
}