Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C PROGRAMMING Write the code for client and server. The client will send server

ID: 3831070 • Letter: C

Question

C PROGRAMMING

Write the code for client and server. The client will send server 3 integers and its private FIFO name. The server adds the integers that it gets from all three clients and sends the sum back to all the client through its private FIFO.

Please correct the code that I have below

//=============Client=================

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

struct Value{
   int num1[1];
   int num2[1];
   int num3[1];
   int sumAll[1];
};

struct String{
   char privateFIFO[14];
   int intbuff;
};

main (void)
{
   int fda;
   int fdb;
   int clientID;
   char temp[14];
   struct Value values;
   struct String input;
  
   memset(values.num1,0,1);
   memset(values.num2,0,1);
   memset(values.num3,0,1);
   memset(values.sumAll,0,1);
   memset(input.privateFIFO,0,14);
  
   clientID = getpid();
   strcpy(input.privateFIFO, "FIFO_"); //get name of private FIFO
   sprintf(temp, "%d", clientID);
   strcat(input.privateFIFO, temp);
   printf(" FIFO name is %s ", input.privateFIFO);
   write(fda,&input,sizeof(input));
  
if((fda=open("to_Server_FIFO", O_WRONLY))<0)
   {
printf("Can't open to_Server_FIFO to write");
}  
  
   //First client
   printf(" Client: Please enter num1: ");
   scanf("&d",&values.num1);
  
   //Second client
   printf(" Client: Please enter num2: ");
   scanf("&d",&values.num2);
  
   //Third Client
   printf(" Client: Please enter num3: ");
   scanf("&d",&values.num3);
  

   write(fda,&values,sizeof(values));
  
   if((fdb=open(input.privateFIFO, O_RDONLY))<0) {
       printf("Can't open to_Client_FIFO to read");
   }
   read(fdb, &values, sizeof(values));
  
   printf("Sum is: %d", values.sumAll[0]);
  
   close(fda);
   close(fdb);
  
   printf(" All done! ");
      
}

//==============Server==============

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

struct Value{
   int num1[1];
   int num2[1];
   int num3[1];
   int sumAll[1];
};

struct String{
   char privateFIFO[14];
   int intbuff;
};

main (void){
   int fda;
   int fdb;
   int total;
   int clientID;
   char temp[14];
   struct Value values;
   struct String input;
  
   /* Create the fifos and open them */
   if ((mkfifo("to_Server_FIFO", 0666) < 0 && errno != EEXIST)) {
       perror("Can't create to_Server_FIFO");
       exit(-1);
   }

   if ((mkfifo("to_Client_FIFO", 0666) < 0 && errno != EEXIST)) {
       perror("Can't create to_Client_FIFO");
       exit(-1);
   }

   if ((fda = open("to_Server_FIFO", O_RDONLY)) < 0) {
       printf("Can't open to_Server_FIFO to read");
   }

   if ((fdb = open("to_Client_FIFO", O_WRONLY)) < 0) {
       printf("Can't open to_Client_FIFO to write");
   }
  
   read(fda,&input,sizeof(input));
   read(fda,&values,sizeof(values));
   memset(values.num1,0,1);
   memset(values.num2,0,1);
   memset(values.num3,0,1);
   memset(values.sumAll,0,1);
   memset(input.privateFIFO,0,14);
   strcpy(input.privateFIFO,input.privateFIFO);
  
   total = values.num1[0] + values.num2[0] + values.num3[0];
   values.sumAll[0] = total;
   printf("Server: num1 is: &d",values.num1[0]);
   printf("Server: num2 is: &d",values.num2[0]);  
   printf("Server: num3 is: &d",values.num3[0]);
  
if ((mkfifo(input.privateFIFO,0666)<0 && errno != EEXIST))
{
perror("cant create client fifo");
exit(-1);
}

if((fdb=open(input.privateFIFO, O_WRONLY))<0)
printf("cant open %s to read", input.privateFIFO);  
  
   write(fdb,&values,sizeof(values));
   close(fda);
   close(fdb);
   unlink("FIFO1");
   unlink(input.privateFIFO);

}

Explanation / Answer

You have created a mess of code and could lead to great confusion. Please create 4 structs with Client-1,2,3,4 and let us suppose we have the clemt 4 is the final one then Client-1 as struct 1 and so on.. and send that to the struct 4,

post this we could schedule the whole code in the FIFO method, the provided code is un-understandable