In: Computer Science
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:
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:
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!!!!!!!!----------