Advertisement
rjsantiago0001

Threaded Server for Quadratic Client

Apr 30th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1.  
  2. // Ricardo Santiago
  3. // 4/29/16
  4. // CSCI-112
  5. // Threaded Server for Quadratic Client
  6.  
  7. import java.io.*;
  8. import java.net.*;
  9.  
  10. public class ThreadedServer {
  11.     static final int PORTNUMBER = 5013;
  12.  
  13.     static void pStr(String p) {
  14.         System.out.println(p);
  15.     }
  16.  
  17.     void runServer() {
  18.         ServerSocket server;
  19.         Socket connection;
  20.  
  21.         try {
  22.             pStr("Creating Server Socket " + 5013 + " . . . ");
  23.             server = new ServerSocket(5013);
  24.             pStr("SUCCESS!!!");
  25.  
  26.             while (true) {
  27.                 pStr("Waiting for connection.");
  28.                 connection = server.accept();
  29.                 pStr("Done");
  30.                 ThreadConnect t = new ThreadConnect(connection);
  31.                 t.start();
  32.             }
  33.         } catch (Exception e) {
  34.             e.printStackTrace();
  35.         }
  36.     }
  37.  
  38.     public class ThreadConnect extends Thread {
  39.         private Socket connection;
  40.  
  41.         ThreadConnect(Socket x) {
  42.             connection = x;
  43.         }
  44.  
  45.         public void run() {
  46.             DataOutputStream output = null;
  47.             DataInputStream input = null;
  48.  
  49.             try {
  50.  
  51.                 input = new DataInputStream(connection.getInputStream());
  52.                 output = new DataOutputStream(connection.getOutputStream());
  53.  
  54.                 String message;
  55.  
  56.                 try {
  57.  
  58.                     double a, b, c, root1, root2;
  59.  
  60.                     c = input.readDouble();
  61.                     b = input.readDouble();
  62.                     a = input.readDouble();
  63.  
  64.                     double discriminant = b * b - 4.0 * a * c;
  65.  
  66.                     String statusStr;
  67.                     int statusNum;
  68.  
  69.                     if (discriminant > 0) {
  70.  
  71.                         root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
  72.                         root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
  73.                         statusNum = 2;
  74.  
  75.                         output.writeDouble(root1);
  76.                         output.writeDouble(root2);
  77.                         output.writeInt(statusNum);
  78.                     }
  79.  
  80.                     if (discriminant == 0) {
  81.  
  82.                         statusNum = 1;
  83.                         root1 = -b / (2 * a);
  84.                         root2 = -b / (2 * a);
  85.  
  86.                         output.writeDouble(root1);
  87.                         output.writeDouble(root2);
  88.                         output.writeInt(statusNum);
  89.  
  90.                     }
  91.                     if (discriminant < 0) {
  92.                         statusNum = 0;
  93.                         output.writeInt(statusNum);
  94.                     }
  95.  
  96.                     else {
  97.                         statusNum = -1;
  98.                         statusStr = ("The server encountered an error doing the calculation");
  99.                         output.writeInt(statusNum);
  100.                         output.writeUTF(statusStr);
  101.                     }
  102.  
  103.                 } catch (Exception e) {
  104.                     message = e.getMessage();
  105.                 }
  106.  
  107.                 output.flush();
  108.             } catch (Exception e) {
  109.                 pStr(e.getMessage());
  110.             } finally {
  111.                 try {
  112.                     input.close();
  113.                 } catch (Exception e) {
  114.                 }
  115.                 try {
  116.                     output.close();
  117.                 } catch (Exception e) {
  118.                 }
  119.                 try {
  120.                     connection.close();
  121.                 } catch (Exception e) {
  122.                 }
  123.             }
  124.         }
  125.     }
  126.  
  127.     public static void main(String args[]) {
  128.         ThreadedServer s = new ThreadedServer();
  129.         s.runServer();
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement