In: Computer Science
Answer ONLY in C language.
Server program:
The server program provides a search to check for a specific value in an integer array. The client writes a struct containing 3 elements: an integer value to search for, the size of an integer array which is 10 or less, and an integer array to the server through a FIFO. The server reads the struct from the FIFO and checks the array for the search value. If the search value appears in the array, the server indicates the index of the first occurrence of that value in the array. The server writes back to the client a struct that contains the search value and its array position. If the value does not occur in the array, the search value and an appropriate return code are returned to the client. (An appropriate return code for not finding the search value could be -1)
The server program needs to:
Client Program:
The client program requests an integer value to search for, an array size and array elements to fill the array. The client program should:
#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 problem {
char letter[1];
int number[1];
};
main (void)
{
struct problem x=0; //Initializing structure to null
int fda; // to write to character server
int fdb; // to read response from character server
int i; // for the iteration
printf("Client: Please enter a character: ");
scanf("%c", &x.letter[0]);
printf("Client: Please enter an integer: ");
scanf("%d", &x.number[0]);
memset(&x.letter, 0, 1);
memset(&x.number, 0, 1);
#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 problem {
char letter[1]; // struct to store the character
int number [1]; // struct to store the integer
};
main (void)
{
struct problem x;
int fda; // to read from client char
int fdb; // to write to client char
int finish; // lets me know that client is done
int i; // because C needs this defined as int
int p;
char outletter[7];
memset(&x.letter, 0, 7);
memset(&x.number, 0, 1);
/* Create the fifos and open them */
if ((mkfifo("FIFO1",0666)<0 && errno != EEXIST))
{
perror("cant create FIFO1");
exit(-1);
}
if ((mkfifo("FIFO2",0666)<0 && errno != EEXIST))
{
perror("cant create FIFO2");
exit(-1);
}
if((fda=open("FIFO1", O_RDONLY))<0)
printf("cant open fifo to write");
if((fdb=open("FIFO2", O_WRONLY))<0)
printf("cant open fifo to read");
read(fda, x.letter, 1); //read the character
read(fda, x.number, 1); //read the integer
printf("\nServer: just got character: , %c", x.letter[0]);
printf("\nServer: just got integer: , %d", x.number[0]);
p=x.number[0]-'0';
for( i = 0; i<=p; i++) // iteration to create the character's
string
outletter[p] = x.letter[0];
printf("iteration: %d and character: %c\n", i, outletter[0]); //
validating the character and integer received
printf("\nServer: outchar is, %s", outletter); // this shows the
character to be sent back to client
write(fdb, outletter, p);
printf("\nServer: Got the characters sent: %s", outletter ); //
this sends the letter back to client
if(finish == 1)
printf("\nServer: This says I am ready to close ");
close(fda);
close(fdb);
unlink("FIFO1");
unlink("FIFO2");
}