0% found this document useful (0 votes)
9 views1 page

Program 6 Server

The document contains a Java program that implements a simple server using DatagramSocket to handle RARP (Reverse Address Resolution Protocol) requests. It listens for incoming MAC addresses and responds with the corresponding IP address from a predefined list. The server processes one request and then exits the loop before closing the socket.

Uploaded by

medeepthisurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Program 6 Server

The document contains a Java program that implements a simple server using DatagramSocket to handle RARP (Reverse Address Resolution Protocol) requests. It listens for incoming MAC addresses and responds with the corresponding IP address from a predefined list. The server processes one request and then exits the loop before closing the socket.

Uploaded by

medeepthisurya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.io.

*;
import java.net.*;

class ServerRARP {
public static void main(String args[]) {
try {
DatagramSocket server = new DatagramSocket(1309);

while (true) {
byte[] sendByte = new byte[1024];
byte[] receiveByte = new byte[1024];

DatagramPacket receiver = new DatagramPacket(receiveByte,


receiveByte.length);
server.receive(receiver);

String str = new String(receiver.getData(), 0,


receiver.getLength());
String macAddress = str.trim();
InetAddress addr = receiver.getAddress();
int port = receiver.getPort();

// Mapping MAC addresses to IP addresses


String[] ip = {"165.165.80.80", "165.165.79.1"};
String[] mac = {"6A:08:AA:C2", "8A:BC:E3:FA"};

// Check if MAC address matches and send corresponding IP address


for (int i = 0; i < ip.length; i++) {
if (macAddress.equals(mac[i])) {
sendByte = ip[i].getBytes();
DatagramPacket sender = new DatagramPacket(sendByte,
sendByte.length, addr, port);
server.send(sender);
break;
}
}
break; // Exit the loop after handling one request
}

server.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

You might also like