|
| 1 | +package exercise_31_03Server; |
| 2 | + |
| 3 | +import exercise_17_06.Loan; |
| 4 | +import java.io.DataInputStream; |
| 5 | +import java.io.DataOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.InetAddress; |
| 8 | +import java.net.ServerSocket; |
| 9 | +import java.net.Socket; |
| 10 | +import java.util.Date; |
| 11 | +import javafx.application.Application; |
| 12 | +import javafx.application.Platform; |
| 13 | +import javafx.scene.Scene; |
| 14 | +import javafx.scene.control.ScrollPane; |
| 15 | +import javafx.scene.control.TextArea; |
| 16 | +import javafx.stage.Stage; |
| 17 | + |
| 18 | +public class Exercise_31_03Server extends Application { |
| 19 | + private TextArea taStatus = new TextArea(); |
| 20 | + private int clientNo = 0; |
| 21 | + |
| 22 | + @Override |
| 23 | + public void start(Stage primaryStage) { |
| 24 | + |
| 25 | + Scene scene = new Scene(new ScrollPane(taStatus), 400, 150); |
| 26 | + primaryStage.setTitle("Exercise_31_03Server"); |
| 27 | + primaryStage.setScene(scene); |
| 28 | + primaryStage.show(); |
| 29 | + |
| 30 | + new Thread(() -> { |
| 31 | + try { |
| 32 | + ServerSocket serverSocket = new ServerSocket(8000); |
| 33 | + Platform.runLater(() -> taStatus.appendText("Exercise_31_01Server started at " + new Date() + "\n")); |
| 34 | + |
| 35 | + |
| 36 | + while(true) { |
| 37 | + Socket socket = serverSocket.accept(); |
| 38 | + clientNo++; |
| 39 | + |
| 40 | + Platform.runLater(() -> { |
| 41 | + taStatus.appendText("Starting thread for client " + clientNo + " at " + new Date() + "\n"); |
| 42 | + InetAddress inetAddress = socket.getInetAddress(); |
| 43 | + taStatus.appendText("Client " + clientNo + "'s host name is " + inetAddress.getHostName() + "\n"); |
| 44 | + taStatus.appendText("Client " + clientNo + "'s IP Address is " + inetAddress.getHostAddress() + "\n"); |
| 45 | + }); |
| 46 | + |
| 47 | + new Thread(new HandleAClient(socket)).start(); |
| 48 | + } |
| 49 | + } |
| 50 | + catch (IOException ex) { |
| 51 | + ex.printStackTrace(); |
| 52 | + } |
| 53 | + }).start(); |
| 54 | + } |
| 55 | + |
| 56 | + private class HandleAClient implements Runnable { |
| 57 | + private Socket socket; |
| 58 | + |
| 59 | + public HandleAClient(Socket socket) { |
| 60 | + this.socket = socket; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void run() { |
| 65 | + try{ |
| 66 | + DataInputStream inputFromClient = new DataInputStream(socket.getInputStream()); |
| 67 | + DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream()); |
| 68 | + |
| 69 | + while(true) { |
| 70 | + double interestRate = inputFromClient.readDouble(); |
| 71 | + int numberOfYears = inputFromClient.readInt(); |
| 72 | + double amount = inputFromClient.readDouble(); |
| 73 | + Platform.runLater(() -> taStatus.appendText("Annual interest rate: " + interestRate + "\n" + |
| 74 | + "Number of Years: " + numberOfYears + "\n" + "Loan amount: " + amount + "\n")); |
| 75 | + |
| 76 | + Loan loan = new Loan(interestRate, numberOfYears, amount); |
| 77 | + |
| 78 | + double monthlyPayment = loan.getMonthlyPayment(); |
| 79 | + double totalPayment = loan.getTotalPayment(); |
| 80 | + |
| 81 | + outputToClient.writeDouble(monthlyPayment); |
| 82 | + outputToClient.writeDouble(totalPayment); |
| 83 | + Platform.runLater(() -> taStatus.appendText("monthlyPayment: " + monthlyPayment + "\n" + |
| 84 | + "totalPayment: " + totalPayment)); |
| 85 | + } |
| 86 | + } |
| 87 | + catch(IOException ex) { |
| 88 | + ex.printStackTrace(); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static void main(String[] args) { |
| 94 | + launch(args); |
| 95 | + } |
| 96 | +} |
0 commit comments