Question

Write two C programs that run a server program and a client program concurrently. Server program:...

Write two C programs that run a server program and a client program concurrently.

Server program:

The server program provides a simple search for a specific value in an array sent to it from a client. If the value appears in the array, the server indicates the index of the first occurrence of that value in the array. The server sends the client search value and its array position. If the value does not occur in the array, only the search value and an appropriate return code is returned to the client.

The server program needs to:

  • Determine an integer value to be searched for
  • Create a well-known FIFO, named FIFO_to_Server through which the server reads inputs from the client
  • Open FIFO_to_Server in READ mode
  • Read a request from a client that includes a struct containing a size integer and array values of that size, 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 will request 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 an array size
  • Prompt the user for elements to fill that array
  • Write the struct that includes 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Client:
// ClientTest.c
// opens fifo1 for writing and fifo2 for reading


#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; 
  int fda;  // to write to character server
  int fdb;  // to read response from character server
  int i;    // for the iteration

  memset(&x, 0, sizeof(struct problem)) ;


  printf("Client: Please enter a character: ");
  scanf("%c", x.letter); 
  printf("Client: Please enter an integer: ");
  scanf("%d", x.number);  

// removed or else the letter and number would be reverted to zero
 // memset(&x.letter, 0, sizeof(char));
//  memset(&x.number, 0, sizeof(int));  


  if((fda=open("FIFO_to_Server", O_WRONLY))<0)//opening and validating fifos
     printf("cant open fifo to write");

  if((fdb=open("FIFO_to_Client", O_RDONLY))<0)
     printf("cant open fifo to read");

  write(fda, x.letter, sizeof(char));
  printf("\nClient: Got the character sent, now waiting for response ");

  write(fda, x.number, sizeof(int));
  printf("\nClient: Got the integer sent, now waiting for response ");
  //sleep(0.250);

  char outletter[7];

  read(fdb, outletter, 7);
  printf("\nClient: received characters from server:") ;
  printf("%s\n", outletter);

  close(fda);
  close(fdb);


  printf ("\nall done!\n");

}

Server:

// ServerTest.c
// makes 2 fifos named fifo1 and fifo2
// opens fifo1 for reading and fifo2 for writing

#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, sizeof(char));
  memset(x.number, 0, sizeof(int));


  /* Create the fifos and open them  */
  if ((mkfifo("FIFO_to_Server",0666)<0 && errno != EEXIST))
    {
    perror("cant create FIFO_to_Server");
    exit(-1);
    }
  if ((mkfifo("FIFO_to_Client",0666)<0 && errno != EEXIST))
    {
    perror("cant create FIFO_to_Client");
    exit(-1);
    }
  if((fda=open("FIFO_to_Server", O_RDONLY))<0)
     printf("cant open fifo to write");
  if((fdb=open("FIFO_to_Client", O_WRONLY))<0)
     printf("cant open fifo to read");

  read(fda, x.letter, sizeof(char)); //read the character
  read(fda, x.number, sizeof(int));   //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];



  if (p > 6) p = 6;  // Cannot write more than 6 characters in outletter



  for( i = 0; i<=p; i++) {               // iteration to create the character's string
    outletter[i] = x.letter[0];
    printf("iteration: %d and character: %c\n", i, outletter[i]); // validating the character and integer received
  }
  outletter[p] = '\0'; // the string must finish with '\0'


  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("FIFO_to_Server");
  unlink("FIFO_to_Client");
}
Add a comment
Know the answer?
Add Answer to:
Write two C programs that run a server program and a client program concurrently. Server program:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write two programs Client and Server

    Given two integer matrices A and B, you are requested to compose a program to perform matrix addition (A + B). Both matrices have N rows and M columns; N > 1, M > 1; You need to divide both (A and B) into four equal (or close to equal) size of submatrices (A0,0, A0,1, A1,0, A1,1 and B0,0, B0,1, B1.0, B1.1)andeachsubmatrixhasdimensioncloseto(N/2)x(M/2). YouneedtocreatefourJavathreads each thread performs a subset of addition on one pair of the submatrices. Example, thread 0 performs addition...

  • Objective: Create programs that are run independently and can use simple IPC using FIFOs/named pipes to...

    Objective: Create programs that are run independently and can use simple IPC using FIFOs/named pipes to pass information back and forth in a client/server fashion. The information/request from the client will look like simple semaphore system calls and will include information for the server to be able to identify which client requested it and what request it is and its associated parameters. This assignment requires the analysis, implementation, testing and documentation of two C program that use C on the...

  • Assignment One program will be the update server and the other will be the update client....

    Assignment One program will be the update server and the other will be the update client. Here is how the two programs should interact with each other: Server 1. the server starts up and reads the version number in the data.bin file and stores it in memory. 2. The server binds to a port and awaits connections. Client 1. The client starts up and it will first read the version number found within it's own data.bin file. 2. The client...

  • Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array...

    Here is the description of the client and server programs that you need to develop in C using TCP: Suppose we have a simple student query system, where the server keeps student's info in an array of struct student_info ( char abc123171 char name [101 double GPA; To simplify the tasks, the server should create a static array of 10 students with random abc123, name, and GPA in the server program. Then the server waits for clients. When a client...

  • 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 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...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a program that will create an array of ten integers, allow the user to enter...

    Write a program that will create an array of ten integers, allow the user to enter those integers, and eventually search through the array based on index position: 1. Declare the array. You cannot assign elements to an array until it has first been created. 2. Next, run a for loop so that the user can enter an integer to save in each index position of the array. Since you this array holds ten elements , the index position starts...

  • C++ programming please Write a program that will display random numbers in order from low to...

    C++ programming please Write a program that will display random numbers in order from low to high. 1. Declare an integer array of size 100. Ask the user how many numbers to work with (n). It can be less than 100. 2. Generate random numbers from 10 to 50. 3. Write the random numbers to an output file named random.dat. Close the file. 4. Open random.dat for input and read the data values into your array. Pass your array to...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT