Reverse Address Resolution Protocol (RARP) in Java

Java Program to implement Reverse Address Resolution Protocol

Source code :

// RARP.java - Client

import java.io.*;
import java.net.*;
import java.util.*;

public class RARP {
    public static void main(String args[]){
    try{
        DatagramSocket client = new DatagramSocket();
        InetAddress addr = InetAddress.getByName("127.0.0.1");
        byte[] sendByte = new byte[1204];
        byte[] receiveByte = new byte[1024];
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the Physical Address ");
        String str = in.readLine();
        sendByte = str.getBytes();
        DatagramPacket sender = new DatagramPacket(sendByte,sendByte.length,addr,1309);
        client.send(sender);
        DatagramPacket receiver = new DatagramPacket(receiveByte,receiveByte.length);
        client.receive(receiver);
        String s = new String(receiver.getData());
        System.out.println("The Logical Address is :" + s.trim());
        client.close();
    }
    catch(Exception e){
    System.out.println(e);
    }
    }
}

// RARPServer.java - Server

import java.io.*;
import java.net.*;
import java.util.*;

public class RARPServer{

    public static void main(String args[])    {
    try{
    
    DatagramSocket server = new DatagramSocket(1309);
        while(true){
                byte[] sendByte = new byte[1204];
                byte[] receiveByte = new byte[1204];
                DatagramPacket receiver = new DatagramPacket(receiveByte,receiveByte.length);
                server.receive(receiver);
                String str = new String(receiver.getData());
                String s = str.trim();
                InetAddress addr = receiver.getAddress();
                int port = receiver.getPort();
                String ip[] = {"10.0.3.186"};
                String mac[] = {"D4:3D:7E:12:A3:D9"};
                
                    for (int i = 0; i < ip.length; i++) {
                    if(s.equals(mac[i]))
                    {
                    sendByte = ip[i].getBytes();
                    DatagramPacket sender = new DatagramPacket(sendByte,sendByte.length,addr,port);
                    server.send(sender);
                    break;
                    }            
            }
            break;
        }        
    }catch(Exception e)
    {
        System.out.println(e);
    }
    }
}

Output :

Enter the Physical Address :  D4:3D:7E:12:A3:D9
The Logical Address is : 10.0.3.186

Notes :

Save these two programs as individual files based on their class name. First run the Server then the Client.

Note : Here we have virtually implemented the RARP Server.


Output :


                

Comments :