#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h>
int main(int argc, char **argv)
{
char x[10];
char the_string[15] = {};
short size = 7;
int variant = 62307;
long number = 0;
short size_field = 0;
int length =0;
int *pInt;
int shmId;
strcpy(x, "CONNECT");
if (mkfifo("RESP_PIPE_62307", 0640) < 0 )
{
perror("Error creating the pipe");
exit(1);
}
int fd1 = open("REQ_PIPE_62307", O_RDONLY);
if (fd1 < 0)
{
perror("Error opening the request pipe");
exit(1);
}
int fd = open("RESP_PIPE_62307", O_WRONLY);
if (fd < 0)
{
perror("Error opening the response pipe");
exit(1);
}
write(fd, &size, 1);
write(fd, x, sizeof(char)*size);
read(fd1, &size_field, 1);
read(fd1, &the_string, size_field);
length =4;
printf("SUCCESS\n");
while (strstr(the_string,"EXIT") == NULL)
{
if (strstr(the_string,"PING") != NULL)
{
printf("PING HAS BEEN READ\n");
write(fd, &length, 1);
write(fd, "PING", size_field);
write(fd, &length, 1);
write(fd, "PONG", size_field);
write(fd, &variant, sizeof(int));
}
if (strstr(the_string,"CREATE_SHM") != NULL)
{
printf("CREATE SHM HAS BEEN READ\n");
read(fd1, &number, sizeof(long));
printf("the number read is shm is : %ld\n", number);
shmId = shmget(17293, number, IPC_CREAT|0664);
printf("the shm id = %d\n", shmId);
if (shmId < 0)
{
write(fd, &size_field, 1);
write(fd, "CREATE_SHM", size_field);
size_field = 5;
write(fd, &size_field, 1);
write(fd, "ERROR", size_field);
size_field = 10;
}
else
{
pInt = (int*) shmat(shmId, 0, 0);
if (*pInt == -1)
perror("Error attaching shared memory area");
write(fd, &size_field, 1);
write(fd, "CREATE_SHM", size_field);
size_field = 7;
write(fd, &size_field, 1);
write(fd, "SUCCESS", size_field);
size_field= 10;
}
}
read(fd1, &size_field, 1);
read(fd1, &the_string, size_field);
printf(" -->%s <-- \n", the_string);
}
shmctl(shmId, IPC_RMID, 0);
shmdt(pInt);
close(fd1);
close(fd);
return 0;
}