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

Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking...
Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking integers until the user enters -100. In the end, the program should display the count of positive, negative (excluding that -100) and zeros entered. Sample Input/Output 1: Input the number: 0 2 3 -9 -6 -4 -100 Number of positive numbers: 2 Number of Negative numbers: 3 Number of Zero: 1
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...
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.
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; }
Language: C and comment code please PThreads The goal of this homework is to show an...
Language: C and comment code please PThreads The goal of this homework is to show an understanding of pthreads and C. Assignment: Write a C program to use the bitwise operations with each bitwise operation contained within its own thread. Each thread should take 1 parameter and will only modify global values. First, create global integers x and y, then, in the main function you will read in values for x and y from the user and then create the...
C language only please and please make a simple code Write a function that will find...
C language only please and please make a simple code Write a function that will find whether there exist two integers that sum to the target integer. The function is to “return” three values.First, return “1” if the integers were found,return “-1” if your search was not successful.If you find two integers which add up to the target value, you should return their respective index position inside the array. Suggested prototype:int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2);Inside...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT