Search This Blog

If you like any posts in this blog then click this icon which is present under the post

Wednesday, 27 July 2011

Program for Address Resolutuion Protocol (ARP) using UDP


Client: 
import java.io.*;
import java.net.*;
import java.util.*;
class Clientarp12
{
            public static void main(String args[])
            {
            try
            {
                        DatagramSocket client=new DatagramSocket();
                        InetAddress addr=InetAddress.getByName("127.0.0.1");

                        byte[] sendbyte=new byte[1024];
                        byte[] receivebyte=new byte[1024];
                        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("Enter the logical address (IP):");
                        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 Physical Address is(MAC): "+s.trim());     
                        client.close();
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}

Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverarp12
{
            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());
                                    String s=str.trim();
                                    //System.out.println(s);
                                    InetAddress addr=receiver.getAddress();
                                    int port=receiver.getPort();
                                    String ip[]={"165.165.80.80","165.165.79.1"};
                                    String mac[]={"6A:08:AA:C2","8A:BC:E3:FA"};
                                    for(int i=0;i<ip.length;i++)
                                    {
                                                if(s.equals(ip[i]))
                                                {
                                                            sendbyte=mac[i].getBytes();
                                                            DatagramPacket sender=new DatagramPacket(sendbyte,sendbyte.length,addr,port);
                                                            server.send(sender);
                                                            break;
                                                }
                                    }
                                    break;


                        }
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}
Output:
E:\networks>java Serverarp12
E:\networks>java Clientarp12
Enter the logical address (IP):
165.165.79.1
The Physical Address is(MAC): 8A:BC:E3:FA

3 comments:

Followers