ALGORITHM:
Client
- Start the program
- Using socket connection is established between client and server.
- Get the IP address to be converted into MAC address.
- Send this IP address to server.
- Server returns the MAC address to client.
Server
- Start the program
- Accept the socket which is created by the client.
- Server maintains the table in which IP and corresponding MAC addresses are stored.
- Read the IP address which is send by the client.
- Map the IP address with its MAC address and return the MAC address to client.
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