You are on page 1of 36

c 




  c

c
c cc c




To create a client server program to get time and date information using the TCP protocol date time
server.






‡ Declare the necessary arrays and variables.


‡ Set server port address using socket().
‡ Get the current date and time.
‡ Connect to the client.
‡ Stop the process.

 

‡ Set the client machine address.


‡ Connect to the server.
‡ Read from the server the current date and time.
‡ Display the current date and time.
‡ Close the connection.

 c



#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;
}

 

#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;
}

!
!




[root@localhost ~]# ./a.out

 

[root@localhost ~]# ./a.out 127.0.0.1


The current date and time :Mon Sep 07 17:42:56 2009

Y
Y
Y
Y
Y
Y
Y
Y
Y
Y



c " 

!   c




To echo the message in the client through server using UDP.




 

1. Set client machine address and server port address.

2. Using socket() create socket for server.

3. Read the message given in the client.

4. Send the message to server from client.

5. Receive the same message from the server.

6. Print the message.



1. Set server port address using socket().

2. Create a server by specifying server port.

3. Receive the message from client and resend the message to client.

4. Stop the process.


 c

 

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<string.h>

#include<arpa/inet.h>

#include<stdio.h>

#define MAXLINE 1024

int main(int argc,char *argv[])

int sockfd;

int n;

socklen_t len;

char sendline[1024],recvline[1024];

struct sockaddr_in servaddr;

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);

sendto(sockfd,sendline,strlen(sendline),0,(struct sockaddr *)&servaddr,len);


n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL);

recvline[n]=0;

printf("%s",recvline);

return 0;



#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<unistd.h>

#include<netdb.h>

#include<arpa/inet.h>

#define MAXLINE 1024

int main(int argc,char **argv)

int sockfd;

int n;

socklen_t len;

char msg[1024];

struct sockaddr_in servaddr,cliaddr;

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);

bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

for(;;)

len=sizeof(cliaddr);

n=recvfrom(sockfd,msg,MAXLINE,0,(struct sockaddr *)&cliaddr,&len);

printf("%s",msg);

if(n<0)

perror("send error");

sendto(sockfd,msg,n,0,(struct sockaddr *)&cliaddr,len);

return 0;

!
!


./a.out udpserver.c

./a.out udpclient.c

Hi

Hi





c #
$$ 
 



To demonstrate sliding window protocol by sending frames and acknowledging it.




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.

 c

#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;
}

!
!


Number of frame is 5
Sending frame 1
Sending frame 2
Sending frame 3
Sending frame 4
Sending frame 5

Acknowledgement for the frames upto 5


End of sliding window protocol
Y
Y
Y
Y
Y
Y
Y
Y
Y
Y
c % 

 ! 




To create a program to implement DNS using TCP.




  

1.Create an instance of the sockaddr_in for getting the IP address.

2.Create a socket for the client using the socket() function.

3.Get the domain name from the user.

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.



1.Specify IP address of the server by socket() function.

2.To bind the server to the socket, use the bind() function.

3.Create listening sockets using the listen() function.

4.Write the domain to the buffer using the write() function.

5.Close the connection after writing.


 c



#include<string.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#define TCP_SERV_PORT 8900

#include<stdio.h>

#define SA struct sockaddr

#include<stdlib.h>

#include<netdb.h>

int main()

int listenfd,connfd;

socklen_t len;

struct sockaddr_in servaddr,cliaddr;

struct hostent *hp,tp;

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)

printf("cant get address");

if(inet_ntop(AF_INET,hp->h_addr,buff,sizeof(buff))<=0)

printf("host address not available");

printf("%s",buff);

write(connfd,buff,strlen(buff)+1);

close(connfd);

return 0;

 

#include<string.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<stdio.h>
#include<netdb.h>

#include<stdlib.h>

#define SA struct sockaddr

int main(int argc,char ** argv)

socklen_t len;

struct sockaddr_in servaddr;

struct hostent tp;

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");

printf("enter the domain name");

scanf("%s",host);

write(sockfd,host,strlen(host)+1);

if(read(sockfd,buff,sizeof(buff))<0)m

printf("read error");

printf("the host address %s",buff);

return 0;

}
!
!




[student@karthik ~]$ cc dnss.c

[student@karthik ~]$ ./a.out

 

[student@karthik ~]$ cc dnsc.c

[student@karthik ~]$ ./a.out

enter the domain name localhost.localdomain

the host address 127.0.0.1

Y
Y
Y
c %"

Èc
 Èc 
 



To transfer a file using FTP between the server and the client systems.






1. Set server port address.

2. Using socket function, create a socket for server by specifying server port.

3. Allocate a buffer size.

4. Use TCP socket for sending messages from server to client.

5. Use accept() method to read file by server.

6. Use the input method to read file.

7. Write file to the client.

 

1. Set client machine address and server port address.

2. Using socket function, create socket for server.

3. Allocate buffer use TCP for receiving message from server to client.

4. Read the file from the server.

5. Read the output from the system.


 c



#include<stdio.h>

#include<sys/types.h>

#include<netinet/in.h>

#include<sys/socket.h>

#include<errno.h>

#define SIZE 256

int main()

int srFd,CIFD,len;

struct sockaddr_in server,client;

char buf[SIZE],buf1[SIZE];

int n,j;

FILE *fp,*fp1;

srFd=socket(AF_INET,SOCK_STREAM,0);

if(srFd<0)

printf("\n Socket error");

exit(0);

bzero(&server,sizeof(server));

server.sin_family=AF_INET;

server.sin_port=htons(2200);
server.sin_addr.s_addr=htons(INADDR_ANY);

if(bind(srFd,(struct sockaddr *)&server,sizeof(server))<0)

printf("\n server : bind error");

close(srFd);

exit(0);

if(listen(srFd,1)<0)

printf("\n Listen Error");

close(CIFD);

exit(0);

printf("\n Server is ready to listen\n");

len=sizeof(client);

CIFD=accept(srFd,(struct sockaddr *)&client,&len);

if(CIFD<0)

printf("\n Accept error");

close(CIFD);

close(srFd);

exit(0);

printf("Client gets connected\n");

bzero(&buf,sizeof(buf));

if((n=recv(CIFD,&buf,SIZE,0))<0)
{

printf("\n Receive Error in Server\n");

close(srFd);

close(CIFD);

exit(0);

buf[n-1]=NULL;

printf("\n File name %s",buf);

if((n=send(CIFD,buf,strlen(buf),0))<0)

printf("\n Error");

close(CIFD);

exit(0);

close(srFd);

close(CIFD);

exit(0);

 

#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;

struct sockaddr_in client;

char buf[SIZE];

int n;

if((CIFD=socket(AF_INET,SOCK_STREAM,0))<0)

printf("\n Client Socket Error");

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);

if(connect(CIFD,(struct sockaddr *)&client,sizeof(client))<0)

printf("\n connection failed");

close(CIFD);

exit(0);

printf("\n connection enabled\n");

printf("\n Enter source file\n");

bzero(&buf,sizeof(buf));

if(fgets(buf,SIZE,stdin)==NULL)
{

printf("\n Failed to get source file name in client\n");

close(CIFD);

exit(0);

if(send(CIFD,buf,strlen(buf),0)<0)

printf("\n Send Error\n");

close(CIFD);

exit(0);

printf("\n Client message sent\n");

bzero(&buf,sizeof(buf));

if((n=recv(CIFD,buf,SIZE,0))<0)

printf("\n File name receiver error in client from server\n");

close(CIFD);

exit(0);

//buf[n]=NULL;

printf("\n Destination file name from server %s",buf);

buf[n]=NULL;

close(CIFD);

exit(0);

}
!
!

 

[student@localhost ~]$ ./a.out

Connection Enabled

Enter Source File

Tps.c

Client Message Sent

Destination Filename From Server tps.c



[student@localhost ~]$ ./a.out

Server is ready to listen

Client get connected

Tps.c
c %& 


 




To write a program using TCP chat for establishing communication between the server & client.




1. Start.

2. The sender must send the message to the receiver through the network.

3. The sender message must terminate with a µbye string.

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.

6. Thus the chat program follows recursively.

 c

 

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<netdb.h>

#include<strings.h>

#define SERV_TCP_PORT 5001

int main(int argc,char *argv[])

{
int sockfd,newsockfd,clength;

struct sockaddr_in serv_addr,cli_addr;

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;

}

  

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<netdb.h>

#include<strings.h>

#define SERV_TCP_PORT 5001

int main(int argc,char *argv[])

int sockfd;

struct sockaddr_in serv_addr;

struct hostent *server;

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));

printf("\nEnter your message to send..\n");

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;

!
!




[student@localhost ~]$ ./a.out

hi

client: hi

server:hi

client:hi

server:

client:bye

server:bye

[student@localhost ~]$

 

[student@localhost ~]$ ./a.out

Enter your message to send..

Client:hi
Server:hi

Client:hi

Server:hi

Client:bye

Server:bye

[student@localhost ~]$
c '

YYYYYYYYYYYYYYYYYYYYYYYYYYYYY

!
 c

c
c cc



To create a client server program to get time and date information using the multiclient date time server.






‡ Declare the necessary arrays and variables.

‡ Set server port address using socket().

‡ Get the current date and time.

‡ Connect to the client.

‡ Stop the process.

 

‡ Set the client machine address.

‡ Connect to the server.

‡ Read from the server the current date and time.

‡ Display the current date and time.

‡ Close the connection.


 c



#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;

}
 

#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;

!
!




[root@localhost ~]# ./a.out

 

[root@localhost ~]# ./a.out 127.0.0.1

The current date and time :Mon Sep 10 17:42:56 2007


c (  

    c
$ !
  c

 

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

Mobility Random waypoint, Random drunken, Trace based

Radio Propagation Two ray and Free space

Radio Model Noise Accumulating

Packet Reception Models SNR bounded, BER based with BPSK/QPSK modulation

Data Link (MAC) CSMA, IEEE 802.11 and MACA

Network (Routing) IP with AODV, Bellman-Ford, DSR, Fisheye, LAR scheme 1, ODMRP, WRP

Transport TCP and UDP

Application CBR, FTP, HTTP and Telnet


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.




c (" 


cc
$ !
) # 


!


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.

* +*

,-. *

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.)
/0/1+1"-1+*

As of November 2005, ns is available at this /-&È/+/& /.

Ns requires a modestly up-to-date installation of Tcl/Tk (with header files), and two additional packages:
tclcl and otcl. Most OS installations do not come with full Tcl/Tk installations or with these other
packages, so you will most likely need to install several packages.

&-23   1 /** -& /*

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.

4Y * + 4/5&6+ This package has a install script that handles installation of
Tcl/Tk, OTcl, tclcl, ns-2, nam-1 and other packages.
iY !51 +/ 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.

 # 
c
!c

Y
Y

Y
Y