Question: Write the code for a client that sends the server an integer and a private FIFO name through a common FIFO. Invoke this client 3

Write the code for a client that sends the server an integer and a private FIFO name through a common FIFO. Invoke this client 3 separate times. Write the code for a server that adds the integers that it receives from all three clients and sends the sum back to all 3 clients through their private FIFOs.

This is my server and client code. I cannot seem to get the proper counts printing on the server, it spits out 0 for all clients. How can I get the proper counts to print on the server coming from the clients?

SERVER CODE

#include

#include

#include

#include

#include

#include

#include

#include

#include

main (void)

{

struct values

{

char privateFIFO[20];

char name[8];

int count;

} ;

int fda; // to read from client

int fdb; // to write to client

int finish; // lets me know that client is done

int i; // because C needs this defined as int

int clientSum = 0;

int clients;

struct values input;

int x = 0;

printf(" How many clients will be accessing the server: ");

scanf("%d", &clients);

for (x=0; x

{

/* Create the fifo and opens it */

if ((mkfifo("commonFIFO",0666)<0 && errno != EEXIST)) >

{

perror("cant create commonFIFO");

exit(-1);

}

if((fda=open("commonFIFO", O_RDONLY))<0) //open commonFIFO

printf("cant open fifo to write");

read(fda, &input, sizeof(input)); //read struct

printf(" Server just got an integer: %d", input);

fdb = open(input.privateFIFO, O_WRONLY); //open privateFIFO

write(fdb, &input, sizeof(input));

if(finish == 1)

printf(" Server: This says I am ready to close ");

close(fda);

close(fdb);

unlink("commonFIFO");

}

printf(" Sum is: %d", clientSum);

printf(" Server Complete: All connections closed. ");

}

CLIENT CODE

#include

#include

main (void)

{

int fda; // to write to server

int fdb; // to read response from server

int clientSum;

int name;

int count;

struct values

{

char privateFIFO[20];

char name[8];

int count;

} input;

printf("Client: Please enter a name: ");

scanf("%s", &input.name);

printf(" Client: Please enter a count: ");

scanf("%d", &input.count);

if((fda=open("commonFIFO", O_WRONLY))<0) //write into FIFO

printf("cant open fifo to write");

if((fdb=open("commonFIFO", O_RDONLY))<0) //read into FIFO

printf("cant open fifo to read");

write(fda, &input, sizeof(input));

printf(" Client: Got the integer sent, now waiting for response ");

read(fdb, clientSum, 7);

printf(" Client: Sum is %d", clientSum);

close(fda);

close(fdb);

printf (" all done! ");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!