Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/shm.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- char buff[100];
- char dim[2];
- unsigned char size;
- int main()
- {
- mkfifo("RESP_PIPE_50157",0600);
- int ok=1;
- int fd=open("REQ_PIPE_50157",O_RDONLY);
- if(fd<0)
- {
- perror("Error opening pipe");
- ok=0;
- }
- int myfd=open("RESP_PIPE_50157",O_RDWR);
- if(myfd<0)
- {
- perror("Error opening pipe2");
- ok=0;
- }
- //write(myfd,"07434f4e4e454354",16);
- if(ok==1)
- {
- printf("SUCCESS");
- size=strlen("CONNECT");
- write(myfd,&size,sizeof(unsigned char));
- write(myfd,"CONNECT",7);
- }
- while(strcmp(buff,"EXIT")!=0)
- {
- read(fd,&dim,sizeof(unsigned char));
- read(fd,buff,*dim);
- if(strcmp(buff,"PING")==0)
- {
- size=strlen("PING");
- write(myfd,&size,sizeof(unsigned char));
- write(myfd,"PING",4);
- size=strlen("PONG");
- write(myfd,&size,sizeof(unsigned char));
- write(myfd,"PONG",4);
- int nr=50157;
- write(myfd,&nr,sizeof(nr));
- }
- if(strcmp(buff,"CREATE_SHM")==0)
- {
- unsigned int memsize=0;
- int ok1=1;
- size=strlen("CREATE_SHM");
- write(myfd,&size,sizeof(unsigned char));
- write(myfd,"CREATE_SHM",4);
- read(myfd,&memsize,sizeof(unsigned int));
- int shmid=shmget(15603,memsize, IPC_CREAT|664);
- if(shmid<0)
- {
- size=strlen("ERROR");
- write(myfd,&size,sizeof(unsigned char));
- write(myfd,"ERROR",5);
- ok1=0;
- }
- int* sharedMem=(int*)shmat(shmid,NULL,0);
- if(sharedMem<0)
- {
- size=strlen("ERROR");
- write(myfd,&size,sizeof(unsigned char));
- write(myfd,"ERROR",5);
- ok1=0;
- }
- if(ok1==1)
- {
- size=strlen("SUCCESS");
- write(myfd,&size,sizeof(unsigned char));
- write(myfd,"SUCCESS",7);
- }
- }
- if(strcmp(buff,"EXIT")==0)
- {
- close(fd);
- // unlink("RESP_PIPE_50157");
- close(myfd);
- }
- }
- // close(fd);
- //close(myfd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement