Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

GUI is not required. Synchronisation needs to be implemented Figure 30.21 The cl

ID: 3847542 • Letter: G

Question

GUI is not required.

Synchronisation needs to be implemented

Figure 30.21

The client in (b) sends the annual interest rate, number of years, and loan amount to the server and receives the monthly payment and total payment from the server in (a).

java

Project: Mutli-Thread Server

Problem Description: Loan server) Write a server for multiple clients. The client sends loan information (annual interest rate, number of years, and loan amount) to the server (see Figure 30.21(b)). The server computes monthly payment and total payment and sends them back to the client (see Figure 30.21(a)). Name the client Exercise30_1Client and the server Exercise30_1Server.

Exercise30 iserver xercise30 1 Server started at Wed Jul 18 15:49:01 EDT 2007 Starting thread for client1 at Wed Jul 18 15:49:12 EDT 2007 Client 1's host name is localhost Client 1's IP Address is 127.0.0.1 Annual Interest Rate: 3.5 Number of Years: 3 Loan Amount: 5000.0 monthly Payment: 14 6.5103986345515 totalPayment: 146.5103986345515

Explanation / Answer

//Server.java: The server computes monthly payment and total payment and sends them back to the client

import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame {

//Text area for displaying contents
private JTextArea jta = new JTextArea();

public static void main(String[] args){

new Server();

}

public Server() {

//Place text area on the frame
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);

setTitle("Exercise30_1Server");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); //It is necessary to show the frame here!

try{
   jta.append("Exercise30_1Server started at " + new Date() + ' ');

// Number a client
int clientNo = 1;
   while (true) {

//Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
jta.append("Starting thread for client " + clientNo + " at " + new Date() + ' ');
// Find the client's host name and IP address
   InetAddress inetAddress = socket.getInetAddress();
   jta.append("Client " + clientNo + "'s host name is " + InetAddress.getHostName()+ " ");
   jta.append("Client " + clientNo + "'s IP Address is " + InetAddress.getHostAddress()+ " ");
   // Create a new thread for the connection
   HandleAClient task = new HandleAClient(socket);
  
   // Start the new thread
   new Thread(task).start();
  
   // Increment clientNo
   clientNo++;
}
}
catch(IOException ex){
System.err.println(ex);

}
}
// Inner class
// Define the thread class for handling new connection
class HandleAClient implements Runnable {
   private Socket socket; // A connected socket
   /** Construct a thread */
   public HandleAClient(Socket socket) {
       this.socket = socket;
   }

   @Override /** Run a thread */
   public void run(){
       try {
           // Create data input and output streams
           DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
           DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
           // Continuously serve the client
       while (true) {

           //Receive annual interest rate from the client
           double annualInterestRate = isFromClient.readDouble();

           //Receive number of years from the client
           int numOfYears = isFromClient.readInt();

           //Receive loan amount from the client
           double loanAmount = isFromClient.readDouble();


           //Obtain monthly interest rate
           double monthlyInterestRate = annualInterestRate / 1200;


           //Compute total payment
           double totalPayment=(loanAmount*annualInterestRate/100*numOfYears)+loanAmount;

           //Compute monthly payment
           double monthlyPayment = totalPayment/(numOfYears*12);

           //Send monthly payment back to the client
           osToClient.writeDouble(monthlyPayment);

           //Send total payment back to the client
           osToClient.writeDouble(totalPayment);


           jta.append("The Annual Interest Rate received from client is "+ annualInterestRate+' ');
           jta.append("The Number Of Years received from client is "+ numOfYears+' ');
           jta.append("The Loan Amount received from client is "+ loanAmount+' ');
           jta.append("The Monthly Payment is "+ monthlyPayment+' ') ;
           jta.append("The Total Payment is"+ totalPayment+' ');
           }
       }
       catch(IOException ex){
           System.err.println(ex);
       }
   }
}

//Client.java: The client sends loan information (annual interest rate, number of years, and loan amount) to the server and receives
//result back from the server

import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client extends JFrame implements ActionListener{

//Text field for receiving annual interest rate,number of years, loan amount
private JTextField jtfAnnualInterestRate = new JTextField();
private JTextField jtfNumOfYears = new JTextField();
private JTextField jtfLoanAmount = new JTextField();
private JButton jbtSubmit = new JButton("Submit");

//Text area for displaying contents
private JTextArea jta = new JTextArea();

//IO streams
DataOutputStream osToServer;
DataInputStream isFromServer;

public static void main(String[] args){
new Client();
}

public Client(){
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,1));
p1.add(new JLabel("Annual Interest Rate"));
p1.add(new JLabel("Number Of Years"));
p1.add(new JLabel("Loan Amount"));

Panel p2 = new Panel();
p2.setLayout(new GridLayout(3,1));
p2.add(jtfAnnualInterestRate);
p2.add(jtfNumOfYears);
p2.add(jtfLoanAmount);

JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(p1, BorderLayout.WEST);

p.add(p2,BorderLayout.CENTER);
p.add(jbtSubmit,BorderLayout.EAST);

jtfAnnualInterestRate.setHorizontalAlignment(JTextField.RIGHT);
jtfNumOfYears.setHorizontalAlignment(JTextField.RIGHT);
jtfLoanAmount.setHorizontalAlignment(JTextField.RIGHT);

getContentPane().setLayout(new BorderLayout());
getContentPane().add(p,BorderLayout.NORTH);
getContentPane().add(new JScrollPane(jta),BorderLayout.CENTER);

jbtSubmit.addActionListener(this); //Register listener

setTitle("Exercise30_1Client");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);

try{

//Create a socket to connect to the server
Socket connectToServer = new Socket("localhost",8000);

//Create an input stream to receive data from the server
isFromServer = new DataInputStream(connectToServer.getInputStream());

//Create an output stream to send data to the server
osToServer = new DataOutputStream(connectToServer.getOutputStream());

}

catch(IOException ex){
jta.append(ex.toString()+' ');
}
}

public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand();
if(e.getSource() instanceof JButton){
try{

//Get the annual interest rate from the text field
double annualInterestRate =
Double.parseDouble(jtfAnnualInterestRate.getText().trim());

//Get the number of years from the text field
int numOfYears =
Integer.parseInt(jtfNumOfYears.getText());

//Get the loan amount from the text field
double loanAmount =
Double.parseDouble(jtfLoanAmount.getText().trim());

//Send the annual interest rate to the server
osToServer.writeDouble(annualInterestRate);

//Send the number of years to the server
osToServer.writeInt(numOfYears);

//Send the loan amount to the server
osToServer.writeDouble(loanAmount);

osToServer.flush();

//Get monthly payment from the server
double monthlyPayment = isFromServer.readDouble();

//Get total payment from the server
double totalPayment = isFromServer.readDouble();

//Display to the text area
jta.append("Annual Interest Rate: " + annualInterestRate +" ");
jta.append("Number Of Years: " + numOfYears +" ");
jta.append("Loan Amount: " + loanAmount +" ");
jta.append("The Monthly Payment is: "+ monthlyPayment +" ");
jta.append("The Total Payment is: "+ totalPayment +" ");

}
catch(IOException ex){
System.err.println(ex);
}
}
}
}