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

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

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

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

  • in C++ please ELET 2300 Programming Assignment # 2 Write a program that generates an array...

    in C++ please ELET 2300 Programming Assignment # 2 Write a program that generates an array filled up with random positive integer number ranging from 15 to 20, and display it on the screen. After the creation and displaying of the array, the program displays the following: [P]osition [Reverse, [A]verage, (Search, [Q]uit Please select an option: Then, if the user selects: -P (lowercase or uppercase): the program displays the array elements position and value pairs for all member elements of...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

  • Write a C program to do the following: 1. Write a C function named find that...

    Write a C program to do the following: 1. Write a C function named find that receives a one-dimensional array of type character named arr and its size of type integer. After that, the function must select a random index from the array. The function must find if the character stored in the randomly selected index comes before all the characters to its left alphabetically. Finally, the function prints to the screen the element if the element meets the condition...

  • Can anyone help me with my C hw? Exercise 3 You will write a new program...

    Can anyone help me with my C hw? Exercise 3 You will write a new program that combines dynamically allocating an array and saving that array to a file. These are the tasks your program must perform Open an output file named "data.txt" and prepare it for writing in text mode o If the file handle is NULL, quit the program o By default, it is created and stored in the same directory as your source code file Prompt the...

  • this program is in C. Write a program that computes the number of elements in an...

    this program is in C. Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2,2,4}). Sample execution of the program for this array...

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
ADVERTISEMENT