0% found this document useful (0 votes)
53 views31 pages

NP Full Programs

Uploaded by

Saiteja Popuri
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views31 pages

NP Full Programs

Uploaded by

Saiteja Popuri
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 31

Basic TCP : TCP clinet.c #include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<netinet/in.h> #include<fcntl.h> #include<sys/types.h> #include<unistd.h> #include<string.h> #include<arpa/inet.

h> int main() { int sockfd, n; struct sockaddr_in servaddr; char buf[1024]; sockfd=socket(AF_INET,SOCK_STREAM,0); printf("socket created \n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons (9997); inet_pton (AF_INET,"172.168.9.4",&servaddr.sin_addr); if (connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1) { printf ("connection error \n"); } else printf ("connection established \n"); while ((n=read(STDIN_FILENO,buf,1024))>0) { buf[n-1]='\0'; write (sockfd,buf,n); write (sockfd,"\n",1); if ((strcmp(buf,"quit"))==0) break; } close(sockfd); return 0; }

TCP server.c

#include<stdio.h> #include<fcntl.h> #include<sys/types.h> #include<unistd.h> #include<string.h> #include<arpa/inet.h> int main() { int listenfd, connfd, n; socklen_t clilen; char buf[1024]; struct sockaddr_in cliaddr, servaddr; printf ("server ip address :%s and port number:%s \n","192.168.11.98","9999"); listenfd=socket(AF_INET,SOCK_STREAM,0); if (listenfd==-1) printf("socket error \n"); else printf("socket is created for tcp connection \n"); bzero (&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; inet_pton(AF_INET,"192.168.11.98",&servaddr.sin_addr); servaddr.sin_port=htons(9999); if (bind( listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1) printf ("bind error \n"); else printf ("server bind to ip and port number \n"); if(listen(listenfd,5)==-1) printf ("listening not working \n"); else printf ("server is listening the client \n"); clilen=sizeof (cliaddr); connfd=accept (listenfd,(struct sockaddr*)&cliaddr,&clilen); for(;;) { n=read (connfd,&buf,1024); write (1,buf,n); buf[n-1]='\0'; if (strcmp(buf,"quit")==0) break; } close(connfd); return 0; }

TCP ECHO : TCP client.c #include<stdio.h> #include<fcntl.h> #include<sys/types.h> #include<unistd.h> #include<string.h> #include<arpa/inet.h> int main() { int listenfd, connfd, n; socklen_t clilen; char buf[1024]; struct sockaddr_in cliaddr, servaddr; printf ("server ip address :%s and port number:%s \n","192.168.11.98","9999"); listenfd=socket(AF_INET,SOCK_STREAM,0); if (listenfd==-1) printf("socket error \n"); else printf("socket is created for tcp connection \n"); bzero (&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; inet_pton(AF_INET,"192.168.11.98",&servaddr.sin_addr); servaddr.sin_port=htons(9999); if (bind( listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1) printf ("bind error \n"); else printf ("server bind to ip and port number \n"); if(listen(listenfd,5)==-1) printf ("listening not working \n"); else printf ("server is listening the client \n"); clilen=sizeof (cliaddr); connfd=accept (listenfd,(struct sockaddr*)&cliaddr,&clilen); for(;;) { n=read (connfd,&buf,1024); write (1,buf,n); buf[n-1]='\0'; if (strcmp(buf,"quit")==0) break; } close(connfd); return 0;

} TCP server.c #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> int main() { int listenfd, pid, connfd, n,true=1,size_in; socklen_t clilen; char buf[1024],str[200]; struct sockaddr_in cliaddr,servaddr; listenfd=socket(AF_INET,SOCK_STREAM,0); if(listenfd==-1) printf("socket error \n"); else printf("socket is created for tcp connection \n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; inet_pton(AF_INET,"172.168.9.4",&servaddr.sin_addr); servaddr.sin_port=htons(9995); if (bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1) printf("bind error \n"); else printf("server bind to ip and port number \n"); listen(listenfd,5); for(;;) { printf("Waiting for another client..................\n"); size_in=sizeof(struct sockaddr_in); connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&size_in); printf("%s client connected\n",inet_ntoa(cliaddr.sin_addr)); printf("Port number %d\n",ntohs(cliaddr.sin_port)); if((pid=fork())==0) { close(listenfd); while((n=read(connfd,&buf,1024))>0) { if(strncmp(buf,"quit",4)==0) {

close(connfd); exit(0); } printf("\nMesssage from %s Client:\n",inet_ntoa(cliaddr.sin_addr)); write(1,buf,n); write(connfd,buf,n); } } close(connfd); } return 0; }

FILE transfer using TCP TCPFTP client.c #include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<netinet/in.h> #include<fcntl.h> #include<sys/types.h> #include<unistd.h> #include<string.h> #include<arpa/inet.h> #include<fcntl.h> int main() { int sockfd, n; struct sockaddr_in servaddr; char buf[1024],filename[40],ack[50]; sockfd=socket(AF_INET,SOCK_STREAM,0); printf("socket created \n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(9999); inet_pton(AF_INET,"192.168.11.98",&servaddr.sin_addr); if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1) { printf("connection error \n"); } else printf("connection established \n"); printf("enter the file name \n"); gets(filename); write(sockfd,filename,sizeof(filename)); while((n=read(sockfd,&buf,1024))>0) { write(STDOUT_FILENO,buf,n); } close(sockfd); return 0; }

TCP server.c #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <fcntl.h> int main() { int listenfd, pid, connfd, n,true=1,size_in,fd; socklen_t clilen; char buf[1024],str[200],filename[256]; struct sockaddr_in cliaddr,servaddr; listenfd=socket(AF_INET,SOCK_STREAM,0); if(listenfd==-1) printf("socket error \n"); else printf("socket is created for tcp connection \n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; inet_pton(AF_INET,"172.168.9.4",&servaddr.sin_addr); servaddr.sin_port=htons(9999); if (bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1) printf("bind error \n"); else printf("server bind to ip and port number \n"); listen(listenfd,5); printf("Waiting for another client..................\n"); size_in=sizeof(struct sockaddr_in); connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&size_in); printf("%s client connected\n",inet_ntoa(cliaddr.sin_addr)); printf("Port number %d\n",ntohs(cliaddr.sin_port)); read(connfd,&filename,sizeof(filename)); printf("\n client is asking for the file:: %s\n",filename);

if((fd=open(filename,O_RDONLY))<0) { write(connfd,"file does not exists\n",21); printf("File does not exists\n"); close(listenfd); close(connfd); exit(0); } else { write(connfd,"File found\n",11); } while((n=read(fd,&buf,1024))) { write(connfd,buf,n); write(STDOUT_FILENO,buf,n); } close(listenfd); close(connfd); return 0; }

TCP server using SELECT function Select server.c

#include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<netinet/in.h> #include<fcntl.h> #include<sys/types.h> #include<unistd.h> #include<string.h> #include<arpa/inet.h> #include <errno.h> #include <string.h> #include<sys/select.h> #include<sys/time.h> int main() { int i,maxi,maxfd,listenfd,connfd,sockfd; int nready,client[FD_SETSIZE]; ssize_t n; fd_set rset,allset; char line[1024]; socklen_t clilen; struct sockaddr_in cliaddr,servaddr; listenfd=socket(AF_INET, SOCK_STREAM,0); if(listenfd==-1) printf("socket error \n"); else printf("socket is created for tcp connection %d \n",listenfd); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(1276); if(bind(listenfd,(struct sockaddr *)&servaddr, sizeof(servaddr))==-1) printf("bind error \n"); else printf("server bind to ip and port number \n"); listen(listenfd,5); maxfd=listenfd; maxi=-1; for(i=0;i<FD_SETSIZE; i++) client[i]= -1; FD_ZERO(&allset); FD_SET(listenfd,&allset); for(;;) {

rset=allset; nready=select(maxfd +1, &rset, NULL,NULL,NULL); if(FD_ISSET(listenfd,&rset)) { clilen=sizeof(cliaddr); connfd=accept(listenfd, (struct sockaddr *)&cliaddr, &clilen); printf("%s %dclient connected\n",inet_ntoa(cliaddr.sin_addr),connfd); printf("Port number %d\n",ntohs(cliaddr.sin_port)); for(i=0;i<FD_SETSIZE;i++) { if(client[i]<0) { client[i]=connfd; break; } } if(i==FD_SETSIZE) { printf("toomany clients"); exit(0); } FD_SET(connfd,&allset); if(connfd > maxfd) maxfd=connfd; if(i>maxi) maxi=i; if(--nready<=0) continue; } for(i=0;i<=maxi;i++) { if((sockfd=client[i]) <0) continue; if(FD_ISSET(sockfd,&rset)) { if( (n=read(sockfd, line, 1024))==0) { close(sockfd); FD_CLR(sockfd,&allset); client[i]= -1; } else write(sockfd,line, n);

if(--nready <=0) break; } } } }

TCP server using POLL function Pollserver.c

#include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<netinet/in.h> #include<fcntl.h> #include<sys/types.h> #include<unistd.h> #include<string.h> #include<arpa/inet.h> #include <errno.h> #include <string.h> #include<sys/select.h> #include<sys/time.h> int main() { int i,maxi,maxfd,listenfd,connfd,sockfd; int nready,client[FD_SETSIZE]; ssize_t n; fd_set rset,allset; char line[1024]; socklen_t clilen; struct sockaddr_in cliaddr,servaddr; listenfd=socket(AF_INET, SOCK_STREAM,0); if(listenfd==-1) printf("socket error \n"); else printf("socket is created for tcp connection %d \n",listenfd); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(1276); if(bind(listenfd,(struct sockaddr *)&servaddr, sizeof(servaddr))==-1) printf("bind error \n"); else printf("server bind to ip and port number \n"); listen(listenfd,5); maxfd=listenfd; maxi=-1; for(i=0;i<FD_SETSIZE; i++) client[i]= -1; FD_ZERO(&allset); FD_SET(listenfd,&allset); for(;;) {

rset=allset; nready=select(maxfd +1, &rset, NULL,NULL,NULL); if(FD_ISSET(listenfd,&rset)) { clilen=sizeof(cliaddr); connfd=accept(listenfd, (struct sockaddr *)&cliaddr, &clilen); printf("%s %dclient connected\n",inet_ntoa(cliaddr.sin_addr),connfd); printf("Port number %d\n",ntohs(cliaddr.sin_port)); for(i=0;i<FD_SETSIZE;i++) { if(client[i]<0) { client[i]=connfd; break; } } if(i==FD_SETSIZE) { printf("toomany clients"); exit(0); } FD_SET(connfd,&allset); if(connfd > maxfd) maxfd=connfd; if(i>maxi) maxi=i; if(--nready<=0) continue; } for(i=0;i<=maxi;i++) { if((sockfd=client[i]) <0) continue; if(FD_ISSET(sockfd,&rset)) { if( (n=read(sockfd, line, 1024))==0) { close(sockfd); FD_CLR(sockfd,&allset); client[i]= -1; } else write(sockfd,line, n);

if(--nready <=0) break; } } } }

Echo server using UDP protocol Udpclient.c

#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<fcntl.h> #include<string.h> int main() { int sockfd,fd,n; char buf[1024],filename[256]; struct sockaddr_in servaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; inet_pton(AF_INET,"192.168.11.98",&servaddr.sin_addr); servaddr.sin_port=htons(4324); while(1) { printf("Enter the Message\n"); gets(buf); if(strcmp(buf,"quit")==0) exit(0); sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&servaddr,sizeof(servaddr)); n=recvfrom(sockfd,buf,1024,0,NULL,NULL); write(STDOUT_FILENO,buf,n); } /*while(1) { if(strncmp(buf,"quit",4)==0) { close(sockfd); break; } n=recvfrom(sockfd,buf,1024,0,NULL,NULL); write(STDOUT_FILENO,buf,n); }*/ return 0; }

UDP server.c #include<stdio.h>

#include<fcntl.h> #include<unistd.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h> int main() { int sockfd,fd,n; socklen_t len; char filename[256],buff[1024]; 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(1276); inet_pton(AF_INET,"192.168.11.98",(struct sockaddr*)&servaddr.sin_addr); bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); while(1) { n=recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr,&len); buff[n]='\0'; sendto(sockfd,&buff,n,0,(struct sockaddr*)&cliaddr,len); if(strcmp(buff,"quit")==0) exit(0); printf("\nClient send message:: %s\n",buff); } /*if((fd=open(filename,O_RDONLY))==-1) { sendto(sockfd,"file does not exist",20,0,(struct sockaddr*)&cliaddr,len); sendto(sockfd,"quit",4,0,(struct sockaddr*)&cliaddr,len); close(sockfd); exit(0); } while((n=read(fd,&buff,1024))) { sendto(sockfd,&buff,n,0,(struct sockaddr*)&cliaddr,len); } sendto(sockfd,"quit",4,0,(struct sockaddr*)&cliaddr,len);*/ close(sockfd); return 0; }

FILE transfer using UDP UDP ftpclient.c

#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<fcntl.h> #include<string.h> int main() { int sockfd,fd,n; char buf[1024],filename[256]; struct sockaddr_in servaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; inet_pton(AF_INET,"10.2.216.2",&servaddr.sin_addr); servaddr.sin_port=htons(1227); while(1) { printf("Enter the Message\n"); gets(buf); if(strcmp(buf,"quit")==0) exit(0); sendto(sockfd,buf,strlen(buf),0,(struct sockaddr*)&servaddr,sizeof(servaddr)); n=recvfrom(sockfd,buf,1024,0,NULL,NULL); write(STDOUT_FILENO,buf,n); } return 0; }

UDP ftpserver.c #include<stdio.h> #include<fcntl.h> #include<unistd.h> #include<stdlib.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<string.h> int main() { int sockfd,fd,n; socklen_t len; char buff[1024]; 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(1227); inet_pton(AF_INET,"10.2.216.2",(struct sockaddr*)&servaddr.sin_addr); bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)); printf("bind success"); n=recvfrom(sockfd,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr,&len); buff[n]='\0'; if(strcmp(buff,"quit")==0) exit(0); if((fd=open(buff,O_RDONLY))==-1) { sendto(sockfd,"file does not exist",20,0,(struct sockaddr*)&cliaddr,len); sendto(sockfd,"quit",4,0,(struct sockaddr*)&cliaddr,len); close(sockfd); exit(0); } while((n=read(fd,&buff,1024))) { sendto(sockfd,&buff,n,0,(struct sockaddr*)&cliaddr,len); } sendto(sockfd,"quit",4,0,(struct sockaddr*)&cliaddr,len); close(sockfd); return 0; }

Server using both TCP and UDP protocols serverUDPTCP.c

#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<errno.h> #include<fcntl.h> #include<netdb.h> #include<signal.h> #include<string.h> #include<sys/stat.h> #include<sys/wait.h> #include<linux/poll.h> #include<sys/types.h> #define SA struct sockaddr int main() { int listenfd,connfd,udpfd,nready,maxi,len,i; char buff[1024]; pid_t childpid; ssize_t n; struct pollfd client[2]; struct sockaddr_in cliaddr,servaddr; void sig_chld(int); listenfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(9999); inet_ntop(AF_INET,"192.168.11.98",&servaddr.sin_addr,sizeof(se rvaddr.sin_addr)); if((bind(listenfd,(SA *)&servaddr,sizeof(servaddr)))==-1) printf("bind error\n"); else printf(" bind successful\n"); listen(listenfd,5); client[0].fd=listenfd; client[0].events=POLLRDNORM; udpfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(9999); inet_ntop(AF_INET,"192.168.11.98",&servaddr.sin_addr,sizeof(se rvaddr.sin_addr)); bind(udpfd,(SA *)&servaddr,sizeof(servaddr)); client[1].fd=udpfd; client[1].events=POLLRDNORM;

signal(SIGCHLD,sig_chld); for(;;) { nready=poll(client,2,-1); if(client[0].revents & POLLRDNORM) { len=sizeof(cliaddr); connfd=accept(listenfd,(SA *)&cliaddr,&len); if((childpid=fork()==0)) { close(listenfd); while((n=read(connfd,&buff,1024))) { if(strncmp(buff,"quit",4)==0) break; for(i=0;i<n;i++) buff[i]=toupper(buff[i]); write(connfd,buff,n); } exit(0); } close(connfd); } if(client[1].revents & POLLRDNORM) { len=sizeof(cliaddr); n=recvfrom(udpfd,buff,1024,0,(SA *) &cliaddr,&len); for(i=0;i<n;i++) buff[i]=toupper(buff[i]); sendto(udpfd,buff,n,0,(SA *)&cliaddr,len); } } return 0; } void sig_chld(int signo) { pid_t pid; int stat; pid=wait(&stat); printf("id %d is terminated\n",pid); return; }

IPC techniques File transfer using PIPEs concept Pipe.c

#include<stdio.h> #include<unistd.h> #include<string.h> #include<stdlib.h> # define max 1024 void client(int ,int ); void server(int ,int ); main() { int childpid,pipe1[2],pipe2[2]; if(pipe(pipe1) < 0 || pipe(pipe2) < 0) printf("pipe eror"); if((childpid=fork())<0) {printf("fork error"); }else if(childpid > 0) { close(pipe1[0]); close(pipe2[1]); client(pipe2[0],pipe1[1]); while(wait((int *) 0)!=childpid) close(pipe1[1]); close(pipe2[0]); exit(0); } else { close(pipe1[1]); close(pipe2[0]); server(pipe1[0],pipe2[1]); close(pipe1[0]); close(pipe2[1]); exit(0); } } void client(readfd,writefd) int readfd,writefd; { char buff[max];

int n; printf("enter a file name"); if(fgets(buff,max,stdin)==NULL) printf("reading error"); n=strlen(buff); if(buff[n-1]=='\n') n--; if(write(writefd,buff,n)!=n) printf("write error"); while((n=read(readfd,buff,max))>0) write(1,buff,n); if(n<0) printf("data read error"); } void server(readfd,writefd) int readfd,writefd; { char buff[max]; int n,fd; if((n=read(readfd,buff,max))<=0) printf("server: filename reading error"); buff[n]='\0'; if((fd=open(buff,0))<0) printf("file opening error"); else { while((n=read(fd,buff,max))>0) write(writefd,buff,n); } } FILE transfer using FIFO s concept FIFO.c #include<stdio.h> #include<unistd.h> #include<string.h> #include<stdlib.h>

#include<sys/types.h> #include<sys/stat.h> #define fifo1 "/tmp/fifo.1" #define fifo2 "/tmp/fifo.2" #define max 1024 void client(int ,int ); void server(int ,int ); main() { int childpid,readfd,writefd; if( (mknod(fifo1,S_IFIFO | 0666,0)<0)) printf("error"); if( (mknod(fifo2,S_IFIFO | 0666,0)<0)) printf("error"); if((childpid=fork())<0) printf("fork error"); else if(childpid>0) { writefd=open(fifo1,1); readfd=open(fifo2,0); client(readfd,writefd); while( wait((int *) 0) != childpid) ; close(readfd); close(writefd); unlink(fifo1); unlink(fifo2); exit(0); } else { readfd=open(fifo1,0); writefd=open(fifo2,1); server(readfd,writefd); close(readfd); close(writefd); exit(0); } } void client(readfd,writefd) int readfd,writefd; { char buff[max];

int n; printf("enter a file name"); if(fgets(buff,max,stdin)==NULL) printf("reading error"); n=strlen(buff); if(buff[n-1]=='\n') n--; if(write(writefd,buff,n)!=n) printf("write error"); while((n=read(readfd,buff,max))>0) write(1,buff,n); if(n<0) printf("data read error"); } void server(readfd,writefd) int readfd,writefd; { char buff[max]; int n,fd; if((n=read(readfd,buff,max))<=0) printf("\nserver: filename reading error\n"); buff[n]='\0'; if((fd=open(buff,0))<0) printf("\nfile opening error\n"); else { while((n=read(fd,buff,max))>0) write(writefd,buff,n); } }

Message transfer using message queues Receive.c #include #include #include #include <sys/types.h> <sys/ipc.h> <sys/msg.h> <stdio.h>

#include<stdlib.h> #define MSGSZ 128

/* * Declare the message structure. */ typedef struct msgbuf { long mtype; char mtext[MSGSZ]; } message_buf; main() { int msqid; key_t key; message_buf

rbuf;

/* * Get the message queue id for the * "name" 1234, which was created by * the server. */ key = 1234; if ((msqid = msgget(key, 0666)) < 0) { printf("mesgqid %d",msqid); exit(1); } printf("mesgqid %d",msqid); /* * Receive an answer of message type 1. */ if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0) { printf("eroor in msgrcv"); exit(1); } /* * Print the answer. */ printf("%s\n", rbuf.mtext); exit(0);

} Sened.c #include <sys/types.h> #include <sys/ipc.h> #include <sys/msg.h> #include <stdio.h> #include <string.h> #include<stdlib.h> #define MSGSZ 128

/* * Declare the message structure. */ typedef struct msgbuf { long mtype; char mtext[MSGSZ]; } message_buf; main() { int msqid; int msgflg = IPC_CREAT | 0666; key_t key; message_buf sbuf; size_t buf_length; /* * Get the message queue id for the * "name" 1234, which was created by * the server. */ key = 1234; printf("\nmsgget: Calling msgget\n"); if ((msqid = msgget(key, msgflg )) < 0) { printf("eroor at msgget"); exit(1); } else printf("msgget: msgget succeeded: msqid = %d\n", msqid);

/* * We'll send message type 1 */ sbuf.mtype = 1; (void) strcpy(sbuf.mtext, "hello , this is a message for ipcmessagequeue\n"); buf_length = strlen(sbuf.mtext) + 1 ;

/* * Send a message. */ if (msgsnd(msqid, &sbuf, buf_length, IPC_NOWAIT) < 0) { printf ("%d, %d, %s, %d\n", msqid, sbuf.mtype, sbuf.mtext, buf_length); printf("error msgsnd"); exit(1); } else printf("Message: \"%s\" Sent\n", sbuf.mtext); exit(0); }

You might also like