Question

In: Computer Science

Answer ONLY in C language. Server program: The server program provides a search to check for...

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:

  • Create a well-known FIFO, named FIFO_to_Server through which the server reads a struct from the client
  • Open FIFO_to_Server in READ mode
  • Read a request from a client that includes a struct containing a search value, the size of the integer array and the array itself, all integers.
  • Find the index (start counting with zero) of the search value, if one exists
  • Create a FIFO named FIFO_to_Client through which the server responds to the client
  • Open FIFO_to_Client in WRITE mode
  • Write the search value and the index position back to the client through FIFO_to_Client
  • Close FIFO_to_Client
  • Unlink FIFO_to_Client
  • Close FIFO_to_Server
  • Unlink FIFO_to_Server

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:

  • Open FIFO_to_Server in WRITE mode
  • Prompt the user for a search value
  • Prompt the user for an array size
  • Prompt the user for elements to fill that array
  • Write the struct that includes the search value, the array size and the array to FIFO_to_Server
  • Close FIFO_to_Server *
  • Open the FIFO_to_Client in READ mode
  • Read the response from the server from FIFO_to_Client
  • Print a message such as “The value 87 occurs in array position 4”
  • Close FIFO_to Client

Solutions

Expert Solution

#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");
}


Related Solutions

Please code in C language. Server program: The server program provides a search to check for...
Please code 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...
In C language Write a program that includes a function search() that finds the index of...
In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...
IN C LANGUAGE This program will read in a series of strings and print only the...
IN C LANGUAGE This program will read in a series of strings and print only the consonants (including Y) until the word "stop" appears. No string will be longer than 100 characters. A consonant is any letter that is not a vowel. Don't forget to follow the standard read pattern! Examples Enter a string: Hello Hll Enter a string: World! Wrld Enter a string: 123! Enter a string: stop Enter a string: stop
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
C LANGUAGE ONLY Write a C program to count the frequency of each element in an...
C LANGUAGE ONLY Write a C program to count the frequency of each element in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements of the array: element [0]: 25 element [1]: 12 element [2]: 43 Expected output: The frequency of all elements of an array: 25 occurs 1 times 12 occurs 1 times 3 occurs 1 times
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
IN C LANGUAGE ONLY PLEASE! First Program: create branchloop.c file Content of the program Enter your...
IN C LANGUAGE ONLY PLEASE! First Program: create branchloop.c file Content of the program Enter your name while running your program as you did with hello2.c (using argc and argv) name has to be 2 words max (e.g. Jose Lopez, Maria Rodrigues) . Make sure to check properly. Print your name 10 times using a loop. 10 is a fixed number. Count how many characters are in your first name and last name. If the number of characters of your...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT