Question: Resuming your Client-Server java practice, For its part, the client must request the necessary data to create an instance of the Product class. Once the

Resuming your Client-Server java practice, For its part, the client must request the necessary data to create an instance of the Product class. Once the instance is created, the client must send it to the server so that it store it in a file (You can save it as an object or as a string, you choose). Once stored in the file, the server should display all the products in the file (consider using the toString method of the class Product for this point).

classes:

public class Product { private String name; private double price;

public Product(String name, double price) { this.name = name; this.price = price; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public double getPrice() { return price; }

public void setPrice(double price) { this.price = price; }

@Override public String toString() { return "Product{" + "name=" + name + ", price=" + price + '}'; } }

-----------------------------------------

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

public class Server {

public static void main(String[] args) throws IOException {

// Create a server socket

ServerSocket serverSocket = new ServerSocket(8888);

System.out.println("Waiting for connection...");

// Listen for a connection request

Socket socket = serverSocket.accept();

System.out.println("Connection established.");

// Create streams for communication

BufferedReader in = new BufferedReader(

new InputStreamReader(socket.getInputStream()));

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

// Receive messages from the client

String line;

while ((line = in.readLine()) != null) {

if (line.equalsIgnoreCase("goodbye")) {

out.println("Goodbye");

break;

}

System.out.println("Client: " + line);

out.println(line + " This is an automatic reply to your message");

}

// Close the connection

socket.close();

}

}

-----------------------------------

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

import java.util.Scanner;

public class Client {

public static void main(String[] args) throws IOException {

// Create a socket

Socket socket = new Socket("localhost", 8888);

System.out.println("Connected to the server");

// Create streams for communication

BufferedReader in = new BufferedReader(

new InputStreamReader(socket.getInputStream()));

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

// Write messages to the server

Scanner scanner = new Scanner(System.in);

String line;

BufferedWriter bw = new BufferedWriter(new FileWriter("conversation.txt"));

while (true) {

System.out.println("Write a message to the server: ");

line = scanner.nextLine();

out.println(line);

bw.write("Client: " + line + " ");

if (line.equalsIgnoreCase("goodbye")) {

break;

}

line = in.readLine();

System.out.println("Server: " + line);

bw.write("Server: " + line + " ");

}

// Close the connection

socket.close();

scanner.close();

bw.close();

}

}

The code must be in java

????

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!