I’m able to pass a struct through my fifo, but how would I go about making my program keep on going back and forth from the client to the server until the client decides to exit the program.
I’ve tried it with a while loop but it just doesn’t seem to work. Any help would be appreciated.it will run the program but wont pass the data to the through the fifo. Without the while loop the data is passed. any help or tips will be appreciated
Client Program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
struct Client {
int clientNum;
int systemCall;
int num;
int result;
} client;
int main(int argc, char* argv[]) {
if (mkfifo("ClientFifo", 0777) == -1) {
if (errno != EEXIST) {
printf("Could not create fifo file\n");
return 1;
}
printf("Opened\n");
int fd = open("ClientFifo", O_RDONLY);
if (fd == -1) {
return 1;
}
//while statement
while(1) {
printf("Reading\n");
read(fd, &client, sizeof(client));
printf("Client number: %d\n", client.clientNum);
printf("System call number: %d\n", client.systemCall);
printf("Parameter number %d\n", client.num);
printf("Result %d\n", client.result);
close(fd);
} //while
return 0;
} //end if
} //end main
Server Program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
struct Client {
int clientNum;
int systemCall;
int num;
int result;
} client;
struct Client get_student_data()
{
struct Client s;
s.clientNum = getpid();
printf("Client Number:%d", s.clientNum);
printf("\n");
printf("Enter System Call number:");
scanf("%d", &s.systemCall);
printf("Enter Number:");
scanf("%d", &s.num);
printf("system call:%d\n", s.systemCall);
switch (s.systemCall)
{
case 1:
printf("Youre in case 1\n");
s.result = s.num * s.num;
break;
case 2:
printf("Youre in case 2\n");
s.result = s.num * s.num * s.num;
break;
default:
printf("INVALID CHOICE\n");
s.result = 0;
break;
}
return s;
}
int main(int argc, char* argv[]) {//main
//makes fifo
if(mkfifo("ClientFifo", 0777) == -1) {
//error message in case fifo isnt created
if(errno != EEXIST) {
printf("Could not create fifo file\n");
return 1;
}//end error checking
//area to do work
printf("\n");
//while Statement
while(client.systemCall != 0) {
struct Client c1 = get_student_data();
printf("\nClient number: %d\n",c1.clientNum);
printf("System call number: %d\n",c1.systemCall);
printf("Paramet number: %d\n",c1.num);
printf("Result number: %d\n",c1.result);
//opens and closes FIFO
printf("Opening...\n");
int fd = open("ClientFifo", O_WRONLY);
printf("Opened\n");
if(write(fd, &c1, sizeof(c1)) == -1) {
return 2;
}
//}//ends write
//Closes fd
printf("Written..\n");
close(fd);
}
return 0;
} //ends if fifo
} //ends main
Placed while loop where placed in code but doesn't run correctly