Sei sulla pagina 1di 39

AIM: Implement connection oriented (TCP) concurrent Echo service. Server program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.

h> main() { int sockfd,newfd,len; struct sockaddr_in ser,cli; char msg[100]; ser.sin_family=AF_INET; ser.sin_port=htons(6045); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); if(bind(sockfd,(struct sockaddr *)&ser,sizeof(ser))<0) { perror("blind error"); exit(1); } if(listen(sockfd,5)<0) { perror("cannot listen"); exit(1); } len=sizeof(cli); while(1) { newfd=accept(sockfd,(struct sockaddr *)&cli,&len); if(fork()==0) { recv(newfd,(char *)&msg,sizeof(msg),0); send(newfd,(char *)&msg,sizeof(msg),0); close(newfd); exit(0); } } } Client program #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<stdlib.h> int main() { int sockfd; char msg1[100],msg2[100]; system("clear"); struct sockaddr_in obj; obj.sin_family=AF_INET; obj.sin_port=htons(6045); obj.sin_addr.s_addr=inet_addr("127.0.0.1");

sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd<0) { perror("socket creation error"); exit(0); } if(connect(sockfd,(struct sockaddr *)&obj,sizeof(obj))<0) { perror("connect error"); exit(0); } printf("enter a message to send to port 6010"); scanf("%s",msg1); send(sockfd,(char *)&msg1,sizeof(msg1),0); recv(sockfd,(char *)&msg2,sizeof(msg2),0); printf("\n message received from port 6010 is %s \n",msg2); return 0; } Output: enter a message to send to port 6045 hello message received from port 6045 is hello

AIM: Implement connection oriented(TCP) iterative Echo service. Server program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h>

#include<stdlib.h> #include<netinet/in.h> main() { int sockfd,newfd,len; struct sockaddr_in ser,cli; char msg[100]; ser.sin_family=AF_INET; ser.sin_port=htons(6045); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); if(bind(sockfd,(struct sockaddr *)&ser,sizeof(ser))<0) { perror("blind error"); exit(1); } if(listen(sockfd,5)<0) { perror("cannot listen"); exit(1); } len=sizeof(cli); while(1) { newfd=accept(sockfd,(struct sockaddr *)&cli,&len); recv(newfd,(char *)&msg,sizeof(msg),0); send(newfd,(char *)&msg,sizeof(msg),0); close(newfd); exit(0); } } Client program #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<stdlib.h> int main() { int sockfd; char msg1[100],msg2[100]; system("clear"); struct sockaddr_in obj; obj.sin_family=AF_INET; obj.sin_port=htons(6045); obj.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd<0) { perror("socket creation error"); exit(0);

} if(connect(sockfd,(struct sockaddr *)&obj,sizeof(obj))<0) { perror("connect error"); exit(0); } printf("enter a message to send to port 6010"); scanf("%s",msg1); send(sockfd,(char *)&msg1,sizeof(msg1),0); recv(sockfd,(char *)&msg2,sizeof(msg2),0); printf("\n message received from port 6010 is %s \n",msg2); return 0; } Output: enter a message to send to port 6045 hello message received from port 6045 is hello

AIM: Implement connection oriented (TCP) concurrent application to calculate gross salary in which the client sends the basic salary to the server, server calculates the gross salary and returns it to the client . Server program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() {

int sockfd,newfd,len; struct sockaddr_in ser,cli; int sal,net,da,hra,cca; ser.sin_family=AF_INET; ser.sin_port=htons(6040); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); if(bind(sockfd,(struct sockaddr *)&ser,sizeof(ser))<0) { perror("cannot bind"); exit(1); } if(listen(sockfd,5)<0) { perror("cannot listen"); exit(1); } len=sizeof(cli); while(1) { newfd=accept(sockfd,(struct sockaddr *)&cli,&len); if(fork()==0) { recv(newfd,(int *)&sal,sizeof(sal),0); da=(0.7)*sal; hra=(0.3)*sal; cca=(0.1)*sal; net=da+hra+cca; send(newfd,(int *)&net,sizeof(net),0); close(newfd); } } } Client program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd; struct sockaddr_in obj; int sal,net; obj.sin_family=AF_INET; obj.sin_port=htons(6040); obj.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); if(connect(sockfd,(struct sockaddr *)&obj,sizeof(obj))<0) { perror("connect error"); exit(1); } printf("enter sal:");

scanf("%d",&sal); send(sockfd,(int *)&sal,sizeof(sal),0); recv(sockfd,(int *)&net,sizeof(net),0); printf("\n\nnet salary is:%d\n\n",net); } Output: enter sal:5900 net salary is:6490

AIM: Implement connection oriented (TCP) iterative application to calculate gross salary in which the client sends the basic salary to the server ,server calculates the gross salary and returns it to the client Server program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd,newfd,len; struct sockaddr_in ser,cli; int sal,net,da,hra,cca; ser.sin_family=AF_INET; ser.sin_port=htons(6040); ser.sin_addr.s_addr=inet_addr("127.0.0.1");

sockfd=socket(AF_INET,SOCK_STREAM,0); if(bind(sockfd,(struct sockaddr *)&ser,sizeof(ser))<0) { perror("cannot bind"); exit(1); } if(listen(sockfd,5)<0) { perror("cannot listen"); exit(1); } len=sizeof(cli); while(1) { newfd=accept(sockfd,(struct sockaddr *)&cli,&len); recv(newfd,(int *)&sal,sizeof(sal),0); da=(0.7)*sal; hra=(0.3)*sal; cca=(0.1)*sal; net=da+hra+cca; send(newfd,(int *)&net,sizeof(net),0); close(newfd); } } Client program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd; struct sockaddr_in obj; int sal,net; obj.sin_family=AF_INET; obj.sin_port=htons(6040); obj.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); if(connect(sockfd,(struct sockaddr *)&obj,sizeof(obj))<0) { perror("connect error"); exit(1); } printf("enter sal:"); scanf("%d",&sal); send(sockfd,(int *)&sal,sizeof(sal),0); recv(sockfd,(int *)&net,sizeof(net),0); printf("\n\nnet salary is:%d\n\n",net); } Output: enter sal:5900

net salary is:6490

AIM: Implement connection oriented (TCP) concurrent application to access data of a file in which the client sends the name the student to the server, server access the file and returns student details to the client. [Initially the details should be stored in file] . //program to create database #include<stdio.h> #include<fcntl.h> #include<sys/stat.h> #include<stdlib.h> struct data { char name[30]; char email[50]; long ph; }obj; main() { int fd,n,i; system("clear"); fd=open("./db",O_RDWR|O_CREAT|O_APPEND,S_IRWXU|S_IRWXG); if(fd==-1) { perror("file open error"); exit(1); }

printf("enter no.of records"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter name"); scanf("%s",obj.name); printf("enter email address"); scanf("%s",obj.email); printf("enter phone number"); scanf("%d",&obj.ph); write(fd,(struct data *)&obj,sizeof(obj)); } } Server Program: #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<sys/types.h> #include<fcntl.h> #include<sys/stat.h> #include<string.h> struct data { char name[30]; char email[50]; int ph; }in,out; main() { struct sockaddr_in ser,cli; int sockfd,l,fd,newfd,flg; ser.sin_family=AF_INET; ser.sin_port=htons(6524); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); bind(sockfd,(struct sockaddr *)&ser,sizeof(ser)); l=sizeof(cli); listen(sockfd,5); while(1) { newfd=accept(sockfd,(struct sockaddr *)&cli,&l); if(fork()==0) { recv(newfd,(struct data *)&in,sizeof(in),0); flg=0; fd=open("./db",O_RDONLY); if(fd==-1) { perror("file error"); out.ph = -1; send(newfd,(struct data *)&out,sizeof(out),0);

} while(read(fd,(struct data *)&out,sizeof(out),0)) { if(!strcmp(in.name,out.name)) { send(newfd,(struct data *)&out,sizeof(out),0); flg++; break; } } if(!flg) { out.ph=0; send(newfd,(struct data *)&out,sizeof(out),0); } close(newfd); } } } Client Program: #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<sys/types.h> #include<fcntl.h> #include<sys/stat.h> #include<string.h> struct data { char name[30]; char email[50]; int ph; }obj; main() { struct sockaddr_in cli; int sockfd; system("clear"); cli.sin_family=AF_INET; cli.sin_port=htons(6524); cli.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); connect(sockfd,(struct sockaddr *)&cli,sizeof(cli)); printf("\n enter name "); scanf("%s",obj.name); system("clear"); send(sockfd,(struct data *)&obj,sizeof(obj),0); recv(sockfd,(struct data *)&obj,sizeof(obj),0); if(obj.ph!=-1) { if(!obj.ph) printf("\n\t.......%S invaild details....\n",obj.name); else {

printf("\n\t.........%s\n",obj.name); printf("\t Name: %s \n",obj.name); printf("\tEmail:%s\n",obj.email); printf("\tphone:%d\n\n",obj.ph); } } } Output: //db.c enter no.of records 2 enter name a enter email address a@gmail.com enter phone number 9876543210 enter name b enter email address b@gmail.com enter phone number 1234567890 //dbclient input: enter name a output: .........a Name: a Email:a@gmail.com phone:9876543210

AIM: Implement connection oriented (TCP) iterative application to access data of a file in which the client sends the name the student to the server, server access the file and returns student details to the client. [Initially the details should be stored in file]. //program to create database #include<stdio.h> #include<fcntl.h> #include<sys/stat.h> #include<stdlib.h> struct data { char name[30]; char email[50]; long ph; }obj; main() { int fd,n,i; system("clear"); fd=open("./db",O_RDWR|O_CREAT|O_APPEND,S_IRWXU|S_IRWXG); if(fd==-1) { perror("file open error"); exit(1); } printf("enter no.of records"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter name"); scanf("%s",obj.name); printf("enter email address"); scanf("%s",obj.email); printf("enter phone number"); scanf("%d",&obj.ph);

write(fd,(struct data *)&obj,sizeof(obj)); } } Server Program: #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<sys/types.h> #include<fcntl.h> #include<sys/stat.h> #include<string.h> struct data { char name[30]; char email[50]; int ph; }in,out; main() { struct sockaddr_in ser,cli; int sockfd,l,fd,newfd,flg; ser.sin_family=AF_INET; ser.sin_port=htons(6524); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); bind(sockfd,(struct sockaddr *)&ser,sizeof(ser)); l=sizeof(cli); listen(sockfd,5); while(1) { newfd=accept(sockfd,(struct sockaddr *)&cli,&l); recv(newfd,(struct data *)&in,sizeof(in),0); flg=0; fd=open("./db",O_RDONLY); if(fd==-1) { perror("file error"); out.ph = -1; send(newfd,(struct data *)&out,sizeof(out),0); } while(read(fd,(struct data *)&out,sizeof(out),0)) { if(!strcmp(in.name,out.name)) { send(newfd,(struct data *)&out,sizeof(out),0); flg++; break; } }

if(!flg) { out.ph=0; send(newfd,(struct data *)&out,sizeof(out),0); } close(newfd); } } Client Program: #include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<sys/types.h> #include<fcntl.h> #include<sys/stat.h> #include<string.h> struct data { char name[30]; char email[50]; int ph; }obj; main() { struct sockaddr_in cli; int sockfd; system("clear"); cli.sin_family=AF_INET; cli.sin_port=htons(6524); cli.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); connect(sockfd,(struct sockaddr *)&cli,sizeof(cli)); printf("\n enter name "); scanf("%s",obj.name); system("clear"); send(sockfd,(struct data *)&obj,sizeof(obj),0); recv(sockfd,(struct data *)&obj,sizeof(obj),0); if(obj.ph!=-1) { if(!obj.ph) printf("\n\t.......%S invaild details....\n",obj.name); else { printf("\n\t.........%s\n",obj.name); printf("\t Name: %s \n",obj.name); printf("\tEmail:%s\n",obj.email); printf("\tphone:%d\n\n",obj.ph); } } } Output: //db.c

enter no.of records 2 enter name a enter email address a@gmail.com enter phone number 9876543210 enter name b enter email address b@gmail.com enter phone number 1234567890 //dbclient input: enter name a output: .........a Name: a Email:a@gmail.com phone: 9876543210

AIM: Implement connection oriented (TCP) concurrent application to transfer the file contents in which the client sends the file name to the server, server access the file and returns file contents to the client.. Server Program: #include<stdio.h> #include<sys/types.h> #include<fcntl.h> #include<sys/stat.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd,newfd,len,fd; struct sockaddr_in ser,cli; char fname[20],ch; ser.sin_family=AF_INET; ser.sin_port=htons(9545); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); bind(sockfd,(struct sockaddr *)&ser,sizeof(ser)); listen(sockfd,5); len=sizeof(cli); while(1) { newfd=accept(sockfd,(struct sockaddr *)&cli,&len); if(fork()==0) { recv(newfd,fname,sizeof(fname),0); fd=open(fname,O_RDONLY); if(fd==-1) { perror("file error"); ch='\0'; send(newfd,(char *)&ch,sizeof(ch),0); exit(1); } while(read(fd,(char *)&ch,sizeof(ch))) { send(newfd,(char *)&ch,sizeof(ch),0); } ch='\0'; send(newfd,(char *)&ch,sizeof(ch),0);

close(newfd); exit(0); } } } Client Program: #include<stdio.h> #include<fcntl.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<sys/stat.h> #include<stdlib.h> int main() { int sockfd,fd; char fname[20],ch; struct sockaddr_in cli; system("clear"); cli.sin_family=AF_INET; cli.sin_port=htons(9545); cli.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); connect(sockfd,(struct sockaddr *)&cli,sizeof(cli)); system("ls"); printf("enter a filename to execute"); scanf("%s",fname); send(sockfd,fname,sizeof(fname),0); do { recv(sockfd,(char *)&ch,sizeof(ch),0); printf(" %c ",ch); }while(ch!='\0'); printf("\n"); } Output: a.out db dbclient.c dbservercon.c echocliitr.c echoseritr.c~ ser tcpftccli.c tcpitergrosssalcli.c cli db.c dbclientitr.c dbservercon.c~ echosercon.c labdb.c serdb.c tcpftcsercon.c tcpitergrosssalser.c clidb.c db.c~ dbseritr.c echoclicon.c echoseritr.c salarysercon.c sudha.txt tcpftseriter.c enter a filename to execute labdb.c # i n c l u d e < s t d i o . h > # i n c l u d e < f c n t l . h > # i n c l u d e < s y s / s t a t . h > # i n c l u d e < s t d l i b . h > s t r u c t d a t a

{ c h a r n a m e [ 5 0 ] ; c h a r e m a i l [ 5 0 ] ; l o n g p h ; } o b j ; m a i n ( ) { i n t f d , n , i ; s y s t e m ( " c l e a r " ) ; f d = o p e n ( " . / d b 1 " , O _ R D W R | O _ C R E A T | O _ A P P E N D , S _ I R W X U | S _ I R W X G ) ; i f ( f d = = - 1 ) { p e r r o r ( " f i l e o p e n e r r o r " ) ; e x i t ( 1 ) ; } p r i n t f ( " e n t e r n o o f r e c o r d s " ) ; s c a n f ( " % d " , & n ) ; f o r ( i = 0 ; i < n ; i + + ) { p r i n t f ( " e n t e r n a m e " ) ; s c a n f ( " % s " , o b j . n a m e ) ; p r i n t f ( " e n t e r e m a i l a d d r e s s " ) ; s c a n f ( " % s " , o b j . e m a i l ) ; p r i n t f ( " e n t e r p h o n e n u m b e r " ) ; s c a n f ( " % d " , & o b j . p h ) ; w r i t e ( f d , ( s t r u c t d a t a * ) & o b j , s i z e o f ( o b j ) ) ; } }

AIM: Implement connection oriented (TCP) iterative application to transfer the file contents in which the client sends the file name to the server, server access the file and returns file contents to the client. Server Program: #include<stdio.h> #include<sys/types.h> #include<fcntl.h> #include<sys/stat.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd,newfd,len,fd; struct sockaddr_in ser,cli; char fname[20],ch; ser.sin_family=AF_INET; ser.sin_port=htons(9945); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); bind(sockfd,(struct sockaddr *)&ser,sizeof(ser)); listen(sockfd,5); len=sizeof(cli); while(1) { newfd=accept(sockfd,(struct sockaddr *)&cli,&len); recv(newfd,fname,sizeof(fname),0); fd=open(fname,O_RDONLY); if(fd==-1) { perror("file error"); ch='\0'; send(newfd,(char *)&ch,sizeof(ch),0); exit(1); } while(read(fd,(char *)&ch,sizeof(ch))) { send(newfd,(char *)&ch,sizeof(ch),0); } ch='\0'; send(newfd,(char *)&ch,sizeof(ch),0); close(newfd); exit(0); } }

Client Program: #include<stdio.h>

#include<fcntl.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<sys/stat.h> #include<stdlib.h> int main() { int sockfd,fd; char fname[20],ch; struct sockaddr_in cli; system("clear"); cli.sin_family=AF_INET; cli.sin_port=htons(9545); cli.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_STREAM,0); connect(sockfd,(struct sockaddr *)&cli,sizeof(cli)); system("ls"); printf("enter a filename to execute"); scanf("%s",fname); send(sockfd,fname,sizeof(fname),0); do { recv(sockfd,(char *)&ch,sizeof(ch),0); printf(" %c ",ch); }while(ch!='\0'); printf("\n"); } Output: a.out db dbclient.c dbservercon.c echocliitr.c echoseritr.c~ ser tcpftccli.c tcpitergrosssalcli.c cli db.c dbclientitr.c dbservercon.c~ echosercon.c labdb.c serdb.c tcpftcsercon.c tcpitergrosssalser.c clidb.c db.c~ dbseritr.c echoclicon.c echoseritr.c salarysercon.c sudha.txt tcpftseriter.c enter a filename to execute labdb.c # i n c l u d e < s t d i o . h > # i n c l u d e < f c n t l . h > # i n c l u d e < s y s / s t a t . h > # i n c l u d e < s t d l i b . h > s t r u c t d a t a { c h a r n a m e [ 5 0 ] ; c h a r e m a i l [ 5 0 ] ; l o n g p h ; } o b j ; m a i n ( ) { i n t f d , n , i ; s y s t e m ( " c l e a r " ) ; f d = o p e n ( " . / d b 1 " , O _ R D W R | O _ C R E A T | O _ A P P E N D , S _ I R W X U | S _ I R W X G ) ;

i f ( f d = = - 1 ) { p e r r o r ( " f i e x i t ( 1 ) ; } p r i n t f ( " e n s c a n f ( " % d f o r ( i = 0 ; i < { p r i n t f ( " e n s c a n f ( " % s p r i n t f ( " e n s c a n f ( " % s p r i n t f ( " e n s c a n f ( " % d w r i t e ( f d , ( } }

l e

o p e n

e r r o r " ) ; o f r e c o r d s " ) ;

t e r n o " , & n ) ; n ; i + + ) t " t " t " s

e r n a m e " ) ; , o b j . n a m e ) ; e r e m a i l a d d r e s s " ) ; , o b j . e m a i l ) ; e r p h o n e n u m b e r " ) ; , & o b j . p h ) ; t r u c t d a t a * ) & o b j , s i z e o f ( o b j ) ) ;

AIM: Implement connection less (UDP) concurrent Echo service. Server Program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() {

int sockfd,newfd,len; struct sockaddr_in ser,cli; char msg[100]; ser.sin_family=AF_INET; ser.sin_port=htons(6045); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); if(bind(sockfd,(struct sockaddr *)&ser,sizeof(ser))<0) { perror("bind error"); exit(1); } len=sizeof(cli); while(1) { recvfrom(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&cli,&len); if(fork()==0) { sendto(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&cli,len); exit(0); } } } Client Program: #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<stdlib.h> int main() { int sockfd,len; char msg1[100],msg2[100]; system("clear"); struct sockaddr_in obj; obj.sin_family=AF_INET; obj.sin_port=htons(6045); obj.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd<0) { perror("socket creation error"); exit(0); } printf("enter a message to send to port 6045"); scanf("%s",msg1); len=sizeof(obj); sendto(sockfd,msg1,sizeof(msg2),0,(struct sockaddr *) &obj,len); recvfrom(sockfd,msg2,sizeof(msg1),0,(struct sockaddr *) &obj,&len);; printf("\n message received from port 6045 is %s \n",msg2);

return 0; } Output: enter a message to send to port 6045 hello message received from port 6045 is hello

AIM: Implement connection less (UDP) iterative Echo service. Server Program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd,newfd,len; struct sockaddr_in ser,cli; char msg[100]; ser.sin_family=AF_INET; ser.sin_port=htons(6045);

ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); if(bind(sockfd,(struct sockaddr *)&ser,sizeof(ser))<0) { perror("bind error"); exit(1); } len=sizeof(cli); while(1) { recvfrom(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&cli,&len); sendto(sockfd,msg,sizeof(msg),0,(struct sockaddr *)&cli,len); exit(0); } } Client Program: #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<stdlib.h> int main() { int sockfd,len; char msg1[100],msg2[100]; system("clear"); struct sockaddr_in obj; obj.sin_family=AF_INET; obj.sin_port=htons(6045); obj.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd<0) { perror("socket creation error"); exit(0); } printf("enter a message to send to port 6045"); scanf("%s",msg1); len=sizeof(obj); sendto(sockfd,msg1,sizeof(msg2),0,(struct sockaddr *) &obj,len); recvfrom(sockfd,msg2,sizeof(msg1),0,(struct sockaddr *) &obj,&len);; printf("\n message received from port 6045 is %s \n",msg2); return 0; } Output: enter a message to send to port 6045 hello

message received from port 6045 is hello

AIM: Implement connection less (UDP) concurrent application to calculate gross salary in which the client sends the basic salary to the server, server calculates the gross salary and returns it to the client . Server Program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd,newfd,len; struct sockaddr_in ser,cli; int sal,net,da,hra,cca; ser.sin_family=AF_INET; ser.sin_port=htons(6040); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); if(bind(sockfd,(struct sockaddr *)&ser,sizeof(ser))<0) {

perror("cannot bind"); exit(1); } len=sizeof(cli); while(1) { recvfrom(sockfd,&sal,sizeof(net),0,(struct sockaddr *) &cli,&len); if(fork()==0) { da=(0.7)*sal; hra=(0.3)*sal; cca=(0.1)*sal; net=da+hra+cca; sendto(sockfd,&net,sizeof(sal),0,(struct sockaddr *) &cli,len); } } } Client Program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd; struct sockaddr_in obj; int sal,net,len; obj.sin_family=AF_INET; obj.sin_port=htons(6040); obj.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); len=sizeof(obj); printf("enter sal:"); scanf("%d",&sal); sendto(sockfd,&sal,sizeof(net),0,(struct sockaddr *)&obj,len); recvfrom(sockfd,&net,sizeof(sal),0,(struct sockaddr *)&obj,&len); printf("\n\nnet salary is:%d\n\n",net); } Output: enter sal:5900 net salary is:6490

AIM: Implement connection less (UDP) iterative application to calculate gross salary in which the client sends the basic salary to the server, server calculates the gross salary and returns it to the client Server Program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd,newfd,len; struct sockaddr_in ser,cli; int sal,net,da,hra,cca; ser.sin_family=AF_INET; ser.sin_port=htons(6040); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); if(bind(sockfd,(struct sockaddr *)&ser,sizeof(ser))<0) { perror("cannot bind"); exit(1); } len=sizeof(cli); while(1)

{ recvfrom(sockfd,&sal,sizeof(net),0,(struct sockaddr *) &cli,&len); da=(0.7)*sal; hra=(0.3)*sal; cca=(0.1)*sal; net=da+hra+cca; sendto(sockfd,&net,sizeof(sal),0,(struct sockaddr *) &cli,len); } } Client Program: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd; struct sockaddr_in obj; int sal,net,len; obj.sin_family=AF_INET; obj.sin_port=htons(6040); obj.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); len=sizeof(obj); printf("enter sal:"); scanf("%d",&sal); sendto(sockfd,&sal,sizeof(net),0,(struct sockaddr *)&obj,len); recvfrom(sockfd,&net,sizeof(sal),0,(struct sockaddr *)&obj,&len); printf("\n\nnet salary is:%d\n\n",net); } Output: enter sal:5900 net salary is:6490

AIM: Implement connection less (UDP) concurrent application to transfer the file contents in which the client sends the file name to the server, server access the file and returns file contents to the client. Server Program: #include<stdio.h> #include<sys/types.h> #include<fcntl.h> #include<sys/stat.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd,newfd,len,fd; struct sockaddr_in ser,cli; char fname[20],ch; ser.sin_family=AF_INET; ser.sin_port=htons(6045); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); bind(sockfd,(struct sockaddr *)&ser,sizeof(ser)); len=sizeof(cli); while(1) { if(fork()==0) { recvfrom(sockfd,fname,sizeof(fname),0,(struct sockaddr *)&cli,&len); fd=open(fname,O_RDONLY); if(fd==-1)

{ perror("file error"); ch='\0'; send(newfd,(char *)&ch,sizeof(ch),0); exit(1); } while(read(fd,(char *)&ch,sizeof(ch))) { sendto(sockfd,(char *)&ch,sizeof(ch),0,(struct sockaddr *)&cli,len); } ch='\0'; sendto(sockfd,(char *)&ch,sizeof(ch),0,(struct sockaddr *)&cli,len); exit(0); } } } Client Program: #include<stdio.h> #include<fcntl.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<sys/stat.h> #include<stdlib.h> int main() { int sockfd,fd,len; char fname[20],ch; struct sockaddr_in cli; system("clear"); cli.sin_family=AF_INET; cli.sin_port=htons(6045); cli.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); system("ls"); printf("enter a filename to execute"); scanf("%s",fname); len=sizeof(cli); sendto(sockfd,fname,sizeof(fname),0,(struct sockaddr *)&cli,len); do { recvfrom(sockfd,(char *)&ch,sizeof(ch),0,(struct sockaddr *)&cli,&len); printf(" %c ",ch); }while(ch!='\0'); printf("\n"); } Output:

a.out db dbclient.c dbservercon.c echocliitr.c echoseritr.c~ ser tcpftccli.c tcpitergrosssalcli.c cli db.c dbclientitr.c dbservercon.c~ echosercon.c labdb.c serdb.c tcpftcsercon.c tcpitergrosssalser.c clidb.c db.c~ dbseritr.c echoclicon.c echoseritr.c salarysercon.c sudha.txt tcpftseriter.c enter a filename to execute labdb.c # i n c l u d e < s t d i o . h > # i n c l u d e < f c n t l . h > # i n c l u d e < s y s / s t a t . h > # i n c l u d e < s t d l i b . h > s t r u c t d a t a { c h a r n a m e [ 5 0 ] ; c h a r e m a i l [ 5 0 ] ; l o n g p h ; } o b j ; m a i n ( ) { i n t f d , n , i ; s y s t e m ( " c l e a r " ) ; f d = o p e n ( " . / d b 1 " , O _ R D W R | O _ C R E A T | O _ A P P E N D , S _ I R W X U | S _ I R W X G ) ; i f ( f d = = - 1 ) { p e r r o r ( " f i l e o p e n e r r o r " ) ; e x i t ( 1 ) ; } p r i n t f ( " e n t e r n o o f r e c o r d s " ) ; s c a n f ( " % d " , & n ) ; f o r ( i = 0 ; i < n ; i + + ) { p r i n t f ( " e n t e r n a m e " ) ; s c a n f ( " % s " , o b j . n a m e ) ; p r i n t f ( " e n t e r e m a i l a d d r e s s " ) ; s c a n f ( " % s " , o b j . e m a i l ) ; p r i n t f ( " e n t e r p h o n e n u m b e r " ) ; s c a n f ( " % d " , & o b j . p h ) ; w r i t e ( f d , ( s t r u c t d a t a * ) & o b j , s i z e o f ( o b j ) ) ; } }

AIM: Implement connection less (UDP) iterative application to transfer the file contents in which the client sends the file name to the server, server access the file and returns file contents to the client. Server Program: #include<stdio.h> #include<sys/types.h> #include<fcntl.h> #include<sys/stat.h> #include<sys/socket.h> #include<stdlib.h> #include<netinet/in.h> main() { int sockfd,newfd,len,fd; struct sockaddr_in ser,cli; char fname[20],ch; ser.sin_family=AF_INET; ser.sin_port=htons(6045); ser.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); bind(sockfd,(struct sockaddr *)&ser,sizeof(ser)); listen(sockfd,5); len=sizeof(cli); while(1) { newfd=accept(sockfd,(struct sockaddr *)&cli,&len); recv(newfd,fname,sizeof(fname),0); fd=open(fname,O_RDONLY); if(fd==-1) { perror("file error"); ch='\0'; send(newfd,(char *)&ch,sizeof(ch),0); exit(1); } while(read(fd,(char *)&ch,sizeof(ch))) { send(newfd,(char *)&ch,sizeof(ch),0);

} ch='\0'; send(newfd,(char *)&ch,sizeof(ch),0); close(newfd); exit(0); } } Client Program: #include<stdio.h> #include<fcntl.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<sys/stat.h> #include<stdlib.h> int main() { int sockfd,fd,len; char fname[20],ch; struct sockaddr_in cli; system("clear"); cli.sin_family=AF_INET; cli.sin_port=htons(6045); cli.sin_addr.s_addr=inet_addr("127.0.0.1"); sockfd=socket(AF_INET,SOCK_DGRAM,0); system("ls"); printf("enter a filename to execute"); scanf("%s",fname); len=sizeof(cli); sendto(sockfd,fname,sizeof(fname),0,(struct sockaddr *)&cli,len); do { recvfrom(sockfd,(char *)&ch,sizeof(ch),0,(struct sockaddr *)&cli,&len); printf(" %c ",ch); }while(ch!='\0'); printf("\n"); } Output: a.out db dbclient.c dbservercon.c echocliitr.c echoseritr.c~ ser tcpftccli.c tcpitergrosssalcli.c cli db.c dbclientitr.c dbservercon.c~ echosercon.c labdb.c serdb.c tcpftcsercon.c tcpitergrosssalser.c clidb.c db.c~ dbseritr.c echoclicon.c echoseritr.c salarysercon.c sudha.txt tcpftseriter.c enter a filename to execute labdb.c # i n c l u d e < s t d i o . h >

# i n c l u d e < f c n t l . h > # i n c l u d e < s y s / s t a t . h > # i n c l u d e < s t d l i b . h > s t r u c t d a t a { c h a r n a m e [ 5 0 ] ; c h a r e m a i l [ 5 0 ] ; l o n g p h ; } o b j ; m a i n ( ) { i n t f d , n , i ; s y s t e m ( " c l e a r " ) ; f d = o p e n ( " . / d b 1 " , O _ R D W R | O _ C R E A T | O _ A P P E N D , S _ I R W X U | S _ I R W X G ) ; i f ( f d = = - 1 ) { p e r r o r ( " f i l e o p e n e r r o r " ) ; e x i t ( 1 ) ; } p r i n t f ( " e n t e r n o o f r e c o r d s " ) ; s c a n f ( " % d " , & n ) ; f o r ( i = 0 ; i < n ; i + + ) { p r i n t f ( " e n t e r n a m e " ) ; s c a n f ( " % s " , o b j . n a m e ) ; p r i n t f ( " e n t e r e m a i l a d d r e s s " ) ; s c a n f ( " % s " , o b j . e m a i l ) ; p r i n t f ( " e n t e r p h o n e n u m b e r " ) ; s c a n f ( " % d " , & o b j . p h ) ; w r i t e ( f d , ( s t r u c t d a t a * ) & o b j , s i z e o f ( o b j ) ) ; } }

AIM: Implement program to create and use a pipe[Inter Process Communication] . Program: #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<string.h> #include<stdlib.h> int main(void) { int fd[2],nbytes; pid_t childpid; char string []="helloworld\n"; char readbuffer[80]; pipe(fd); if((childpid=fork())==-1) { perror("fork error"); exit(1); } if(childpid==0) { close(fd[0]); write(fd[1],string,(strlen(string)+1)); } else { close(fd[1]); nbytes=read(fd[0],readbuffer,sizeof(readbuffer)); printf("received string %s",readbuffer); } return(0); } Output: received string helloworld

AIM: Implement application to show Half-Duplex Communication. HeaderFile: half_duplex.h #define HALF_DUPLEX "/tmp/halfduplex" #define MAX_BUF_SIZE 255 Server Program: #include<stdio.h> #include<errno.h> #include<ctype.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include"half_duplex.h" #include<stdlib.h> int main(int argc,char *argv[]) { int fd,ret_val,count,numread; char buf[MAX_BUF_SIZE]; ret_val=mkfifo(HALF_DUPLEX,0666); if((ret_val==-1)&&(errno!=EEXIST)) { perror("error creating the named pipe\n"); exit(1); } fd=open(HALF_DUPLEX,O_RDONLY); numread=read(fd,buf,MAX_BUF_SIZE); buf[numread]='\0'; printf("half duplex server:read from the pipe :%s\n",buf); count=0; while(count<numread) { buf[count]=toupper(buf[count]); count++; } printf("half dulpex server:converted string:%s",buf); } Client Program: #include<stdio.h> #include<errno.h> #include<ctype.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<string.h> #include<fcntl.h> #include"half_duplex.h" #include<stdlib.h> int main(int argc,char *argv[])

{ int fd; if(argc!=2) { printf("usage:%s<string to be sent to the server>n",argv[0]); exit(1); } fd=open(HALF_DUPLEX,O_WRONLY); write(fd,argv[1],strlen(argv[1])); } Output: [root@localhost lab]# ./cli xyz [root@localhost lab]# half duplex server:read from the pipe :xyz half dulpex server:converted string: XYZ

AIM: Implement application to show RPC [Remote Procedure Call] mechanism. //Echo.x program ECHO{ version ECHO_VER{ string message(string)=1; }=1; }=0x2000000f; Server Program: #include<stdio.h> #include<rpc/rpc.h> #include "Echo.h" char **message_1_svc(char **msg,struct svc_req *obj) { static char *result; result=*msg; return &result; } Client Program: #include<stdlib.h> #include<stdio.h> #include<sys/stat.h> #include<sys/socket.h> #include<sys/types.h> #include<string.h> #include<unistd.h> #include "Echo.h" main(int argc,char *argv[]) { char *ip,*msg,**rval; CLIENT *obj; char temp[100]; system("clear"); if(argc<2) { printf("provide IP address at cmd line\n"); exit(1); } ip=argv[1]; if((obj=clnt_create(ip,ECHO,ECHO_VER,"udp"))==NULL) { printf("\n can't find server"); exit(1); } printf("enter a string"); scanf("%s",temp); msg=temp; if((rval=message_1(&msg,obj))==NULL) { printf("data error"); exit(1); } printf("msg received from server is %s\n",*rval); }

Output: root@localhost lab]# rpcgen Echo.x [root@localhost lab]# ls a.out echo_client.c Echo_clnt.c Echo.h echo_svc.c Echo_svc.c Echo.x half_duplex.h hd_client.c hd_server.c pipe.c tcp udp [root@localhost lab]# gcc -o ser Echo_svc.c echo_svc.c [root@localhost lab]# gcc -o cli echo_client.c echo_clnt.c gcc: echo_clnt.c: No such file or directory [root@localhost lab]# gcc -o cli echo_client.c Echo_clnt.c [root@localhost lab]# ./ser& [1] 3194 [root@localhost lab]# ./cli 192.168.5.45 enter a string xyz msg received from server is xyz

Potrebbero piacerti anche