Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions