DarkWebCode is a place where you can find a lot of exciting and inspiring stuff about Web Development, Blogging, SEO and many more

Breaking

Implementation of Stop and Wait Protocol

Write a code simulating ARP /RARP protocols.

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.*; 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