0% found this document useful (0 votes)
10 views8 pages

Computer Networking Lab Manual-1-Pages-Deleted

Uploaded by

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

Computer Networking Lab Manual-1-Pages-Deleted

Uploaded by

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

Exp. No.

5a
Program for Address Resolution Protocol (ARP)

Aim : To write a java program for simulating ARP protocols using TCP

ALGORITHM:
Client
1. Start the program
2. Using socket connection is established between client and server.
3. Get the IP address to be converted into MAC address.
4. Send this IP address to server.
5. Server returns the MAC address to client.

Server
1. Start the program
2. Accept the socket which is created by the client.
3. Server maintains the table in which IP and corresponding MAC addresses are stored.
4. Read the IP address which is send by the client.
5. Map the IP address with its MAC address and return the MAC address to client.

Program
Client:
import java.io.*;
import java.net.*;
public class Clientarp {
public static void main(String[] args) {
try {
// Create a socket to connect to the server on localhost and port 8080
Socket socket = new Socket("localhost", 8080);
// Get the input and output streams for communication
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
// Prompt the user to enter the logical address (IP)
System.out.print("Enter the Logical address (IP): ");
String ipAddress = userInput.readLine();
// Send the IP address to the server
out.writeBytes(ipAddress + "\n");

// Receive the MAC address from the server


String macAddress = in.readLine();
System.out.println("The Physical Address is: " + macAddress);

// Close the streams and socket


in.close();
out.close();
socket.close();
} catch (IOException e) {
System.out.println("Client Error: " + e.getMessage());
e.printStackTrace();
}
}
}
Server:
import java.io.*;
import java.net.*;
public class Serverarp {
public static void main(String[] args) {
try {
// Create a server socket listening on port 8080
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("Server is waiting for a client...");
while (true) {
// Accept a client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected!");
// Get the input and output streams for communication
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
// Read the IP address sent by the client
String ipAddress = in.readLine();
System.out.println("Received IP Address: " + ipAddress);

// Simulate ARP by mapping IP to MAC address


String macAddress = getMacAddress(ipAddress);
// Send the MAC address back to the client
out.writeUTF(macAddress);
// Close the streams and socket
in.close();
out.close();
clientSocket.close();
}
} catch (IOException e) {
System.out.println("Server Error: " + e.getMessage());
e.printStackTrace();
}
}
// Method to simulate ARP table lookup
private static String getMacAddress(String ipAddress) {
// Example ARP table with some predefined entries
if (ipAddress.equals("165.165.80.80")) {
return "6A:08:AA:C2";
} else if (ipAddress.equals("165.165.79.1")) {
return "8A:BC:34:AB";
} else {
return "MAC Address not found";
}
}
}
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

Resut: Thus the ARP protocol using TCP Sockets program was executed.
Exp. No. 5b
Program for Reverse Address Resolution Protocol (RARP)

Aim:
To write a java program for simulating RARP protocols using UDP

ALGORITHM

Client

1. Start the program


2. using datagram sockets UDP function is established. 2.Get the MAC
address to be converted into IP address.
3. Send this MAC address to server.
4. Server returns the IP address to client.

Server

1. Start the program.


2. Server maintains the table in which IP and corresponding MAC addresses are stored. 3. Read the MAC
address which is send by the client.
4. Map the IP address with its MAC address and return the IP address to client.

Client:
import java.io.*;
import java.net.*;
import java.util.*;
class Clientrarp
{
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 Physical address (MAC):");
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(IP): "+s.trim());
client.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}

Server:
import java.io.*;
import java.net.*;
import java.util.*;
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());
String s-str.trim();
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(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:
I:\ex>java Serverrarp12
I:\ex>java Clientrarp12

Enter the Physical address


(MAC): 6A:08:AA:C2
The Logical Address is(IP): 165.165.80.80

Result : Thus the RARP program using UDP was executed.

You might also like