Question

In: Computer Science

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

Solution:

Note: In this program we use java programming language so the struct in c++ is act as a class in java.

Algorithm:

1. Create ServerSocket object in server file.

2. By using ServerSocket object call accept() method.

3. Create Socket object in client file.

4. Create scanner object for get input from user.

5.Create DataOutputStream and DataInputStream objects.

6. Get serach value, array size and array elements from user.

7. Send that information to server by using DataOutputStream object.

8. Receive data on server using DataInputStream object.

9. Search element in the given array if it is found then send index if array and if it is not present send -1 to the client using DataOutputStream object.

10. Get the sed data from server at client side and display respected output.

Code:

FIFO_to_Server.java

import java.net.*;
import java.io.*;
import java.util.*;

class FIFO_to_Server
{
   public static void main(String[] args) throws IOException {
       int arrFromClient[];
       int i,ch,size,index=-1;

ServerSocket welcomeSocket = new ServerSocket(6786);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
while(true){
Socket connectionSocket = welcomeSocket.accept();
System.out.println("Client accepted");
DataInputStream inFromClient = new DataInputStream(connectionSocket.getInputStream());
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
ch=inFromClient.readInt();
size=inFromClient.readInt();
arrFromClient=new int[size];
for(i=0;i<size;i++)
{
arrFromClient[i] = inFromClient.readInt();
}
for(i=0; i< arrFromClient.length; i++)
{
   if(arrFromClient[i]==ch)
   {
       index=i;
       outToClient.writeInt(index);
       System.exit(0);

   }

  
}
outToClient.writeInt(index);

   

}
   }
   }

FIFO_to_Client.java

import java.net.*;
import java.io.*;
import java.util.*;
class FIFO_to_Client
{
   public static void main(String[] args) throws IOException{
   int arr[];
   int ch;
   int size,index;
      
Scanner sc= new Scanner(System.in);
try (Socket clientSocket = new Socket("localhost",6786)) {
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
DataInputStream inFromServer = new DataInputStream(clientSocket.getInputStream());
System.out.println("Enter the search value:");
ch=sc.nextInt();
System.out.println("Enter the size of Array:");
size=sc.nextInt();
arr=new int[size];
System.out.println("Enter the Array elements:");
for(int i=0;i<size;i++)
arr[i]=sc.nextInt();

outToServer.writeInt(ch);
outToServer.writeInt(size);

for(int i=0;i<size;i++)
outToServer.writeInt(arr[i]);

index=inFromServer.readInt();
if(index==-1)
{
   System.out.println("The search value "+ch+" is not found in array");
   System.exit(0);
}
else
{
   System.out.println("The value "+ch+" is occurs in array position "+index);
   System.exit(0);
}

  
}
}

   }

Output:

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of computers sold...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of computers sold by three vendors in four different zones. Is required:  Request the sale amount of each seller by zone, the values must be entered through the keyboard and validate that it does not accept negative numbers.  Menu that requests the operation to be carried out. In case of entering an invalid data, send an error message and request the operation again.  Option...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of votes obtained...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of votes obtained by 3 amounts in five different zones. Is required: + Request the total of votes by zone of each candidate, the values must be entered on the keyboard and validate that it does not accept negative numbers + Menu that requests the operation to be performed, in case of entering an invalid data send an error message and request the operation again + Option...
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search...
[C++ Language] Look at the following pseudo code: Binary_search(int a[], int size) { ……….// binary search and return } Selection_Sort(int a[], int z) { …..// do the selection sort } main() {     Selection_Sort(array, size);     Binary_Search(array, item); }
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory...
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory If(shared memory not full) { //sendthe string to the shared memory //send signal to process; }
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
PROGRAM LANGUAGE IN C, PLEASE KEEP IT SIMPLE EVEN IF IT IS REDUNDANT In this problem...
PROGRAM LANGUAGE IN C, PLEASE KEEP IT SIMPLE EVEN IF IT IS REDUNDANT In this problem you will take a list of names from the user and sort them alphabetically using selection sort. The code for selection sort for integer is available in the "Lab and Weekly Coding Solution module" in webcourses. Ask the user how many names the user wants to input. Let's say the number be N. Then take N number of names and put them in an...
can someone please check this code? – Enter Quantity of Numbers Design a program that allows...
can someone please check this code? – Enter Quantity of Numbers Design a program that allows a user to enter any quantity of numbers until a negative number is entered. Then display the highest number and the lowest number. For the programming problem, create the pseudocode and enter it below. Enter pseudocode here start     Declarations           num number           num high             num low              housekeeping()     while number >=0 detailLoop()      endwhile      finish() stop housekeeping( )           output...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT