Advertisement
Guest User

Quadratic Threaded Server

a guest
Apr 29th, 2017
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. // MultiThreaded Quadratic Equation Solver Server
  2. // A.C. Silvestri
  3. // 4/29/17
  4. // CSC-112 Intermediate Java Programming
  5. // silvestri@stcc.edu
  6. // HW 10
  7.  
  8. package server;
  9.  
  10. import java.io.*;
  11. import java.util.*;
  12. import java.net.*;
  13. import java.text.SimpleDateFormat;
  14.  
  15. public class QuadraticThreadedServer {
  16.     private static final int PORTNUMBER = 5009;
  17.  
  18.     private static void pStr(String p) {
  19.         System.out.println(p);
  20.     }
  21.  
  22.     private void runServer() {
  23.         ServerSocket server;
  24.         Socket connection;
  25.  
  26.         try {
  27.             pStr("Creating Server Socket " + PORTNUMBER + " . . . ");
  28.             server = new ServerSocket(PORTNUMBER);
  29.             pStr("SUCCESS!!!");
  30.  
  31.             while (true) {
  32.                 pStr("Waiting for connection.");
  33.                 connection = server.accept();
  34.                 pStr("Done");
  35.                 CalculateQuadraticThread t = new CalculateQuadraticThread(connection);
  36.                 t.start();
  37.             }
  38.         } catch (IOException e) {
  39.             e.printStackTrace();
  40.         }
  41.     }
  42.  
  43.     private class CalculateQuadraticThread extends Thread {
  44.         private Socket connection;
  45.  
  46.         public CalculateQuadraticThread(Socket x) {
  47.             connection = x;
  48.         }
  49.  
  50.         public void run() {
  51.             pStr("");
  52.             pStr("Communicating with " + connection.getInetAddress() + " " + connection.getPort());
  53.             SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  54.             Date date = new Date();
  55.             pStr("Time: " + dateFormat.format(date));
  56.            
  57.             double a, b, c;
  58.             double r1 = 0, r2 = 0;
  59.             int status;
  60.             String errmsg = "";
  61.            
  62.             try (
  63.                     Scanner input = new Scanner(connection.getInputStream());
  64.                     PrintWriter output = new PrintWriter(connection.getOutputStream());
  65.                 ) {
  66.                 a = Double.parseDouble(input.nextLine());
  67.                 b = Double.parseDouble(input.nextLine());
  68.                 c = Double.parseDouble(input.nextLine());
  69.  
  70.                 if (a == 0) {
  71.                     status = -1;
  72.                     errmsg = "Not a Quadratic Equation";
  73.                 } else {
  74.                     double discrim = b * b - 4 * a * c;
  75.                     if (discrim < 0) {
  76.                         status = 0;
  77.                     } else if (discrim == 0) {
  78.                         status = 1;
  79.                         r1 = -b / (2 * a);
  80.                     } else {
  81.                         status = 2;
  82.                         r1 = (-b + Math.sqrt(discrim)) / (2 * a);
  83.                         r2 = (-b - Math.sqrt(discrim)) / (2 * a);
  84.                     }
  85.                 }
  86.                 output.println(status);
  87.                 output.flush();
  88.                 if (status == -1) {
  89.                     output.println(errmsg);
  90.                     output.flush();
  91.                 } else if (status == 1) {
  92.                     output.println(r1);
  93.                     output.flush();
  94.                 } else if (status == 2) {
  95.                     output.println(r1);
  96.                     output.flush();
  97.                     output.println(r2);
  98.                     output.flush();
  99.                 }
  100.  
  101.             }
  102.             catch (Exception e) {
  103.                 pStr("Error! " + e.getMessage());
  104.             }
  105.             finally {
  106.                 try {
  107.                     connection.close();
  108.                 } catch (Exception e) {
  109.                 }
  110.             }
  111.  
  112.         }
  113.     }
  114.  
  115.     public static void main(String args[]) {
  116.         QuadraticThreadedServer s = new QuadraticThreadedServer();
  117.         s.runServer();
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement