NO: 1(a)
AIM
To create a client server program to get time and date information using the TCP protocol date time
server.
ALGORITHM
Server
Client
PROGRAM CODE
Server
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<time.h>
#include<string.h>
#define MAX_BUF 30
#define PORT 2100
int main()
{int sersoc,clisoc;
int conn,len,wri;
char str[MAX_BUF];
pid_t pid;
time_t ticks;
socklen_t clilen;
struct sockaddr_in servaddr;
struct sockaddr_in cliaddr;
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(PORT);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if((sersoc=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("Socket Error");
exit(0);
}
if(bind(sersoc,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
{
perror("Bind Error");
exit(0);
}
listen(sersoc,10);
for(;;)
{len=sizeof(cliaddr);
conn=(accept(sersoc,(struct sockaddr *)&clisoc,&len));
if((pid=fork())==0)
{close(sersoc);
ticks=time(NULL);
strcpy(str,ctime(&ticks));
if((wri==(write(conn,str,sizeof(str),0)))<0)
{printf("Write Error");
exit(0);
}
close(conn);
exit(0);}
close(conn);}
close(sersoc);
return 0;
}
Client
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<time.h>
#include<arpa/inet.h>
#define CLI_PORT 2100
#define BUFF_SIZE 30
int main(int argc,char **argv)
{int clisoc,IPPORT_IP,re;
char recbuff[BUFF_SIZE];
struct sockaddr_in cliaddr;
bzero(&cliaddr,sizeof(cliaddr));
cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(2100);
cliaddr.sin_addr.s_addr=inet_addr(argv[1]);
if((clisoc=socket(AF_INET,SOCK_STREAM,0))<0)
{perror("Socket Error");
exit(0);}
if((connect(clisoc,(struct sockaddr *)&cliaddr,sizeof(cliaddr)))<0)
{perror("connect error");
exit(0);}
if((re=(read(clisoc,recbuff,sizeof(recbuff),0)))<0)
{printf("Read Error");
exit(0);}
printf("The current date and time :%s \n",recbuff);
close(clisoc);
return 0;
}
OUTPUT
Server
Client
UDP SOCKETS
AIM
ALGORITHM
Client
Server
3. Receive the message from client and resend the message to client.
Client
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<arpa/inet.h>
#include<stdio.h>
int sockfd;
int n;
socklen_t len;
char sendline[1024],recvline[1024];
scanf("%s",sendline);
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(4560);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
len=sizeof(servaddr);
recvline[n]=0;
printf("%s",recvline);
return 0;
Server
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<netdb.h>
#include<arpa/inet.h>
int sockfd;
int n;
socklen_t len;
char msg[1024];
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htons(INADDR_ANY);
servaddr.sin_port=htons(4560);
for(;;)
len=sizeof(cliaddr);
printf("%s",msg);
if(n<0)
perror("send error");
return 0;
OUTPUT
./a.out udpserver.c
./a.out udpclient.c
Hi
Hi
EX.NO: 2
SLIDING WINDOW PROTOCOL
AIM
ALGORITHM
1. Initialize the temporary variables to 0 and the size of the sliding window is 8;
2. Get the number of frames.
3. Send each frame to the receiver.
4. The function simulate() and receive() generate random numbers.
5. As each frame is sent the window size is decreased.
6. After all the frames are sent to the receiver , the receiver sends an
acknowledgement.
7. Thus sliding window protocol was demonstrated.
PROGRAM CODE
#include<stdio.h>
#include<stdlib.h>
int main()
{
int temp1,temp2,temp3,temp4,winsize=8,noframes,moreframes;
int receive(int);
int simulate(int);
int i;
temp1=0;
temp2=0;
temp3=0;
temp4=0;
for(i=0;i<=200;i++)
rand();
noframes=5;
printf("\n Number of frames: %d\n",noframes);
moreframes=noframes;
while(moreframes>0)
{
temp1=simulate(winsize);
winsize=temp1;
temp4+=temp1;
if(temp4>=noframes)
for(i=temp3+1;i<=temp4;i++)
printf("\n Sending frame %d",i);
temp2=receive(temp1);
temp3+=temp2;
temp3=noframes;
printf("\n Acknowledgement for the frames upto %d\n",temp3);
moreframes=temp2;
temp4=temp3;
if(winsize<=0)
winsize=8;
}
printf("\nEnd of sliding window protocol\n");
}
int receive(int temp1)
{
int i;
for(i=0;i<100;i++)
rand();
i=rand()%temp1;
return i;
}
int simulate(int winsize)
{
int temp1,i;
for(i=0;i<50;i++)
temp1=rand();
if(temp1==0)
temp1=simulate(winsize);
i=temp1%winsize;
if(i==0)
return winsize;
else
return temp1%winsize;
}
OUTPUT
Number of frame is 5
Sending frame 1
Sending frame 2
Sending frame 3
Sending frame 4
Sending frame 5
AIM
ALGORITHM
Client
4.Write the domain name on the socket using the write() function.
5.When the client recieves the domain name, read it using the read() function.
Server
2.To bind the server to the socket, use the bind() function.
Server
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<stdlib.h>
#include<netdb.h>
int main()
int listenfd,connfd;
socklen_t len;
char host[50],buff[100];
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8900);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
bind(listenfd,(SA *)&servaddr,sizeof(servaddr));
listen(listenfd,10);
for(; ;)
len=sizeof(cliaddr);
connfd=accept(listenfd,(SA *)&cliaddr,&len);
read(connfd,host,sizeof(host));
printf("%s",host);
if((hp=gethostbyname(host))==NULL)
if(inet_ntop(AF_INET,hp->h_addr,buff,sizeof(buff))<=0)
printf("%s",buff);
write(connfd,buff,strlen(buff)+1);
close(connfd);
return 0;
Client
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<netdb.h>
#include<stdlib.h>
socklen_t len;
char buff[100],host[50];
int sockfd;
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8900);
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))<0)
printf("connect error");
scanf("%s",host);
write(sockfd,host,strlen(host)+1);
if(read(sockfd,buff,sizeof(buff))<0)m
printf("read error");
return 0;
}
OUTPUT
Server
Client
AIM
To transfer a file using FTP between the server and the client systems.
ALGORITHM
Server
2. Using socket function, create a socket for server by specifying server port.
Client
3. Allocate buffer use TCP for receiving message from server to client.
Server
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<errno.h>
int main()
int srFd,CIFD,len;
char buf[SIZE],buf1[SIZE];
int n,j;
FILE *fp,*fp1;
srFd=socket(AF_INET,SOCK_STREAM,0);
if(srFd<0)
exit(0);
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(2200);
server.sin_addr.s_addr=htons(INADDR_ANY);
close(srFd);
exit(0);
if(listen(srFd,1)<0)
close(CIFD);
exit(0);
len=sizeof(client);
if(CIFD<0)
close(CIFD);
close(srFd);
exit(0);
bzero(&buf,sizeof(buf));
if((n=recv(CIFD,&buf,SIZE,0))<0)
{
close(srFd);
close(CIFD);
exit(0);
buf[n-1]=NULL;
if((n=send(CIFD,buf,strlen(buf),0))<0)
printf("\n Error");
close(CIFD);
exit(0);
close(srFd);
close(CIFD);
exit(0);
Client
#include<stdio.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<string.h>
#define SIZE 256
int main()
int CIFD;
char buf[SIZE];
int n;
if((CIFD=socket(AF_INET,SOCK_STREAM,0))<0)
exit(0);
bzero(&client,sizeof(client));
client.sin_family=AF_INET;
client.sin_port=htons(2200);
inet_pton(AF_INET,"127.0.0.1",&client.sin_addr);
close(CIFD);
exit(0);
bzero(&buf,sizeof(buf));
if(fgets(buf,SIZE,stdin)==NULL)
{
close(CIFD);
exit(0);
if(send(CIFD,buf,strlen(buf),0)<0)
close(CIFD);
exit(0);
bzero(&buf,sizeof(buf));
if((n=recv(CIFD,buf,SIZE,0))<0)
close(CIFD);
exit(0);
//buf[n]=NULL;
buf[n]=NULL;
close(CIFD);
exit(0);
}
OUTPUT
Client
Connection Enabled
Tps.c
Server
Tps.c
EX.NO.:4(c)
TCP CHAT
AIM
To write a program using TCP chat for establishing communication between the server & client.
ALGORITHM
1. Start.
2. The sender must send the message to the receiver through the network.
4. As and when the receiver receives tne message ‘bye the control shifts to the receiver.
5. The receiver can now create a new message to be sent to the destinstion ie the sender.
PROGRAM CODE
TCP Server
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<strings.h>
{
int sockfd,newsockfd,clength;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
listen(sockfd,5);
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)&cli_addr,&clength);
while(strcmp(buffer,"bye\n")!=0)
bzero(buffer,4096);
read(newsockfd,buffer,4096);
printf("\nclient:%s",buffer);
bzero(buffer,4096);
printf("\nserver:");
fgets(buffer,4096,stdin);
write(newsockfd,buffer,4096);
close(sockfd);
return 0;
}
TCP Client
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<strings.h>
int sockfd;
char buffer[4096];
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(SERV_TCP_PORT);
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
while(strcmp(buffer,"bye\n")!=0)
bzero(buffer,4096);
printf("\nClient");
fgets(buffer,4096,stdin);
write(sockfd,buffer,4096);
bzero(buffer,4096);
read(sockfd,buffer,4096);
printf("\nServer:%s",buffer);
close(sockfd);
return 0;
OUTPUT
Server
hi
client: hi
server:hi
client:hi
server:
client:bye
server:bye
[student@localhost ~]$
Client
Client:hi
Server:hi
Client:hi
Server:hi
Client:bye
Server:bye
[student@localhost ~]$
EX.NO:5
AIM
To create a client server program to get time and date information using the multiclient date time server.
ALGORITHM
Server
Client
Server
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<time.h>
#include<string.h>
#define MAX_BUF 30
int main()
{int sersoc,clisoc;
int conn,len,wri;
char str[MAX_BUF];
pid_t pid;
time_t ticks;
socklen_t clilen;
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(PORT);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if((sersoc=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("Socket Error");
exit(0);
if(bind(sersoc,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
perror("Bind Error");
exit(0);
listen(sersoc,10);
for(;;)
{len=sizeof(cliaddr);
if((pid=fork())==0)
{close(sersoc);
ticks=time(NULL);
strcpy(str,ctime(&ticks));
if((wri==(write(conn,str,sizeof(str),0)))<0)
{printf("Write Error");
exit(0);
close(conn);
exit(0);}
close(conn);}
close(sersoc);
return 0;
}
Client
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<time.h>
#include<arpa/inet.h>
#define BUFF_SIZE 30
{int clisoc,IPPORT_IP,re;
char recbuff[BUFF_SIZE];
bzero(&cliaddr,sizeof(cliaddr));
cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(2100);
cliaddr.sin_addr.s_addr=inet_addr(argv[1]);
if((clisoc=socket(AF_INET,SOCK_STREAM,0))<0)
{perror("Socket Error");
exit(0);}
{perror("connect error");
exit(0);}
if((re=(read(clisoc,recbuff,sizeof(recbuff),0)))<0)
{printf("Read Error");
exit(0);}
close(clisoc);
return 0;
OUTPUT
Server
Client
GLOMOSIM
In GloMoSim we are building a scalable simulation environment for wireless and wired network systems.
It is being designed using the parallel discrete-event simulation capability provided by Parsec. GloMoSim
currently supports protocols for a purely wireless network. In the future, we anticipate adding
functionality to simulate a wired as well as a hybrid network with both wired and wireless capabilities.
Most network systems are currently built using a layered approach that is similar to the OSI seven layer
network architecture. The plan is to build GloMoSim using a similar layered approach. Standard APIs
will be used between the different simulation layers. This will allow the rapid integration of models
developed at different layers by different people. The protocols being shipped with the current library
include the following:
Layers Protocols
Packet Reception Models SNR bounded, BER based with BPSK/QPSK modulation
Network (Routing) IP with AODV, Bellman-Ford, DSR, Fisheye, LAR scheme 1, ODMRP, WRP
To run GloMoSim, you will need the latest Parsec compiler (now included with the GloMoSim
distribution). If you want to develop your protocols in GloMoSim, you should have some familiarity with
Parsec, but you don't need to be an expert. Most protocol developers will be writing purely C code with
some Parsec functions for time management. Hence, you will need to use the Parsec compiler. Parsec
code is used extensively in the GloMoSim kernel. Most users do not need to know how the kernel works.
If you are interested in the GloMoSim kernel, you will need to have extensive knowledge about Parsec.
EX.NO:6(b)
INTRODUCTION
Ns is 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.
Ns began as a variant of the REAL network simulator in 1989 and has 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 supported through DARPA with SAMAN and
through NSF with CONSER, both in collaboration with other researchers including ACIRI. Ns has
always included substantal 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.
Installing ns
Requirements
To build ns you need a computer and a C++ compiler. We develop ns on several kinds of Unix (FreeBSD,
Linux, SunOS, Solaris), so it installs smoothest there, but it should run on an Posix-like computer,
possibly with some tweaking. Ns also builds and runs under Windows, see the dedicated Windows /
Cygwin page. Simple scenarios should run on any reasonable machine, but very large scenarios benefit
from large amounts of memory.
Ns is fairly large. The allinone package requires about 320MB of disk space to build. Building ns from
pieces can save some disk space. (If multiple people want to share files in the ns build tree to save space,
you may download a simple perl script, then follow the instruction in its README. There is detailed
instruction from CS599b class of USC. You may also find discussions in the ns-users mailing list archive
useful.)
There are two typical ways to build ns: building each component individually ("from the pieces") or
running a script that installs them all in one shot ("allinone"). If you just want to try it out quickly, you
might try the allinone package. If you want to do C-level developement, or save download time or disk
space, or have trouble with allinone, you should build it from the pieces.
Installing the allinone package: This package has a install script that handles installation of
Tcl/Tk, OTcl, tclcl, ns-2, nam-1 and other packages.
o Updating allinone: The "allinone" package is only updated for every release. If you are
interested in the convenience of the allinone package for your platform, but are interested
in using a fresher snapshot of the ns-2 code, this page is for you.
NS 2 ARCHITECTURE