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 TCP


Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientarp
{
            public static void main(String args[])
            {
            try
            {         
                        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

                        Socket clsct=new Socket("127.0.0.1",139);
                        DataInputStream din=new DataInputStream(clsct.getInputStream());
                        DataOutputStream dout=new DataOutputStream(clsct.getOutputStream());
                        System.out.println("Enter the Logical address(IP):");
                        String str1=in.readLine();
                        dout.writeBytes(str1+'\n');
                        String str=din.readLine();
                        System.out.println("The Physical Address is: "+str);             
                        clsct.close();
            }
            catch (Exception e)
            {
            System.out.println(e);
            }
            }
}

Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Serverarp
{
            public static void main(String args[])
            {
            try
            {
                        ServerSocket obj=new ServerSocket(139);
                        Socket obj1=obj.accept();
                        while(true)
                        {
                                    DataInputStream din=new DataInputStream(obj1.getInputStream());
                                    DataOutputStream dout=new DataOutputStream(obj1.getOutputStream());
                                    String str=din.readLine();
                                    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(str.equals(ip[i]))
                                                {
                                                            dout.writeBytes(mac[i]+'\n');
                                                            break;
                                                }
                                    }                     
                                    obj.close();
                        }
                       
            }
            catch(Exception e)
            {
                        System.out.println(e);
            }
            }
}
Output:
E:\networks>java Serverarp
E:\networks>java Clientarp
Enter the Logical address(IP):
165.165.80.80
The Physical Address is: 6A:08:AA:C2

6 comments:

Followers