I have to write a client and server programThis is the quest

I have to write a client and server program,This is the question for the server program.

(This is Java)

PART B Server Program: 1. Maintain the following information using an appropriate data structure of your choice (i.e., an array of a Class you defined). You do not have to place it in a file although you certainly can if you like. Item ID Item Description Unit Price Inventory 00001 New Inspiron 15 $379.99 157 00002 New Inspiron 17 $449.99 128 00003 New Inspiron 15R $549.99 202 00004 New Inspiron 15z Ultrabook $749.99 315 00005 XPS 14 Ultrabook $999.99 261 00006 New XPS 12 UltrabookXPS $1199.99 178 2. Wait for receiving a packet from a Client. 3. Once a packet is received from a Client, retrieve the information relevant to the requested Item ID from the data structure you used in Step 1 and send back such information to the Client. 4. Repeat Step 3 infinitely until an exception is caught. 5. Close the datagram socket.

Solution

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

class Inventory_Base
{
String Item_ID;
String Item_Description;
String Unit_Price;
int Inventory;
  
Inventory_Base(String id, String desc,String price,int n)
{
Item_ID = id;
Item_Description = desc;
Unit_Price = price;
Inventory = n;
}

public String getItem_ID()
{
return Item_ID;
}

public String getDetails()
{
return Item_ID + \", \" + Item_Description + \", \" + Unit_Price + \", \" + Inventory;
}
}


public class Server
{

private static Socket socket;

public static void main(String[] args)
{
try
{

Inventory_Base stock[] = new Inventory_Base[6];

stock[0] = new Inventory_Base(\"00001\", \"New Inspiron 15\", \"$379.99\", 157);
stock[1] = new Inventory_Base(\"00002\", \"New Inspiron 17\", \"$449.99\", 128);
stock[2] = new Inventory_Base(\"00003\", \"New Inspiron 15R\", \"$549.99\", 202);
stock[3] = new Inventory_Base(\"00004\", \"New Inspiron 15z Ultrabook\", \"$749.99\", 315);
stock[4] = new Inventory_Base(\"00005\", \"XPS 14 Ultrabook\", \"$999.99\", 261);
stock[5] = new Inventory_Base(\"00006\", \"New XPS 12 UltrabookXPS\", \"$1199.99\", 178);   

int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println(\"Server Started and listening to the port 25000\");

//Server is running always. This is done using this while(true) loop
while(true)
{
//Reading the Item_ID from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String id = br.readLine();
String returnMessage = \"\";

for(int i=0; i<6; i++)
{
if(stock[i].getItem_ID().equals(id))
{
returnMessage = stock[i].getDetails();
break;
}
else
returnMessage = \"*** Item_ID not found ***\";
}

//Sending the response back to the client.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage);
bw.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}

I have to write a client and server program,This is the question for the server program. (This is Java) PART B Server Program: 1. Maintain the following informa
I have to write a client and server program,This is the question for the server program. (This is Java) PART B Server Program: 1. Maintain the following informa

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site