You are on page 1of 31

Exp# TCP Echo Server/Client

Aim
To implement echo server and client in java using TCP sockets.

Algorithm

Server
1. Create a server socket.
2. Wait for client to be connected.
3. Read text from the client
4. Echo the text back to the client.
5. Repeat steps 4-5 until bye or null is read.
6. Close the I/O streams
7. Close the server socket
8. Stop

Client
1. Create a socket and establish connection with the server
2. Get input from user.
3. If equal to bye or null, then go to step 7.
4. Send text to the server.
5. Display the text echoed by the server
6. Repeat steps 2-4
7. Close the I/O streams
8. Close the client socket
9. Stop

Result

Thus data from client to server is echoed back to the client to check reliability/noise level
of the channel.

1
Program

// TCP Echo Server--tcpechoserver.java

import java.net.*;
import java.io.*;

public class tcpechoserver


{
public static void main(String[] arg) throws IOException
{
ServerSocket sock = null;
BufferedReader fromClient = null;
OutputStreamWriter toClient = null;
Socket client = null;
try
{
sock = new ServerSocket(4000);
System.out.println("Server Ready");
client = sock.accept();
System.out.println("Client Connected");
fromClient = new BufferedReader(new
InputStreamReader(client.getInputStream()));
toClient = new
OutputStreamWriter(client.getOutputStream());
String line;
while (true)
{
line = fromClient.readLine();
if ( (line == null) || line.equals("bye"))
break;
System.out.println ("Client [ " + line + " ]");
toClient.write("Server [ "+ line +" ]\n");
toClient.flush();
}
fromClient.close();
toClient.close();
client.close();
sock.close();
System.out.println("Client Disconnected");
}
catch (IOException ioe)
{
System.err.println(ioe);
}
}
}
//TCP Echo Client--tcpechoclient.java

import java.net.*;
import java.io.*;

public class tcpechoclient


{
public static void main(String[] args) throws IOException
{
BufferedReader fromServer = null, fromUser = null;
PrintWriter toServer = null;
Socket sock = null;
try
{
if (args.length == 0)
sock = new Socket(InetAddress.getLocalHost(),
4000);
else
sock = new Socket(InetAddress.getByName(args[0]),
4000);
fromServer = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
fromUser = new BufferedReader(new
InputStreamReader(System.in));
toServer = new PrintWriter(sock.getOutputStream(),
true
String Usrmsg, Srvmsg;
System.out.println("Type \"bye\" to quit");
while (true)
{
System.out.print("Enter msg to server : ");
Usrmsg = fromUser.readLine();
if (Usrmsg==null || Usrmsg.equals("bye"))
{
toServer.println("bye");
break;
}
else
toServer.println(Usrmsg);
Srvmsg = fromServer.readLine();
System.out.println(Srvmsg);
}
fromUser.close();
fromServer.close();
toServer.close();
sock.close();
}
catch (IOException ioe)
{
System.err.println(ioe);
}
Output

Server Console

$ javac tcpechoserver.java
$ java tcpechoserver
Server Ready
Client Connected
Client [ hello ]
Client [ how are you ]
Client [ i am fine ]
Client [ ok ]
Client Disconnected

Client Consol e

$ javac tcpechoclient.java
$ java tcpechoclient
Type "bye" to quit
Enter msg to server : hello
Server [ hello ]
Enter msg to server : how are you
Server [ how are you ]
Enter msg to server : i am fine
Server [ i am fine ]
Enter msg to server : ok
Server [ ok ]
Enter msg to server : bye
Exp# TCP Chat Server/Client

Aim
To implement a chat server and client in java using TCP sockets.

Algorithm

Server
1. Create a server socket
2. Wait for client to be connected.
3. Read Client's message and display it
4. Get a message from user and send it to client
5. Repeat steps 3-4 until the client sends "end"
6. Close all streams
7. Close the server and client socket
8. Stop

Client
1. Create a client socket and establish connection with the server
2. Get a message from user and send it to server
3. Read server's response and display it
4. Repeat steps 2-3 until chat is terminated with "end" message
5. Close all input/output streams
6. Close the client socket
7. Stop

Result
Thus both the client and server exchange data using TCP socket programming.
Program
// TCP Chat Server--tcpchatserver.java
import java.io.*;
import java.net.*;
class tcpchatserver
{
public static void main(String args[])throws Exception
{
PrintWriter toClient;
BufferedReader fromUser, fromClient;
try
{
ServerSocket Srv = new ServerSocket(5555);
System.out.print("\nServer started\n");
Socket Clt = Srv.accept();
System.out.println("Client connected");
toClient = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())), true);
fromClient = new BufferedReader(new
InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new
InputStreamReader(System.in));
String CltMsg, SrvMsg;
while(true)
{
CltMsg= fromClient.readLine();
if(CltMsg.equals("end"))
break;
else
{
System.out.println("\nServer <<< " +
CltMsg);
System.out.print("Message to Client : ");
SrvMsg = fromUser.readLine();
toClient.println(SrvMsg);
}
}
System.out.println("\nClient Disconnected");
fromClient.close();
toClient.close();
fromUser.close();
Clt.close();
Srv.close();
}
catch (Exception E)
{ System.out.println(E.getMessage()
);
}
}
}

// TCP Chat Client--tcpchatclient.java


import java.io.*;
import java.net.*;
class tcpchatclient
{
public static void main(String args[])throws Exception
{
Socket Clt;
PrintWriter toServer;
BufferedReader fromUser, fromServer;
try
{
if (args.length > 1)
{
System.out.println("Usage: java hostipaddr");
System.exit(-1);
}
if (args.length == 0)
Clt = new Socket(InetAddress.getLocalHost(),5555);
else
Clt = new Socket(InetAddress.getByName(args[0]),
5555);
toServer = new PrintWriter(new BufferedWriter(new
OutputStreamWriter(Clt.getOutputStream())), true);
fromServer = new BufferedReader(new
InputStreamReader(Clt.getInputStream()));
fromUser = new BufferedReader(new
InputStreamReader(System.in));
String CltMsg, SrvMsg;
System.out.println("Type \"end\" to Quit");
while (true)
{
System.out.print("\nMessage to Server : ");
CltMsg = fromUser.readLine();
toServer.println(CltMsg);
if (CltMsg.equals("end"))
break;
SrvMsg = fromServer.readLine();
System.out.println("Client <<< " + SrvMsg);
}
}
catch(Exception E)
{
System.out.println(E.getMessage());
}
}
}

Output

Server Console

$ javac tcpchatserver.java
$ java tcpchatserver
Server started
Client connected

Server <<< hi
Message to Client : hello

Server <<< how r u?


Message to Client : fine

Server <<< me too


Message to Client : bye

Client Disconnected
Client Consol e

$ javac tcpchatclient.java
$ java tcpchatclient
Type "end" to Quit

Message to Server : hi
Client <<< hello

Message to Server : how r u?


Client <<< fine

Message to Server : me too


Client <<< bye

Message to Server : end


Exp# UDP DNS Server/Client

Aim
To implement a DNS server and client in java using UDP sockets.

Algorithm

Server
1. Define a array of hosts and its corresponding IP address in another array
2. Create a datagram socket
3. Create a datagram packet to receive client request
4. Read the domain name from client to be resolved
5. Lookup the host array for the domain name
6. If found then retrieve corresponding address
7. Construct a datagram packet to send response back to the client
8. Repeat steps 3-7 to resolve further requests from clients
9. Close the server socket
10. Stop

Client
1. Create a datagram socket
2. Get domain name from user
3. Construct a datagram packet to send domain name to the server
4. Create a datagram packet to receive server message
5. If it contains IP address then display it, else display "Domain does not
6. exist" the client socket
Close
7. Stop

Result

Thus domain name requests by the client are resolved into their respective logical
address using lookup method.
Program

// UDP DNS Server -- udpdnsserver.java

import java.io.*;
import java.net.*;

public class udpdnsserver


{
private static int indexOf(String[] array, String str)
{
str = str.trim();
for (int i=0; i < array.length; i++)
{
if (array[i].equals(str))
return i;
}
return -1;
}

public static void main(String arg[])throws IOException


{
String[] hosts = {"yahoo.com", "gmail.com",
"cricinfo.com", "facebook.com"};
String[] ip = {"68.180.206.184", "209.85.148.19",
"80.168.92.140", "69.63.189.16"};
System.out.println("Press Ctrl + C to Quit");

while (true)
{
DatagramSocket serversocket=new DatagramSocket(1362);
byte[] senddata = new byte[1021];
byte[] receivedata = new byte[1021];

DatagramPacket recvpack = new


DatagramPacket(receivedata, receivedata.length);
serversocket.receive(recvpack);
String sen = new String(recvpack.getData());
InetAddress ipaddress = recvpack.getAddress();
int port = recvpack.getPort();
String capsent;
System.out.println("Request for host " + sen);

if(indexOf (hosts, sen) != -1)


capsent = ip[indexOf (hosts, sen)];
else
capsent = "Host Not Found";
senddata = capsent.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,
senddata.length,ipaddress,port);

serversocket.send(pack);
serversocket.close();
}
}
}

//UDP DNS Client -- udpdnsclient.java

import java.io.*;
import java.net.*;

public class udpdnsclient


{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
DatagramSocket clientsocket = new DatagramSocket();
InetAddress ipaddress;
if (args.length == 0)
ipaddress = InetAddress.getLocalHost();
else
ipaddress = InetAddress.getByName(args[0]);
byte[] senddata = new byte[1024];
byte[] receivedata = new byte[1024];
int portaddr = 1362;

System.out.print("Enter the hostname : ");


String sentence = br.readLine();
Senddata = sentence.getBytes();
DatagramPacket pack = new DatagramPacket(senddata,
senddata.length, ipaddress,portaddr);
clientsocket.send(pack);

DatagramPacket recvpack =new DatagramPacket(receivedata,


receivedata.length);
clientsocket.receive(recvpack);
String modified = new String(recvpack.getData());
System.out.println("IP Address: " + modified);
clientsocket.close();
}
}
Output

Server Console

$ javac udpdnsserver.java
$ java udpdnsserver Press
Ctrl + C to Quit Request
for host yahoo.com
Request for host cricinfo.com
Request for host youtube.com

Client Consol e

$ javac udpdnsclient.java
$ java udpdnsclient
Enter the hostname : yahoo.com
IP Address: 68.180.206.184

$ java udpdnsclient
Enter the hostname : cricinfo.com
IP Address: 80.168.92.140

$ java udpdnsclient
Enter the hostname : youtube.com
IP Address: Host Not Found
Exp# Go Back N ARQ

Aim
To simulate a sliding window protocol that uses Go Back N ARQ.

Algorithm

Sender

1. Create a server socket


2. Assume the sending window size as 7 (m = 3).
3. If receiver is ready, initialize sender's frame sequence to 0.
4. Get data from user.
5. Send it to the receiver along with sequence number.
6. Increment sequence number by 1.
7. Repeat steps 46 until all frames have been sent.
8. Wait for acknowledgements.
9. If all acknowledgements have arrived then go to step 12.
10. Set sequence number to earliest outstanding frame for which there is no ACK.
11. Go to step 4.
12. Stop

Receiver

1. Indicate to sender, the readiness to accept frames.


2. Initialize receiver's expected frame sequence to 0.
3. Accept the incoming frame.
4. If frame's sequence receiver's sequence then go to step 7.
5. Send an acknowledgement.
6. Repeat steps 36 until all frames are received in sequence and go to step 9
7. Discard frame, thereby force the sender to retransmit.
8. Go to step 3.
9. Stop

Result

Thus using Go Back N procedure, the sender retransmits all frames from the
earliest outstanding frame.
Program

// Go Back N Sender--GBNSend.java
import java.io.*;
import java.net.*;

public class GBNSend


{
String output[] = new String[7];
void sendMsg(BufferedReader buff, BufferedReader in,
PrintWriter out, int x) throws Exception
{
try
{
for(int i=x; i<7; i++)
{
System.out.print("Content for frame" + i + " : ");
output[i] = in.readLine();
out.println(output[i]);
out.flush();
}
}
catch(Exception e)
{
System.out.println("exception is" + e);
}
}

public static void main(String[] args) throws Exception


{
GBNSend g = new GBNSend();
ServerSocket ssoc = new ServerSocket(6000);
Socket soc = ssoc.accept();
String input[] = new String[7];
BufferedReader buff = new BufferedReader(new
InputStreamReader(soc.getInputStream()));
PrintWriter out = new PrintWriter(new
OutputStreamWriter(soc.getOutputStream()));
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));

System.out.println("Window size 7");


System.out.println("Enter message in format--
SeqNoData");
int x = 0;
g.sendMsg(buff,in,out,0);
for(int j=0; j<7; j++)
{
String s = new String();
input[j] = buff.readLine();
s = j + "";
if(input[j].startsWith(s))
System.out.println("Ack for frame" + j);
else
{
System.out.println("Frame" + j + " lost/corrupt.
Go Back & resend");
x = j;
g.sendMsg(buff,in,out,j);
--j;
}
}
soc.close();
ssoc.close();
}
}

//Go Back N Receiver--GBNReceive.java


import java.io.*;
import java.net.*;

public class GBNReceive


{
String input[] = new String[7];
void ReceiveMsg(BufferedReader buff, PrintWriter out,
int x) throws Exception
{
try
{
for(int i=x; i<7; i++)
input[i] = buff.readLine();
}
catch(Exception e)
{
System.out.println("exception is" + e);
}
}

public static void main(String[] args) throws Exception


{
GBNReceive g = new GBNReceive();
Socket soc = new Socket("localhost", 6000);
BufferedReader buff = new BufferedReader(new
InputStreamReader(soc.getInputStream()));
PrintWriter out = new PrintWriter(new
OutputStreamWriter(soc.getOutputStream()));
int x = 0;
g.ReceiveMsg(buff,out,0);
for(int j=x; j<7; j++)
{
String s = new String(); s =
j + "";
if(g.input[j].startsWith(s))
{
String rmsg = g.input[j];
rmsg = rmsg.substring(1,rmsg.length());
System.out.println("Frame" + j + "data is " +
rmsg);
out.println(j + "ack");
out.flush();
}
else
{
out.println("no ack");
out.flush(); x = j;
g.ReceiveMsg(buff,out,j);
--j;
}
}
soc.close();
}
}
Output

Sender

$ javac GBNSend.java
$ java GBNSend
Window size 7
Enter message in format--SeqNoData
Content for frame0 : 0hi Content
for frame1 : 1hello Content for
frame2 : 2i am here Content for
frame3 : 3where r u? Content for
frame4 : here only
Content for frame5 : 5can u see me
Content for frame6 : 6which direction
Ack for frame0
Ack for frame1
Ack for frame2
Ack for frame3
Frame4 lost/corrupt. Go Back & resend
Content for frame4 : 4here only
Content for frame5 : 5which direction
Content for frame6 : ok
Ack for frame4
Ack for frame5
Frame6 lost/corrupt. Go Back & resend
Content for frame6 : 6South
Ack for frame6

Receiver

$ javac GBNReceive.java
$ java GBNReceive
Frame0 data is hi
Frame1 data is hello
Frame2 data is i am here
Frame3 data is where r u?
Frame4 data is here only
Frame5 data is which direction
Frame6 data is South
Exp# Selective Repeat ARQ

Aim
To simulate a sliding window protocol that uses Selective Repeat ARQ.

Algorithm

Sender

1. Create a server socket


2. Assume sending window size as 7.
3. If receiver is ready, initialize sender's frame sequence to 0.
4. Get data from user.
5. Send it to receiver along with sequence number.
6. Increment sequence number by 1.
7. Repeat steps 46 until all frames have been sent.
8. Wait for acknowledgements.
9. If all acknowledgements have arrived then go to step 12
10. Resend the earliest outstanding frame for which there is no ACK.
11. Go to step 8.
12. Stop

Receiver

1. Indicate to the sender, the readiness to accept frames.


2. Initialize receiver's expected frame sequence to 0.
3. Accept the incoming frame.
4. If frame is corrupt then step 8.
5. If frames are in order then step 6 else step 3.
6. Send acknowledgements.
7. If ACK for all frames are sent then go to step 10
8. Discard the frame, thereby force the sender to retransmit
9. Go to step 3
10. Stop

Result

Thus using Selective Repeat procedure, the sender retransmits only frames that
are unacknowledged.
Program

// Selective Repeat Sender--SRSend.java


import java.io.*;
import java.net.*;

public class SRSend


{
String output[] = new String[7];
void sendMsg(BufferedReader buff, BufferedReader in,
PrintWriter out, int x, int flag) throws Exception
{
try
{
if(flag == 1)
{
for(int i=0; i<7; i++)
{
System.out.print("Content for frame "+i+" : ");
output[i] = in.readLine();
out.println(output[i]);
out.flush();
}
}
else
{
System.out.print("Content for frame" + x + " : ");
output[x] = in.readLine();
out.println(output[x]);
out.flush();
}
}
catch(Exception e)
{
System.out.println("exception is" +e);
}
}

public static void main(String[] args) throws Exception


{
SRSend g = new SRSend();
ServerSocket ssoc = new ServerSocket(6000);
Socket soc = ssoc.accept();
String input[] = new String[7];
BufferedReader buff = new BufferedReader(new
InputStreamReader(soc.getInputStream()));
PrintWriter out = new PrintWriter(new
OutputStreamWriter(soc.getOutputStream()));
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));

System.out.println("Window size 7");


System.out.println("Enter message in format-SeqNoData");
int x = 0;
g.sendMsg(buff,in,out,0,1);
for(int j=x; j<7; j++)
{
String s = new String();
input[j] = buff.readLine();
s = j + "";
if(input[j].startsWith(s))
System.out.println("Ack for frame" + j);
else
{
System.out.println("Frame" + j + " lost/corrupt.
Resend it");
x = j;
g.sendMsg(buff,in,out,x,0);
j--;
}
}
soc.close();
ssoc.close();
}
}

// Selective Repeat Receiver


import java.io.*;
import java.net.*;

public class SRReceive


{
String input[] = new String[7];
void ReceiveMsg(BufferedReader buff, PrintWriter out,
int x, int flag) throws Exception
{
try
{
if(flag == 1)
for(int i=0; i<7; i++)
input[i] = buff.readLine();
else
input[x] = buff.readLine();
}
catch(Exception e)
{
System.out.println("exception is" + e);
}}

public static void main(String[] args) throws Exception


{
SRReceive g = new SRReceive();
Socket soc = new Socket("localhost", 6000);
BufferedReader buff = new BufferedReader(new
InputStreamReader(soc.getInputStream()));
PrintWriter out = new PrintWriter(new
OutputStreamWriter(soc.getOutputStream()));

int x=0;
g.ReceiveMsg(buff,out,0,1);
for(int j=x; j<7; j++)
{
String s = new String(); s =
j + "";
if(g.input[j].startsWith(s))
{
String rmsg = g.input[j];
rmsg = rmsg.substring(1, rmsg.length());
System.out.println("Frame" + j + "data is " +
rmsg);
out.println(j + "ack");
out.flush();
}
else
{
out.println("no ack");
out.flush(); x = j;
g.ReceiveMsg(buff,out,j,0);
j--;
}
}
soc.close();
}
}
Output

Sender

$ javac SRSend.java
$ java SRSend
Window size 7
Window size 7
Enter message in format--SeqNoData
Content for frame0 : 0hi Content
for frame1 : 1hello Content for
frame2 : 2i am here Content for
frame3 : 3where r u? Content for
frame4 : here only
Content for frame5 : 5can u see me
Content for frame6 : 6which direction
Ack for frame0
Ack for frame1
Ack for frame2
Ack for frame3
Frame4 lost/corrupt. Resend it
Content for frame4 : 4here only
Ack for frame4
Ack for frame5
Ack for frame6

Receiver

$ javac SRReceive.java
$ java SRReceive
Frame0 data is hi
Frame1 data is hello
Frame2 data is i am here
Frame3 data is where r u?
Frame4 data is here only
Frame5 data is can u see me
Frame6 data is which direction
Exp# 5C ARP Client/Server

Aim
To know the physical address of a host when its logical address is known using ARP
protocol.

Algorithm

Target/Server

1. Create a server socket.


2. Accept client connection.
3. Read IP address from the client request
4. Check its configuration file and compare with its logical address.
5. If there is a match, send the host physical address.
6. Stop

Client

1. Create a socket.
2. Send IP address to the target machine
3. Receive target's response
4. If it is a MAC address then display it and go to step 6
5. Display "Host not found"
6. Stop

Result

Thus using ARP protoocl MAC address is obtained.


Program

//ARP Server -- arpserver.java


import java.io.*;
import java.net.*;

class arpserver
{
public static void main(String args[])throws IOException
{
try
{
ServerSocket soc = new ServerSocket(2500);
System.out.println("Server started");
Socket client = null;
client = soc.accept();
String str;

PrintStream ps = new
PrintStream(client.getOutputStream());
BufferedReader br = new BufferedReader(new
InputStreamReader(client.getInputStream()));
Runtime r = Runtime.getRuntime(); Process
p = r.exec("ifconfig eth0");
BufferedReader pin=new BufferedReader(new
InputStreamReader(p.getInputStream()));
String haddr = "";
String ipaddr = br.readLine();
int flag = 0;

while((str = pin.readLine())!=null)
{
System.out.println(str);
if((str.indexOf("HWaddr")) != -1)
{
int tlen = str.length();
int hlen = tlen - 19;
haddr = str.substring(hlen,tlen);
}
else if ((str.indexOf(ipaddr)) != -1)
{
flag = 1;
}
}
if (flag == 1)
ps.println(haddr);

ps.close();
br.close();
pin.close();
client.close();
soc.close();
}
catch(IOException io)
{
System.err.println("Exception : " + io.toString());
}
}
}

//ARP Client -- arpclient.java


import java.io.*;
import java.net.*;

class arpclient
{
public static void main(String args[])
{
try
{
Socket client = new Socket("localhost", 2500);
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
PrintStream ps = new
PrintStream(client.getOutputStream());
String ipaddr,haddr = null;
BufferedReader sin = new BufferedReader(new
InputStreamReader(client.getInputStream()));

System.out.print("Enter the IP address : ");


ipaddr = br.readLine();
ps.println(ipaddr);
haddr = sin.readLine();
if (haddr == null)
System.out.println("Host does not exist");
else
System.out.println("Physical Address " + haddr);
ps.close();
br.close();
client.close();
}
catch(IOException io)
{
System.err.println(io.toString());
}
}
}
Output

Serve r

$ javac arpserver.java
$ java arpserver
Server started
eth0 Link encap:Ethernet HWaddr B8:AC:6F:1B:AB:06
inet addr:172.16.6.21 Bcast:172.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::baac:6fff:fe1b:ab06/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:450 errors:0 dropped:0 overruns:0 frame:0
TX packets:127 errors:0 dropped:0 overruns:0
carrier:0 collisions:0 txqueuelen:1000
RX bytes:48118 (46.9 KiB) TX bytes:21025 (20.5 KiB)
Interrupt:16

Client

$ javac arpclient.java
$ java arpclient
Enter the IP address : 172.16.6.21
Physical Address B8:AC:6F:1B:AB:06
Exp# RARP Client/Server

Aim
To know the logical address of a host when its physical address is known using RARP
protocol.

Algorithm

Target/Server

1. Create a server socket.


2. Accept client connection.
3. Read MAC address from the client request
4. Check its configuration file and compare with its physical address.
5. If there is a match, send the host logical address.
6. Stop

Client

1. Create a socket.
2. Send physical address to the target machine
3. Receive target's response
4. If it is a IP address then display it and go to step 6
5. Display "Host not found"
6. Stop

Result

Thus using RARP protocol, IP address is obtained.


Program

//RARP Server -- rarpserver.java


import java.io.*;
import java.net.*;
class rarpserver
{
public static void main(String args[])throws IOException
{
try
{
ServerSocket soc = new ServerSocket(2500);
System.out.println("Server started");
Socket client = null;
client = soc.accept();
String str;
PrintStream ps = new
PrintStream(client.getOutputStream());
BufferedReader br = new BufferedReader(new
InputStreamReader(client.getInputStream()));
Runtime r = Runtime.getRuntime(); Process p
= r.exec("ifconfig eth0"); BufferedReader
pin = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String ipaddr = "";
String haddr = br.readLine();
int flag = 0;
while((str = pin.readLine())!=null)
{
System.out.println(str);
if ((str.indexOf(haddr)) != -1)
{
flag = 1;
}
else if((str.indexOf("inet addr")) != -1)
{
int pos = str.indexOf("inet addr:") + 10;
int offset = pos + 13;
ipaddr = str.substring(pos,offset);
}
}
if (flag == 1)
ps.println(ipaddr);
ps.close();
br.close();
pin.close();
client.close();
soc.close();

}
catch(IOException io)
{
System.err.println("Exception : " + io.toString());
}
}
}

// RARP Client -- rarpclient.java


import java.io.*;
import java.net.*;

class rarpclient
{
public static void main(String args[])
{
try
{
Socket client = new Socket("localhost", 2500);
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
PrintStream ps = new
PrintStream(client.getOutputStream());
String haddr,ipaddr = null;
BufferedReader sin = new BufferedReader(new
InputStreamReader(client.getInputStream()));
System.out.print("Enter the physical address : ");
haddr = br.readLine();
ps.println(haddr);
ipaddr = sin.readLine();
if (ipaddr == null)
System.out.println("Host does not exist");
else
System.out.println("Logical Address " + ipaddr);
ps.close();
br.close();
client.close();
}
catch(IOException io)
{
System.err.println(io.toString());
}
}
}
Output

Serve r

$ javac rarpserver.java
$ java rarpserver
Server started
eth0 Link encap:Ethernet HWaddr B8:AC:6F:1B:AB:06
inet addr:172.16.6.21 Bcast:172.255.255.255 Mask:255.0.0.0
inet6 addr: fe80::baac:6fff:fe1b:ab06/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:450 errors:0 dropped:0 overruns:0 frame:0
TX packets:127 errors:0 dropped:0 overruns:0
carrier:0 collisions:0 txqueuelen:1000
RX bytes:48118 (46.9 KiB) TX bytes:21025 (20.5 KiB)
Interrupt:16

Client

$ javac rarpclient.java
$ java rarpclient
Enter the physical address : B8:AC:6F:1B:AB:06
Logical Address 172.16.6.21