Sunteți pe pagina 1din 33

BCAP2008 Computer Networking Lab L T P C

Version No. Date of Approval: Sep 2, 2018 0 0 2 1


Prerequisite
co-requisites

Course Objectives
The objective of this course is to:
1. Teach fundamental concepts of networks.
2. Give hands on training of network installation and configuration.

Course Outcomes
At the end of the course student will be able to:
1. Understand basic of networking from the user’s, developer’s and administrator’s
perspective.

Mode of Evaluation:Continuous assessment of the progress will be done week followed by a SEE
Laboratory Exam.

Laboratory

Components Internal SEE

Marks 50 50

Total Marks 100


Relationship between the Course Outcomes (COs) and Program Outcomes (POs)

Mapping between Cos and Pos

Environment and sustainability


Problem analysis

Design/development of solutions

Conduct investigationsof complex problems

Ethics
Engineering Knowledge

Modern tool usage

Project management and finance


The engineer and society

Individual or team work

Communication

Life-long Learning
Program Outcome→
Mapped
Sl. No. Course Outcomes (COs) Program
Outcomes

Understand basic of networking from the user’s, developer’s


1 and administrator’s perspective. 1,2,3,9

Course
Course Code 1 2 3 4 5 6 7 8 9 10 11 12
Name

Computer
BCAP2008 Networking 2 2 3 2
Lab

1=addressed to small extent


2= addressed significantly
3=major part of course

Theory
Lab
CourseLabOutcomes Assessment

ThelaboratorycomponentstronglycontributestowardstheprogramoutcomeDesign/development
ofsolutions-PO(3).Thisevaluationmethodwillbe usedfor theevaluationoflabandprogram
outcomes of this course.
Direct Measurement Report
BCAP2008Outcome (3)Report Form
Measure– percent of students scoringat least 70%marks in
lab.
Target– 70%ofstudents

List of Experiments:

EX.NO. TITLE OF LAB EXPERIMENTS

1 BASIC NETWORKING COMMANDS

2 SLIDING WINDOW

3 CYCLIC REDUNDANT CHECK

4 ROUTING-SHORTEST PATH FROM SINGLE NODE

5 BROADCASTING

6 MULTICASTING

7 ADDRESS RESOLUTION PROTOCOL

8 IMPLEMENTATION OF UDP

9 FILE TRANSFER PROTOCOL(FTP)

10 DOMAIN NAME SERVICE(DNS)

Prepared By NAGARAJAN.M
(Assistant Professor (SCSE), Computer Science and Engineering)
Ex.No. 1 NETWORKING COMMANDS
AIM
To study the basic networking commands.

COMMANDS:

C:\>arp –a: ARP is short form of address resolution protocol, It will show the IP address of your
computer along with the IP address and MAC address of your router.

C:\>hostname: This is the simplest of all TCP/IP commands. It simply displays the name of your
computer.

C:\>ipconfig: The ipconfig command displays information about the host (the computer your sitting
at)computer TCP/IP configuration.

C:\>ipconfig /all: This command displays detailed configuration information about your TCP/IP
connection including Router, Gateway, DNS, DHCP, and type of Ethernet adapter in your system.
C:\>Ipconfig /renew: Using this command will renew all your IP addresses that you are currently
(leasing) borrowing from the DHCP server. This command is a quick problem solver if you are
having connection issues, but does not work if you have been configured with a static IP address.
C:\>Ipconifg /release: This command allows you to drop the IP lease from the DHCP server.
C:\>ipconfig /flushdns: This command is only needed if you’re having trouble with your networks
DNS configuration. The best time to use this command is after network configuration frustration sets
in, and you really need the computer to reply with flushed.

C:\>nbtstat –a: This command helps solve problems with NetBIOS name resolution.
(Nbt stands for NetBIOS over TCP/IP)

C:\>netdiag: Netdiag is a network testing utility that performs a variety of network diagnostic tests,
allowing you to pinpoint problems in your network. Netdiag isn’t installed by default, but can be
installed from the Windows XP CD after saying no to the install. Navigate to the CD ROM drive
letter and open the support\tools folder on the XP CD and click the setup.exe icon in the
support\tools folder.

C:\>netstat: Netstat displays a variety of statistics about a computers active TCP/IP


connections. This tool is most useful when you’re having trouble with TCP/IP applications such as
HTTP, and FTP.

C:\>nslookup: Nslookup is used for diagnosing DNS problems. If you can access a resource by
specifying an IP address but not it’s DNS you have a DNS problem.

C:\>pathping: Pathping is unique to Window’s, and is basically a combination of the Ping and Tracert
commands. Pathping traces the route to the destination address then launches a 25 second test of each
router along the way, gathering statistics on the rate of data loss along each hop.

C:\>ping: Ping is the most basic TCP/IP command, and it’s the same as placing a phone call to your
best friend. You pick up your telephone and dial a number, expecting your best friend to reply with
“Hello” on the other end. Computers make phone calls to each other over a network by using a Ping
command. The Ping commands main purpose is to place a phone call to another computer on the
network, and request an answer. Ping has 2 options it can use to place a phone call to another
computer on the network. It can use the computers name or IP address.

C:\>route: The route command displays the computers routing table. A typical computer, with a
single network interface, connected to a LAN, with a router is fairly simple and generally doesn’t
pose any network problems. But if you’re having trouble accessing other computers on your
network, you can use the route command to make sure the entries in the routing table are correct.

C:\>tracert: The tracert command displays a list of all the routers that a packet has to go through to
get from the computer where tracert is run to any other computer on the internet.

RESULT
Thus the above list of primitive has been studied
Ex.No. : 2
SLIDING WINDOW

AIM:
To write a java program to perform sliding window.

Sliding window sender side


import java.net.*;
import java.io.*;
import java.rmi.*;
public class slidsender
{
public static void main(String a[])throws Exception
{
ServerSocket ser=new ServerSocket(10);
Socket s=ser.accept();
DataInputStream in=new DataInputStream(System.in);
DataInputStream in1=new DataInputStream(s.getInputStream());
String sbuff[]=new String[8];
PrintStream p;
int sptr=0,sws=8,nf,ano,i;
String ch;
do
{
p=new PrintStream(s.getOutputStream());
System.out.print("Enter the no. of frames : ");
nf=Integer.parseInt(in.readLine());
p.println(nf);
if(nf<=sws-1)
{
System.out.println("Enter "+nf+" Messages to be send\n");
for(i=1;i<=nf;i++)
{
sbuff[sptr]=in.readLine();
p.println(sbuff[sptr]);
sptr=++sptr%8;
}
sws-=nf;
System.out.print("Acknowledgment received");
ano=Integer.parseInt(in1.readLine());
System.out.println(" for "+ano+" frames");
sws+=nf;
}
else
{
System.out.println("The no. of frames exceeds window size");
break;
}
System.out.print("\nDo you wants to send some more frames : ");
ch=in.readLine(); p.println(ch);
}
while(ch.equals("yes")); s.close(); } }

Sliding window Receiver side:

import java.net.*;
import java.io.*;
class slidreceiver
{
public static void main(String a[])throws Exception
{
Socket s=new Socket(InetAddress.getLocalHost(),10);
DataInputStream in=new DataInputStream(s.getInputStream());
PrintStream p=new PrintStream(s.getOutputStream());
int i=0,rptr=-1,nf,rws=8;
String rbuf[]=new String[8];
String ch; System.out.println();
do
{
nf=Integer.parseInt(in.readLine());
if(nf<=rws-1)
{
for(i=1;i<=nf;i++)
{
rptr=++rptr%8;
rbuf[rptr]=in.readLine();
System.out.println("The received Frame " +rptr+" is : "+rbuf

[rptr]);
}
rws-=nf;
System.out.println("\nAcknowledgment sent\n");
p.println(rptr+1); rws+=nf; }
else
break; ch=in.readLine(); } while(ch.equals("yes"));
}
}
SLIDING WINDOW OUTPUT:

RESULT:

Thus the program for sliding window has been executed and output verified.
Ex.No.: 3
CYCLIC REDUNDANT CHECK

AIM:
To write a java program to perform cyclic redundant check.

CRC PROGRAM:
import java.io.*;
class crc_gen
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int[] data;
int[] div;
int[] divisor;
int[] rem;
int[] crc;
int data_bits, divisor_bits, tot_length;
System.out.println("Enter number of data bits : ");
data_bits=Integer.parseInt(br.readLine());
data=new int[data_bits];
System.out.println("Enter data bits : ");
for(int i=0; i<data_bits; i++)
data[i]=Integer.parseInt(br.readLine());
System.out.println("Enter number of bits in divisor : ");
divisor_bits=Integer.parseInt(br.readLine());
divisor=new int[divisor_bits];
System.out.println("Enter Divisor bits : ");
for(int i=0; i<divisor_bits; i++)
divisor[i]=Integer.parseInt(br.readLine());
tot_length=data_bits+divisor_bits-1;
div=new int[tot_length];
rem=new int[tot_length];
crc=new int[tot_length];
/*------------------ CRC GENERATION-----------------------*/
for(int i=0;i<data.length;i++)
div[i]=data[i];
System.out.print("Dividend (after appending 0's) are : ");
for(int i=0; i< div.length; i++)
System.out.print(div[i]);
System.out.println();
for(int j=0; j<div.length; j++){
rem[j] = div[j];
}
rem=divide(div, divisor, rem);
for(int i=0;i<div.length;i++) //append dividend and ramainder
{
crc[i]=(div[i]^rem[i]);
}
System.out.println();
System.out.println("CRC code : ");
for(int i=0;i<crc.length;i++)
System.out.print(crc[i]);
/*-------------------ERROR DETECTION---------------------*/
System.out.println();
System.out.println("Enter CRC code of "+tot_length+" bits : ");
for(int i=0; i<crc.length; i++)
crc[i]=Integer.parseInt(br.readLine());
for(int j=0; j<crc.length; j++){
rem[j] = crc[j];
}
rem=divide(crc, divisor, rem);
for(int i=0; i< rem.length; i++)
{
if(rem[i]!=0)
{
System.out.println("Error");
break;
}
if(i==rem.length-1)
System.out.println("No Error");
}
System.out.println("THANK YOU.... :)");
}
static int[] divide(int div[],int divisor[], int rem[])
{
int cur=0;
while(true)
{
for(int i=0;i<divisor.length;i++)
rem[cur+i]=(rem[cur+i]^divisor[i]);

while(rem[cur]==0 && cur!=rem.length-1)


cur++;
if((rem.length-cur)<divisor.length)
break;
}
return rem; }}
OUTPUT:

RESULT:
Thus the program for CRC has been executed and output verified.

Ex.No.: 4
ROUTING-SHORTEST PATH FROM SINGLE NODE

AIM:
To write a java program to find shortest path from single node using dijkstra routing
algorithm .

PROGRAM:
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class DijkstraAlgorithmSet


{
private int distances[];
private Set<Integer> settled;
private Set<Integer> unsettled;
private int number_of_nodes;
private int adjacencyMatrix[][];
public DijkstraAlgorithmSet(int number_of_nodes)
{
this.number_of_nodes = number_of_nodes;
distances = new int[number_of_nodes + 1];
settled = new HashSet<Integer>();
unsettled = new HashSet<Integer>();
adjacencyMatrix = new int[number_of_nodes + 1][number_of_nodes + 1];
}
public void dijkstra_algorithm(int adjacency_matrix[][], int source)
{
int evaluationNode;
for (int i = 1; i <= number_of_nodes; i++)
for (int j = 1; j <= number_of_nodes; j++)
adjacencyMatrix[i][j] = adjacency_matrix[i][j];
for (int i = 1; i <= number_of_nodes; i++)
{
distances[i] = Integer.MAX_VALUE;
}
unsettled.add(source);
distances[source] = 0;
while (!unsettled.isEmpty())
{
evaluationNode = getNodeWithMinimumDistanceFromUnsettled();
unsettled.remove(evaluationNode);
settled.add(evaluationNode);
evaluateNeighbours(evaluationNode);
} }
private int getNodeWithMinimumDistanceFromUnsettled()
{
int min ;
int node = 0;
Iterator<Integer> iterator = unsettled.iterator();
node = iterator.next();
min = distances[node];
for (int i = 1; i <= distances.length; i++)
{
if (unsettled.contains(i))
{
if (distances[i] <= min)
{
min = distances[i];
node = i;
}
}
}
return node;
}
private void evaluateNeighbours(int evaluationNode)
{
int edgeDistance = -1;
int newDistance = -1;
for (int destinationNode = 1; destinationNode <= number_of_nodes; destinationNode++)
{
if (!settled.contains(destinationNode))
{
if (adjacencyMatrix[evaluationNode][destinationNode] != Integer.MAX_VALUE)
{
edgeDistance = adjacencyMatrix[evaluationNode][destinationNode];
newDistance = distances[evaluationNode] + edgeDistance;
if (newDistance < distances[destinationNode])
{
distances[destinationNode] = newDistance;
}
unsettled.add(destinationNode);
}
}
}
}
public static void main(String... arg)
{
int adjacency_matrix[][];
int number_of_vertices;
int source = 0;
Scanner scan = new Scanner(System.in);
try
{
System.out.println("Enter the number of vertices");
number_of_vertices = scan.nextInt();
adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];

System.out.println("Enter the Weighted Matrix for the graph");


for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
adjacency_matrix[i][j] = scan.nextInt();
if (i == j)
{
adjacency_matrix[i][j] = 0;
continue;
}
if (adjacency_matrix[i][j] == 0)
{
adjacency_matrix[i][j] = Integer.MAX_VALUE;
}
}
}
System.out.println("Enter the source ");
source = scan.nextInt();
DijkstraAlgorithmSet dijkstrasAlgorithm = new DijkstraAlgorithmSet(number_of_vertices);
dijkstrasAlgorithm.dijkstra_algorithm(adjacency_matrix, source);
System.out.println("The Shorted Path to all nodes are ");
for (int i = 1; i <= dijkstrasAlgorithm.distances.length - 1; i++)
{
System.out.println(source + " to " + i + " is "+ dijkstrasAlgorithm.distances[i]);
}
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input Format");
}
scan.close();
}
}
ROUTING OUTPUT:
RESULT:
Thus the program for routing algorithm has been executed and output verified
Ex.No.: 5

BROADCASTING

AIM:
To write a java program to implement Broadcasting.

SERVER SIDE
import java.io.*;
import java.net.*;
class serverbroad
{
public static void main(String args[]) throws Exception
{
DatagramSocket ds=new DatagramSocket(1999);
InetAddress home=InetAddress.getByName("234.1.1.1");
DataInputStream dis=new DataInputStream(System.in);
int flag=0;
while(true)
{
if(flag==1)
ds.close();
else
{
System.out.println("Enter ur msg: ");
String msg=dis.readLine();
byte b[]=msg.getBytes();
DatagramPacket dp=new DatagramPacket(b,b.length,home,6789);
ds.send(dp);
if(msg.equals("quit"))
{
flag=1;
}}
}
}
}
SERVER SIDE OUTPUT:

CLIENT SIDE

import java.io.*;
import java.net.*;
class clientbroad1
{
public static void main(String args[]) throws Exception
{
MulticastSocket ds=new MulticastSocket(6789);
InetAddress addr=InetAddress.getByName("234.1.1.1");
ds.joinGroup(addr);
while(true)
{
byte b[]=new byte[25];
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
String rec=new String(dp.getData());
System.out.println("Msg from server: "+rec);
}
}
}
import java.io.*;
import java.net.*;
class clientbroad2
{
public static void main(String args[]) throws Exception
{
MulticastSocket ds=new MulticastSocket(6789);
InetAddress addr=InetAddress.getByName("234.1.1.1");
ds.joinGroup(addr);
while(true)
{
byte b[]=new byte[25];
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
String rec=new String(dp.getData());
System.out.println("Msg from server: "+rec);
}
}
}

CLIDE SIDE OUTPUT:


RESULT:
Thus the broadcasting program has been executed and the output was verified.
Ex.No.: 6

MULTICASTING
AIM:
To write a java program to implement multicasting.
MULTICASTING

import java.io.*;
import java.net.*;
public class Multiuser
{
public static void main(String args[])throws Exception
{
String ch,ip,rd;
DataInputStream dis=new DataInputStream(System.in);
System.out.println("please enter the class d ip address to form group or join the group");
ip=dis.readLine();
InetAddress group=InetAddress.getByName(ip);
System.out.println("group");
MulticastSocket s=new MulticastSocket(6789);
s.joinGroup(group);
System.out.println("welcome to the group"+ip);
do
{
do
{
System.out.println("do u need to send message to a group(yes or no) ");
ch=dis.readLine();
if(ch.equals("yes"))
{
System.out.println("enter the message");
String msg=dis.readLine();
DatagramPacket hi=new DatagramPacket(msg.getBytes(),msg.length(),group,6789);
s.send(hi);
byte[] buf=new byte[50];
DatagramPacket recv=new DatagramPacket(buf,buf.length);
s.receive(recv);
int m=(recv.getLength());
ch=new String(recv.getData());
StringBuffer l=new StringBuffer(ch);
l.setLength(m);
System.out.println("the msg send to the group is"+l);
System.out.println("do u want to send more msg(yes or no) ");
}
else
break;
}while(ch.equals("yes"));
do
{
System.out.println("you are the listening mode ");
byte[] buf=new byte[50];
DatagramPacket recv=new DatagramPacket(buf,buf.length);
s.receive(recv);
int d=(recv.getLength());
ch=new String(recv.getData());
StringBuffer f=new StringBuffer(ch);
f.setLength(d);
System.out.println("the message received from group"+f);
System.out.println("Continue in listening mode(yes or no) ");
rd=dis.readLine();
}while(rd.equals("yes"));
}while(true);
}
}

OUTPUT:
RESULT:
Thus the multicasting program has been executed and the output was verified.

Ex.No.: 7 ARP Using TCP

AIM
To implement Address Resolution Protocol.

CLIENT:
import java.io.*;
import java.net.*;
class ArpClient
{
public static void main(String args[])throws IOException
{
try
{
Socket ss=new Socket(InetAddress.getLocalHost(),1100);
PrintStream ps=new PrintStream(ss.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String ip;
System.out.println("Enter the IPADDRESS:");
ip=br.readLine();
ps.println(ip);
String str,data;
BufferedReader br2=new BufferedReader(new InputStreamReader(ss.getInputStream()));
System.out.println("ARP From Server::");
do
{
str=br2.readLine();
System.out.println(str);
}
while(!(str.equalsIgnoreCase("end")));
}
catch(IOException e)
{
System.out.println("Error"+e);
}}}

SERVER:
import java.io.*;
import java.net.*;
class ArpServer
{
public static void main(String args[])throws IOException
{
try
{
ServerSocket ss=new ServerSocket(1100);
Socket s=ss.accept();
PrintStream ps=new PrintStream(s.getOutputStream());
BufferedReader br1=new BufferedReader(new InputStreamReader(s.getInputStream()));
String ip; ip=br1.readLine();
Runtime r=Runtime.getRuntime();
Process p=r.exec("arp -a "+ip);
BufferedReader br2=new BufferedReader(new InputStreamReader(p.getInputStream()));
String str;
while((str=br2.readLine())!=null)
{
ps.println(str);
}}
catch(IOException e)
{
System.out.println("Error"+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

Result :
Thus the program for implementing to display simulating ARP.
Ex.No.: 8 TWO WAY COMMUNICATION USING TCP/IP

AIM:
To write a java program to implement one way communication using TCP/IP.
Program:

SERVER:
import java.net.Socket;
import java.io.IOException;
import java.net.ServerSocket;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
class TwoServerTCP
{
public static void main(String args[]) throws IOException
{
try
{
ServerSocket ss=new ServerSocket(60);
Socket sc;
System.out.println("Waiting for client......");
sc=ss.accept();
BufferedReader in=new BufferedReader(new
InputStreamReader(sc.getInputStream()));
PrintWriter out=new PrintWriter(sc.getOutputStream(),true);
BufferedReader stdin =new BufferedReader(new
InputStreamReader(System.in));
String a,b;
System.out.println("Connected to Client..."+sc);
while(true)
{
System.out.println("enter the message for client:");
a=stdin.readLine();
if(a.equals("exit"))
break;
out.flush();
out.println(a);
b=in.readLine();
System.out.println("Client says:"+b);
}
sc.close();
}
catch(Exception e)
{
System.out.println("Error"+e);
}}}
OUTPUT:
CLIENT:
import java.net.Socket;
import java.io.IOException;
import java.net.ServerSocket;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
class TwoClientTCP
{
public static void main(String args[]) throws IOException
{
try
{
Socket sc;
if(args.length!=1)
sc=new Socket("LocalHost",60);
else
sc=new Socket("SYSTEM",60);
BufferedReader in=new BufferedReader(new
InputStreamReader(sc.getInputStream()));
PrintWriter out=new PrintWriter(sc.getOutputStream(),true);
BufferedReader stdin =new BufferedReader(new
InputStreamReader(System.in));
String a,b;
System.out.println("waiting for Server...");
System.out.println("Connected to Server..........."+sc);
b=in.readLine();
System.out.println("Server Says:"+b);
while(true)
{
System.out.println("enter the message for Server:");
a=stdin.readLine();
if(a.equals("exit"))
break;
out.println(a);
b=in.readLine();
System.out.print("Server says:"+b);
}
sc.close();
}
catch(Exception e)
{
System.out.println("error"+e);
}
}
}
OUTPUT:
RESULT:
Thus the two way communication using TCP program has been executed and the output
was verified.
Ex.No.: 9 TWO WAY COMMUNICATION USING UDP

AIM
To write a java program to implement two way communication using UDP.
SERVER:
import java.io.*;
import java.net.*;
import java.net.Socket;
import java.io.IOException;
import java.net.ServerSocket;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.InetAddress;
class Twoserverudp
{
public static void main(String args[])throws IOException
{
while(true)
{
try
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("enter the message for client");
String s=br.readLine();
if(s.equals("exit"))
{
break;
}
byte b[]=s.getBytes();
InetAddress ia=InetAddress.getLocalHost();
DatagramSocket ds=new DatagramSocket();
DatagramPacket dp=new DatagramPacket(b,b.length,ia,2000);
ds.send(dp);
ds.close();
byte b1[]=new byte[100];
DatagramSocket ds1=new DatagramSocket(2001);
DatagramPacket dp1=new DatagramPacket(b1,b1.length);
ds1.receive(dp1);
String s1=new String(dp1.getData());
System.out.print("message from client:");
System.out.println(s1);
ds1.close();
}
catch(Exception e)
{
System.out.println("error");
}
}}}
OUTPUT:

CLIENT:
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.io.IOException;
import java.net.*;
class Twoclientudp
{
public static void main(String args[])throws IOException
{
while(true)
{
try
{
int port=2000;
byte b[]=new byte[100];
DatagramSocket ds=new DatagramSocket(port);
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
String s=new String(dp.getData());
System.out.print("message from server:");
System.out.println(s);
ds.close();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("enter the message for Server:");
String s1=br.readLine();
if(s1.equals("quit"))
{
break;
}
byte b1[]=s1.getBytes();
InetAddress ia=InetAddress.getLocalHost();
DatagramSocket ds1=new DatagramSocket();
DatagramPacket dp1=new DatagramPacket(b1,b1.length,ia,2001);
ds1.send(dp1);
ds1.close();
}
catch(Exception e)
{
System.out.println("error"); }}}}

OUTPUT:
RESULT:
Thus the two way communication using UDP program has been executed and the output
was verified.

Ex.No.: 10 DOMAIN NAME SERVICE(DNS)

Aim
To write a java program for DNS application program

Program :
// UDP DNS Server
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 –
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
$ 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
$ 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

RESULT:
Thus the DNS program has been executed and the output was verified.

S-ar putea să vă placă și