0% found this document useful (0 votes)
880 views

WEEK-9:: Design Udp Client and Server Application To Reverse The Given Input Sentence? Server Program

The document describes programs that illustrate inter-process communication techniques in Linux including UDP client-server programs to reverse and append strings, file locking using semaphores, and shared memory. It provides code for UDP client and server programs that reverse a input string and append one string to another. It also includes a program that uses a semaphore for file locking and programs that use shared memory for inter-process communication without synchronization.

Uploaded by

madirikiran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
880 views

WEEK-9:: Design Udp Client and Server Application To Reverse The Given Input Sentence? Server Program

The document describes programs that illustrate inter-process communication techniques in Linux including UDP client-server programs to reverse and append strings, file locking using semaphores, and shared memory. It provides code for UDP client and server programs that reverse a input string and append one string to another. It also includes a program that uses a semaphore for file locking and programs that use shared memory for inter-process communication without synchronization.

Uploaded by

madirikiran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

WEEK-9:

DESIGN UDP CLIENT AND SERVER APPLICATION TO REVERSE THE GIVEN INPUT SENTENCE? SERVER PROGRAM: #include<unistd.h> #include<strings.h> #include<stdlib.h> #include<sys/socket.h> #include<stdio.h> #include<netinet/in.h> #include<string.h> #define MAXLINE 4000 void strrev(char[],int,struct sockaddr_in,int); int main(int argc,char *argv[]) { int sockfd,len; int clilen; char buf[400]; struct sockaddr_in servaddr,cliaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(9820); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); len=sizeof(cliaddr); recvfrom(sockfd,buf,MAXLINE,0,(struct sockaddr *)&cliaddr,&len); strrev(buf,sockfd,cliaddr,len);

} void strrev(char buf[50],int listenfd,struct sockaddr_in cliaddr,int len) { char buf1[20]; int clilen; int i,j,l; l=strlen(buf); for(i=0;j=l-1;i++,j--) buf1[i]=buf[j]; sendto(listenfd,buf1,sizeof(buf1),0,(struct sockaddr *)&cliaddr,clilen); }

CLIENT PROGRAM: #include<unistd.h> #include<strings.h> #include<stdlib.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdio.h> #include<string.h> #define MAXLINE 4000 #define SA struct sockaddr void main(int argc,char *argv[]) { int sockfd,len; struct sockaddr_in cliaddr,servaddr; char buf[100],buf1[100]; sockfd=socket(AF_INET,SOCK_DGRAM,0); 2

bzero(&cliaddr,sizeof(cliaddr)); cliaddr.sin_family=AF_INET; cliaddr.sin_port=htons(9820); inet_pton(AF_INET,"127.0.0.1",&cliaddr.sin_addr.s_addr); len=sizeof(servaddr); printf("enter the string"); fgets(buf,50,stdin); sendto(sockfd,buf,sizeof(buf),0,(SA *)&cliaddr,sizeof(cliaddr)); recvfrom(sockfd,buf1,sizeof(buf1),0,(SA *)&servaddr,&len); printf("reversed string is:"); buf1[sizeof(buf1)]='\0'; fputs(buf1,stdout); }

OUTPUT: $CC UDPSERVER.C $CC UDPCLENT.C ENTER THE STRING: RANI REVERSED STRING IS:INAR

WEEK-10

DESIGN UDP CLIENT AND SERVER APPLICATION TO APPEND A STRING ? SERVER PROGRAM: #include<unistd.h> #include<strings.h> #include<stdlib.h>

#include<sys/socket.h> #include<netinet/in.h> #include<stdio.h> #include<string.h> #define MAXLINE 4000 int main(int argc,char *argv[]) { int sockfd,slen,alen,i,j,len; char buf[400],buf1[400]; struct sockaddr_in servaddr,cliaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(9820); servaddr.sin_addr.s_addr=htonl(INADDR_ANY); bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); len=sizeof(cliaddr); recvfrom(sockfd,buf,MAXLINE,0,(struct sockaddr *)&cliaddr,&len); slen=strlen(buf); recvfrom(sockfd,buf1,MAXLINE,0,(struct sockaddr *)&cliaddr,&len); alen=strlen(buf1); j=alen-1; for(i=0;i<j;i++,slen++) buf[slen]=buf1[i]; buf[strlen(buf1)]='\0'; sendto(sockfd,buf,MAXLINE,0,(struct sockaddr *)&cliaddr,len); }

CLIENT PROGRAM: #include<unistd.h> #include<strings.h> #include<stdlib.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdio.h> #include<strings.h> #include<arpa/inet.h> #define MAXLINE 4000 #define SA struct sockaddr int main(int argc,char *argv[]) { int sockfd,len; struct sockaddr_in cliaddr,servaddr; char buf[100],buf1[100],buf2[100]; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&cliaddr,sizeof(cliaddr)); cliaddr.sin_family=AF_INET; cliaddr.sin_port=htons(9820); inet_pton(AF_INET,"127.0.0.1",&cliaddr.sin_addr.s_addr); len=sizeof(servaddr); printf("enter the string:"); fgets(buf,50,stdin); sendto(sockfd,buf,sizeof(buf),0,(SA *)&cliaddr,sizeof(cliaddr)); printf("enter the sting to be appended:"); fgets(buf1,50,stdin); sendto(sockfd,buf1,sizeof(buf1),0,(SA *)&cliaddr,sizeof(cliaddr)); 5

recvfrom(sockfd,buf2,sizeof(buf2),0,(SA *)&servaddr,&len); printf("appended string is :"); buf2[sizeof(buf2)]='\0'; fputs(buf2,stdout); }

OUTPUT: $CC UDPSERVER.C $CC UDPCLIENT.C ENTER THE STRING:RAJU ENTER THE STRING TO BE APPENDED:RANI APPENDED STRING IS:RAJU RANI

WEEK-11
WRITE A PROGRAM THAT ILLUSTRATES FILE LOCKING USING SEMAPHORES

AIM : C program that illustrate file locking using semaphores PROGRAM: #include<stdio.h> #include<stdlib.h> #include<error.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> int main(void) { key_t key; int semid; union semun arg; if((key==ftok("sem demo.c","j"))== -1) { perror("ftok"); exit(1);
6

} if(semid=semget(key,1,0666|IPC_CREAT))== -1) { perror("semget"): exit(1); } arg.val=1; if(semctl(semid,0,SETVAL,arg)== -1) { perror("smctl"); exit(1); } return 0; } OUTPUT: semget smctl WEEK-12:
WRITE A PROGRAM ON ILLUSTRATING SHARED MEMORY AIM: Program on illustrating Shared memory DESRIPTION: It is a PURE IPC....i.e, no synchronization is established between the two process.. It is the fastest IPC Uses direct memory block No extra fns for writing or reading from shared mem segment PROGRAM: // PROGRAM FOR SMS RCV #include<stdio.h> #include<sys/types.h> #include<sys/shm.h> main() { int id; 7 save: smsrcv.c

void *ptr; id=shmget(22,128,IPC_CREAT|0664);//shmget allocates the shared memory segment and returns the id associated with key...... ptr=shmat(id,0,0);//shmat attaches the shared memory segment identified by the id for shared memory operation and returns the address of shared memory segment. if(ptr) { printf("attached successfully\n"); } { perror(""); return; } printf("received :%s",(char*)ptr);//receiving the data from the shared mem segmet shmdt(ptr); } Save: smssnd.c #include<stdio.h> #include<sys/ipc.h> #include<sys/shm.h> main() { int id; void *ptr; id=shmget(22,128,IPC_CREAT|0664); if(ptr=shmat(id,0,0)) { printf("Successfully attached\n"); else

} else { perror(""); return; } printf("Enter the msg :"); scanf("%[^\n]",ptr); } OUTPUT: $ cc smssnd.c $ ./a.out Successfully attached Enter the message:hai $ cc smsrcv.c $ ./a.out Attached successfully Received:hai

You might also like