You are on page 1of 52

ANNA UNIVERSITY

SAKTHI ENGINEERING COLLEGE


THIRUNINRAVUR-602 024

DEPARTMENT
OF
INFORMATION TECHNOLOGY

NETWORK LAB-(CS 1305)

NAME :___________________
REG NO :___________________
YEAR :___________________
SEM :___________________
ANNA UNIVERSITY
SAKTHI ENGINEERING COLLEGE

REG
NO:______________

SEM:_______________

NAME:________________ COURSE: B.TECH(I.T)

This is certified to be the bonafide record of work done by the student in the
CS1305 NETWORK LAB of Sakthi Engineering College during
Jan’ 09 to may’ 09.

HEAD OF THE DEPARTMENT STAFF-IN-CHARGE

SUBMITTED FOR PRACTICAL EXAMINATION HELD ON__________

INTERNAL EXAMINER EXTERNAL EXAMINER


INDEX

PAGE
EXPNO DATE EXPERIMENT NAME SIGNATURE
NO

1 LINUX COMMANDS

2 SOCKET CREATION

SIMULATION OF
3
ARP/RARP

4 BIT STUFFING

CYCLIC REDUNDANCY
5
CHECK
SIMULATION OF SLIDING
6
WINDOW PROTOCOL

7 DOMAIN NAME SYSTEM

8 FTP USING RS232

OPEN SHORTEST PATH


9
FIRST ALGORITHM

10 GETTING HOST NAME

STUDY OF NETWORK
11
SIMULATOR NS2
LINUX COMMANDS

1) Mkdir: This is used to create a directory.


Syntax: $mkdir directory name
Example: $mkdir dine

2) cd: This is used to change the directory.


Syntax: $cd directory name
Example: $cd dine

3) rm: This is used to remove the directory.


Syntax: $rm directory name
Example: $rm dine

4) cp: This is used to copy content from one file to another file.
Syntax: $cp oldfile newfile
Example: $cp fcfs fc.c

5) clear: This is used to clear the screen.


Syntax: $clear

6) move: This is used to move the file from source to destination.


Syntax: $mv oldfile newfile
Example: $mv scfs sc.c

7) ls: This command is used to view the content of directory.


Syntaz: $ls

8) wc: This command is used to count the no of words (or) characters in the file.
Syntax: $wc filename
Example: $wc dine.c

9) date: This command is used to display the current date.


Syntax: $date
Example: $13.11.2008

10) echo: This command is used to display the string printed in the command line.
Syntax: $echo “string”
Example: $echo “hello”

11)who: This is used to display the information about the current user.
Syntax:$who
12) grep: This is used to display the information about the current user.
Syntax: $grep[option] pattern filename
Example: $cat>count
ab
ac
ba
grep”a” count
ab
ac

13) cat: This command is used to create a file.


Syntax: $cat>filename
Example: $cat>count

14) sort: This command is used to sort the content of the file name.
Syntax: $sort file name
Example: $cat>mm
sa
co
en
$sort mm
Output: co
en
sa
15) read command: This command is used to read the variables.
Syntax: read varname
Example: read a
Result:

Thus the basic Linux commands has been executed and verified.
SOCKET CREATION

AIM:

To write a c program to create a socket.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: Declare the variable sockfd1,sockfd2.

Step 4: Define .sockfd1 as socket which transmits data as a stream of bytes.

Step 5: Define .sockfd2 as socket which transmits data as a datagram.

Step 6: Display the file discription value.

Step 7: If the sock fd2= -1 then it is not created .

Step 8: Stop the program excution.


SOCKET CREATION
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int sockfd1,sockfd2;
sockfd1=socket(AF_INET,SOCK_STREAM,0);
sockfd2=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd==1)
{
printf("socket 1is not created");
}
else
{
printf("socket 1 created and\t socket1 file descriptor value is %d\n",sockfd1);
if(sockfd2==-1)
{
printf("socket2 creation error");
}
else
{
printf("socket2 created and \t socket2 descriptor value is%d\n",sockfd2);
}
}
}
Output:

Socket 1 created and socket1 file descriptor value is 3

Socket2 created and socket2 descriptor value is4


Result:

Thus the c program for implementing socket creation has been executed and verified.
SIMULATION OF ARP/RARP

AIM:

To write a c program to create a socket.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: Declare the function properties for .ARP/RARP.

Step 4: Create a file and with that file declare the physical and logical address.

Step 5: Get the choice of operation. If it is ARP show the logical address and if its RARP
display the physical address.
.

Step 6: Otherwise exit from it.

Step 7: Stop the program excution.


ARP/RARP

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct file
{
char phyadd[30];
char logadd[30];
}f[15];
File *fp
int n;
void arp(char *ladd)
{
int i;
for(i=0;i<n;i++)
{
if(strcmp,(ladd,f[i].logadd))
{
printf("\n corresponding logical address is",f[i].phyadd);
break;
}
if(i==n)
printf("the address is not found");
}
}
void rarp(char *padd)
{
int i;
for(i=0;i<n;i++)
{
if(!strcmp,(padd,f[i].phyadd))
{
printf("\n corresponding physical address is",f[i].logadd);
break;
}
}
if(i==n)
printf("the address is not found");
}

int main()
{
int i=0,ch;
char padd[30],ladd[30];
fp=fopen("data.txt","r");
if(fp=null)
{
pritnf(" \n file not found");
exit(0);
}
while(!feof(fp))
{
fscanf(fp,"%s%s",f[i].phyadd,f[i].logadd);
i++;
}
fclose(fp);
n=i;
while(1);
{
printf("\n 1.ARP \n 2.RARP \n 3.EXIT");
printf(" enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the physical address");
scanf("%s",ladd);
arp(ladd);
break;
case 2:
printf(" enter the logical address");
scanf("%s",padd);
rarp(padd);
break;
case 3:
exit(1);
break;
}
}
return(0);
}
Output:

1. ARP
2. RARP
3. EXIT

Enter your choice: 1

Enter the physical address: 50

Corresponding logical address is: dsp

Enter your choice: 2

Enter the logical address : arun

Corresponding physical address is: 20

Enter your choice: 3


Result:

Thus the c program of ARP/RARP is verified and successfully.


BIT STUFFING

AIM:

To write a c program to implement bit stuffing for the given bits of information.

ALGORITHM:

Step 1: Start the program execution.

Step 2: Declare the variable and obtain the information size and need it.

Step 3: Create an input file and copy the information into it.

Step 4: Create another file with read mode and check for the condition.

Step 5: Check whether five consecutive is have appeared, if appeared so print 0 as


information in the copied file.

Step 6: Finally print the information bit in the created file.

Step 7: Close all the created files.

Step 8: Stop the program execution.


BIT STUFFING
#include<stdio.h>
main()
{
int n=0,i,size;
char ch,b[100];
FILE*ed;
FILE*es;
printf("enter the bit information size:");
scanf("%d",&size);
printf("enter the bit information:");
for(i=0;i<size;i++)
{
scanf("%c",&b[i]);
}
ed=fopen("sarinput.txt","wt");
i=0;
for(i=0;i<size;i++)
{
fputc(b[i],ed);
}
fclose(ed);
ed=fopen("sarinput.txt","rt");
if(ed==NULL)
printf("\n error ");
es=fopen("saroutput.txt","wt");
ch=getc(ed);
while(ch!=EOF)
{
fputc(ch,es);
if(ch='1')
n++;
else
n=0;
if(n==5)
{
fputc('0',es);
n=0;
}
ch=getc(ed);
}
printf("\n original data");
fclose(ed);
fclose(es);
ed=fopen("sarinput.txt","rbt");
ed=fopen("saroutput","rbt");
ch=getc(ed);
while(ch!=EOF)
{
printf("%c",ch);
ch=getc(ed);
}
printf("\n data after stuffing");
ch=getc(es);
while(ch!=EOF)
{
printf("%c",ch);
ch=getc(es);
}
fclose(ed);
fclose(es);
}
output:

Enter the bit information size:6

Enter the bit information:111111

original data:
111111
data after stuffing:
1111101
Result:

Thus the program for bit stuffing is verified and written successfully.
CYCLIC REDUNDANCY CHECK

AIM:

To write a c program for cyclic redundancy check.

ALGORITHM:

Step 1: Start the program.

Step 2: Enter the dividend and the divisor.

Step 3: Perform the binary division and the CRC remainder is found.

Step 4: Append the CRC remainder with the original data.

Step 5: Stop the program.


CYCLIC REDUNDANCY CHECK
#include<stdio.h>
#include<string.h>
int main()
{
int i,j=1,k=0,m=0,divlen,len;
char orgdiv[20],dividend[20],divisor[20],zeros[10],crc[10];
printf("enter the dividend:");
scanf("%s",dividend);
printf("enter the divisor:");
scanf("%s",divisor);
divlen=strlen(divisor);
for(i=0;i<divlen-1;i++)
zeros[i]='0';
zeros[i]='\0';
strcpy(orgdiv,dividend);
strcat(dividend,zeros);
len=strlen(dividend);
while(i<=(len-divlen+1))
{
if(dividend[m]=='1')
{
for(i=1;i<divlen;i++)
{
if(dividend[i]==divisor[i])
crc[i]='0';
else
crc[i]='1';
dividend[m]=crc[i];
m++;
}
}
else
{
for(i=1;i<divlen;i++)
{
if(dividend[i]=='0')
crc[i]='0';
else
crc[i]='1';
dividend[m]=crc[i];
m++;
}}

else
{
for(i=0;i<divlen;i++)
{
if(dividend[i]='0')
crc[i]='0';
else
crc[i]='1';
dividend[m]=crc[i];
m++;
}
}
for(k=m;dividend[k]!='\0';k++)
dividend[k]=dividend[k+1];
m=0;
j++;
}
for(j=0;j<divlen-1;j++)
crc[j]=dividend[j];
crc[j]='\0';
printf("\n the crc is: %s",crc);
strcat(orgdiv,crc);
printf("the append data is p: %s",orgdiv);
}
Output:

Enter the dividend: 100100

Enter the divisor : 1101

The CRC is : 001

The appendend data is P : 100100001


Result:

Thus the program for cyclic redundancy check is verified and written successfully.
SIMULATION OF SLIDING WINDOW PROTOCOL

AIM:

To implement the sliding window server and client using c program.

ALGORITHM:

Step 1: Start the program.

Step 2: Declare the necessary header files and variables.

Step 3: Create a socket, if not created print socket error.

Step 4: Connect the server by using the client function.

Step 5: Enter the sequence number to be sent and the current sender window.

Step 6: Stop the program.


SLIDING WINDOW
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<error.h>
#include<netdb.h>
int main()
{
int data[5],rec[5];
int m,n,a,i,c=0;
char r1[5];
printf("enter the choice:\n");
printf("1.select reject \n");
printf("2.go backn \n");
scanf("%d",&a);
printf("enter the size of the data");
scanf("%d",&n);
printf("enter the data one by one:");
for(i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
switch(a)
{
case 1:
{
printf("data is ready to send:\n");
for(i=0;i<n;i++)
{
rec[i]=data[i];
printf("%d is sent \n",data[i]);
scanf("%s",r1);
if(strcmp(r1,"n"))
printf("ack is rec %d n",i+1);
else
{
c++;
}
}
if(c!=0)
{
printf("enter the position of the data to be sent again:");
scanf("%d",&m);
while(m>n)
{

printf("\n wrong choice");


printf("\n enter the position of the data again");
scanf("%d",&m);
}
if(a==1)
{
printf("/n%d is sent again\n",data[m-1]);
}
}
return 0;
}
case 2:
{
for(i=0;i<n;i++)
{
rec[i]=data[i];
printf("%d is sent\n",data[i]);
scanf("%s",r1);
if(strcmp(r1,"n"))
printf("ack is rec%d\n",i);
else
{
c++;
}
if(c!=0){
printf("enter the starting position of data to be sent again");
scanf("%d",&m);
for(i=m-1;i<n;i++)
{
rec[i]=data[i];
printf("%d is sent\n",data[i]);
scanf("%s",r1);
}
}
}
}
}
return(0);
}
output:
enter the choice:
1.select reject
2.go backn
1
enter the size of the data: 3
enter the data one by one:
1
2
3
data is ready to send:
1 is sent
a
ack is rec 1
2 is sent
n
3 is sent
a
ack is rec 1
enter the position of the data to be sent again:2
2 is sent again
enter the choice:
1.select reject
2.go backn
2
enter the size of the data3
enter the data one by one:
3
4
5
3 is sent
a
ack is rec 0
4 is sent
n
enter the starting position of data to be sent again2
4 is sent
a
5 is sent
a
Result:

Thus the program for sliding window protocol is verified and written successfully.
DOMAIN NAME SYSTEM

AIM:

To perform DNS server and client program.

ALGORITHM:

Step 1: start the program excution.

Step 2: include header file,define max data size and port.

Step 3: enter the domain name.

Step 4: get the domain name.

Step 5: if it is not found then error on domain name.

Step 6: stop the program excution.


DNS SERVER
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<error.h>
#include<netdb.h>
#define MAX 100
#define ipaddr"127.0.0.1"
#define port 4001
typedef struct sockaddr SA;
struct dns
{
char dname[25];
char ipaddr[25];
};
int main()
{
int cfd;
char buff[MAX];
struct socket_in server;
struct dns r;
cfd=socket(AF_INET<SOCK_STREAM,0);
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(port);
inet_pton(AF_INET,ipaddr,&server.sin_addr);
connect((cfd,(SA*)&server ,sizeof(server));
printf("enter the domain name");
scanf("%s",r,dname);
write(cfd,&r,sizeof(struct dns));
bzero(&r,sizeof(struct dns ));
read(cfd,&r,sizeof(struct dns));
printf("ipaddr = %s",r, ipaddr);
close(cfd);
}
DNS CLIENT
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
int main()
{
int csd,cport,len;
char sendmsg[20],recvmsg[20],rc[20];
struct socket_in servaddr;
printf(“\n enter the port address”);
scanf(“%d”,&cport);
csd=socket(AF_INET<SOCK_STREAM,0);
if(csd<0)
printf(“ERROR:socket creation failed”);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(port);
if(connect(csd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
printf(“\n cannot connect”);
else
printf(“ \n connected”);
printf("enter the domain name");
scanf("%s",sendmsg);
sendmsg(csd,sendmsg,20,0);
recvmsg(csd,rc,20,0);
printf("\n the corresponding ip address for given domain name is:”);
printf(“%s”,rc);}
Output:

Dns server output:

dsp@dsp-desktop:~$ cc dnsser.c
dsp@dsp-desktop:~$ ./a.out
enter the domain name: www.annauniv.edu
ipaddr= 10.0.0.1

Dns client output:

dsp@dsp-desktop:~$ cc dnscli.c
dsp@dsp-desktop:~$ ./a.out

domain name = www.annauniv.edu


Result:

Thus the program for domain name system is verified and written successfully.
FTP USING RS232

AIM:

To write a c++ program to implement FTP using RS232.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: Declare the varible and file pointer.

Step 4: Use the switch statement to get the operation to be performed.

Step 5: Check whether the data that has to be send is ready.

Step 6: If data is send print “data ready”. If not send an error repeat.

Step 7:Display the text that is present in the file name mentioned.

Step 8: Stop the program.


RS232
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<iostream.h>
#include<fstream.h>
void main()
{
FILE *fp;
char fname[23];
union REGS,a,b;
char al;
int op;
clrscr();
a.h.ah=0*00;
a.x.dx=0;
a.h.dl=0;
int 86(0*11,&a,&b);
printf("\n1.send \n2.recevie \n3.exit \n");
do
{
printf("Enter the option:");
scanf("%d",&op);
switch(op)
{
case 1:
printf("enter the fname:");
cin>>fname;
fp=fopen(fname,"r");
if(fp=NULL)
{
printf("Error");
getch();
exit(0);
}
a.b.ah=0*01;
a.x.dx=0;
while(!feof(fp))
{
ch=getc(fp);
a.h.al=ch;
int 86(0*14,&a,&b);
}
if(a.h.ah==0)
printf("Error");
else
printf("Data ready");
fclose(fp);
break;
case 2:
fp=fopen(fname,"r");
if(fp==NULL)
{
printf("\nError");
getch();
exit(0);
}
while(!feof(fp))
{
ch=getc(fp);
cout<<ch;
}
fclose(fp);
cout<<ch;
}
fclose(fp);
break;
case 3:
exit(0);
break;
default :
printf("Enter the correct option");
}
}
while(op!=3)
getch();
}
Output:

1.sender
2.receiver
3.exit

Enter the option : 1

Enter the file name : dsp.txt

Data ready

Enter the option : 2

This is the content of the file

Hai how are you

Enter the option : 3


Result:

Thus the program for FTP using RS232 is verified and written successfully.
OPEN SHORTEST PATH FIRST ALGORITHM

AIM:

To write a c++ program for open shortest path between server and destination
Node.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: Declare the variable functions.

Step 4: Get the Number of nodes and the cost of the edge between various nodes.

Step 5: Enter the source and destination nodes.

Step 6: Find the shortest path between source and destination node.

Step 7: Stop the program excution.


OSPF
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class path
{
int a[10][10],c[10[10],key[10][10],num,min,i,j,k;
public:
void findpath();
void read();
void output(int,int);
void out(int i, int j);
};
void path::read()
{
cout<<”Number of nodes:”;
cin>>num;
for(i=1;i<=num;i++)
for(j=1;j<=nump;j++)
{
if(i==j)
a[i][j]=0;
else
{
cout<<”The cost for[“<<i<<”,”<<j<<”];
cin>>a[i][j];
}
c[i][j]=a[i][j];
key[i][j]=0;
}
}
void path::findpath()
{
int t1,t2,t3;
for(k=1;k<=num;k++)
for(i=1;i<=num;i++)
for(j=1;j<=num;j++)
{
t1=c[i][k];
t2=c[k][j];
t3=c[i][j];
if(t1!=0 &&t2!=0 && (t3==0||t1+t2<t3))
{
c[i][j]=t1+t2;
key[i][j]=k;
}
}
}
void path::output(int i, int j)
{
min=0;
if(c[i][j]==0)
{
cout<<”no path exist”;
return;
}
else
{
cout<<”The path is :”<<i;
out(i,j);
cout<<end1<<”cost is :”<<min;
cout<<”\n”;
}
}
void path :: out(int i, int j)
{
if(i==j)
return;
if(key[i][j]==0)
{
cout<<”->”<<j;
min+=a[i][j];
}
else
{
out(i,key[i][j]);
out(key[i][j],j);
}
}
void main()
{
clrscr();
int ch=1,n1,n2;
path p;
p.read();
p.findpath();
cout<<end1<<”1.Shortest path”<<end1;
cout<<”2.Exit”<<end1;
while(ch!=2)
{
cout<<end1<<”choice…..”;
cin>>ch;
switch(ch)
{
case 1:
cout<<”enter the source node”;
cin>>n1;
cout<<”enter the destination node”;
cin>>n2;
p.output(n1,n2);
break;
case 2:
exit(0);
}
}
getch();
}
Output:

Number of nodes : 3

The cost for [1,2] : 1

The cost for [1,3] : 4

The cost for [2,1] : 2

The cost for [2,3] : 2

The cost for [3,1] : 2

The cost for [3,2] : 1

1.shortest path
2.exit

Choice……….. 1

Enter the source code : 1


Enter the destination code : 3
The path is 1 -> 2 ->3
Cost is 3

Choice……….. 2
Result:

Thus the program for open shortest path specification is verified and written
successfully.
GETTING HOST NAME

AIM:

To write a c program for open shortest path between server and destination
Node.

ALGORITHM:

Step 1: Start the program.

Step 2: Include the necessary header files.

Step 3: Declare the structures and varible.

Step 4: Correct the value of parameter.

Step 5: If the host name of the parameter value is equal to NULL then print error
message.

Step 6: Else the print the host name.

Step 7: Print the ip address.

Step 8: Stop the program excution.


GETTING HOST NAME
#include<stdio.h>
#include<unistd.h>
#include<netdb.h>
int main()
{
char hostname[100];
int ret=gethostname(hostname,sizeof(hostname));
printf("hostname:%s \n",hostname);
struct hostent *ht=gethostbyname(hostname);
int i=0;
while(ht->h_addr_list[i]!=NULL)
{
printf("Address[%d]: \n",i,inet_ntoa(*((struct in_addr*)ht->h_addr_list[i])));
i++;
}
return(0);
}
output:

dsp@dsp-desktop:~$ ./a.out

hostname:dsp-desktop

Address[0]: 127.0.0.1
Result:

Thus the program for getting host name is verified and written successfully.
NETWORK SIMULATOR (NS2)

A discrete event simulator targeted at networking research. Ns provides substantial support for
simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite)
networks. Simulation of protocols (TCP, routing, multicast...) over networks (wireless,
Wired, satellite).

Ns began as a variant of the REAL network simulator in 1989 and have evolved substantially
over the past few years. In 1995 ns development was supported by DARPA through the VINT project
at LBL, Xerox PARC, UCB, and USC/ISI. Currently ns development is support through DARPA with
SAMAN and through NSF with CONSER, both in collaboration with other researchers including
ACIRI. Ns have always included substantial contributions from other researchers, including wireless
code from the UCB Daedelus and CMU Monarch projects and Sun Microsystems. For documentation
on recent changes, see the version 2 change log.

While we have considerable confidence in ns, ns is not a polished and finished product, but the
result of an on-going effort of research and development. In particular, bugs in the software are still
being discovered and corrected. Users of ns are responsible for verifying for themselves that their
simulations are not invalidated by bugs. We are working to help the user with this by significantly
expanding and automating the validation tests and demos.

Similarly, users are responsible for verifying for themselves that their simulations are not
invalidated because the model implemented in the simulator is not the model that they were expecting.
The ongoing Ns Manual should help in this process.

NS is Object Oriented Tcl (OTcl) script interpreter that has a simulation event scheduler and
network component object libraries, and network setup module libraries. To run a simulation network,
a user should write an OTcl script that initiates an event scheduler, sets up the network topology using
the network objects and the plumbing functions in the library, and tells traffic sources when to start
and stop transmitting packets through the event scheduler. Another major component of NS beside
network objects is the event scheduler. An event is NS is a packet ID that is unique for a packet with
scheduled time and the pointer to an object that handles the event.

All the network components that need to spend some simulation time handling a packet (i.e.
need a delay) use the event scheduler by issuing an event for the packet and waiting for the vent to be
fired to itself before doing further action handling the packet. Another use of an event scheduler is
timer.

It can generally implemented in split language programming using C++ and TCL.
SPLIT-LANGUAGE PROGRAMMING

Ns separate the data path implementation from control path implementations. In order to
reduce packet and event processing time, the event scheduler and the basic network component objects
in the data path are written and compiled using C++.

IMPLEMENTATION OF NODE AND LINK

General, architecture of NS a user can be thought of standing at the left bottom corner,
designing and running simulations in Tcl using the simulator objects in the OTcl library. The event
scheduler and most of the network components are implemented in C++ and available to OTcl through
an OTcl linkage that is implemented using tclcl. When a simulation is finished, NS produces one or
more text-based output that files that contain detailed simulation data. The data can be used for
simulation analysis or as and input to a graphical simulation display too called Network Animator
(NAM) that is developed as a part of VINT project.

You might also like