Java Swing Programming

User Generated

byyvrcbyyvr

Programming

Advance Java

colorado technical university online

Description

Modify your server to make it Your server should allow more than one client to connect and request product and customer data. Test your server by connecting more than one client to the server. Take at least 4 screenshots to demonstrate that your server correctly responds to each client's request.

Unformatted Attachment Preview

import import import import import import import import import import import import import import java.awt.BorderLayout; java.awt.Font; java.awt.GridLayout; java.awt.event.ActionEvent; java.awt.event.ActionListener; java.io.ObjectInputStream; java.io.ObjectOutputStream; java.net.Socket; javax.swing.JButton; javax.swing.JFrame; javax.swing.JOptionPane; javax.swing.JPanel; javax.swing.JScrollPane; javax.swing.JTextArea; //using JFrame and JPanel public class Client extends JFrame implements ActionListener { /** * @param args the command line arguments */ public static void main(String[] args) { new Client(); } JTextArea outputArea = new JTextArea(25, 120); JButton customerButton = new JButton("Get Customer Details"); JButton productButton = new JButton("Get Product Details"); //for scroll bars JScrollPane jsp = new JScrollPane(outputArea); Client() { super("Client"); //using different types of layouts setLayout(new BorderLayout()); JPanel jp1 = new JPanel(new GridLayout(0, 2, 5, 5)); jp1.add(customerButton); jp1.add(productButton); this.add(jp1, BorderLayout.NORTH); JPanel jp2 = new JPanel(); outputArea.setEditable(false); jp2.add(new JScrollPane(outputArea)); outputArea.setFont(new Font("monospaced", Font.PLAIN, 12)); this.add(jp2, BorderLayout.CENTER); } pack(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); customerButton.addActionListener(this); productButton.addActionListener(this); @Override public void actionPerformed(ActionEvent e) { } if (e.getSource() == productButton) { productDetails(); } else { customerDetails(); } /** * method to get customer details from server */ private void customerDetails() { try { Socket socket = new Socket("localhost", 67); ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); DataRequest request = new DataRequest(); request.setRequestCustomerDetails(true); out.writeObject(request); Customer c = (Customer) in.readObject(); socket.close(); outputArea.append("\n" + c.toString()); logs"); } } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Something went wrong, see } /** * method to get product details from server */ private void productDetails() { try { Socket socket = new Socket("localhost", 67); ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); DataRequest request = new DataRequest(); request.setRequestProductDetails(true); out.writeObject(request); Product p = (Product) in.readObject(); socket.close(); outputArea.append("\n" + p.toString()); logs"); } } } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, "Something went wrong, see } import java.io.Serializable; public class Customer implements Serializable{ private String name; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Customer{" + "Name=" + name + ",\nAddress=" + address + '}'; } } import java.io.Serializable; /** * * @Olia */ public class DataRequest implements Serializable { private boolean requestCustomerDetails; private boolean requestProductDetails; public boolean isRequestCustomerDetails() { return requestCustomerDetails; } public void setRequestCustomerDetails(boolean requestCustomerDetails) { this.requestCustomerDetails = requestCustomerDetails; } public boolean isRequestProductDetails() { return requestProductDetails; } public void setRequestProductDetails(boolean requestProductDetails) { this.requestProductDetails = requestProductDetails; } } import java.io.Serializable; public class Product implements Serializable{ private String name; private String price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } @Override public String toString() { return "Product{" + "Name=" + name + ",Price=" + price + '}'; } } import import import import import import import java.io.File; java.io.IOException; java.io.ObjectInputStream; java.io.ObjectOutputStream; java.net.ServerSocket; java.net.Socket; java.util.Scanner; public class Server { /** * @param args the command line arguments */ public static void main(String[] args) throws IOException, ClassNotFoundException { Customer c = new Customer(); Product p = new Product(); Scanner customerFile = new Scanner(new File("customer.txt")); Scanner productFile = new Scanner(new File("product.txt")); if(customerFile.hasNextLine()) c.setName(customerFile.nextLine()); if(customerFile.hasNextLine()) c.setAddress(customerFile.nextLine()); if(productFile.hasNextLine()) p.setName(productFile.nextLine()); if(productFile.hasNextLine()) p.setPrice(productFile.nextLine()); // customerFile.close(); // productFile.close(); System.out.println("Starting the server.."); ServerSocket server = new ServerSocket(67); while (true) { System.out.println("Listenting for client"); Socket socket = server.accept(); System.out.println("Got a client.."); ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream()); System.out.println("Reading a reqeust from client.."); DataRequest request = (DataRequest) in.readObject(); if (request.isRequestCustomerDetails()) { out.writeObject(c); } else if (request.isRequestProductDetails()) { out.writeObject(p); } System.out.println("Information sent to client."); } } }
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

here is your assignment bud...


Anonymous
Just what I needed…Fantastic!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags