Advertisement
Guest User

Untitled

a guest
May 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. package sample.client;
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.PrintWriter;
  8. import java.net.Socket;
  9. import java.util.Scanner;
  10.  
  11. public class PharmaClient {
  12.  
  13.   private static boolean isConnected;
  14.   private Socket clientSocket;
  15.   private PrintWriter out;
  16.   private BufferedReader in;
  17.   private static int number;
  18.   private static int expiditing;
  19.  
  20.   public static void main(String[] args) throws IOException {
  21.     PharmaClient client = new PharmaClient();
  22.     client.startConnection("localhost", 6666);
  23.     Scanner scanner = new Scanner(System.in);
  24.     int selection = 0;
  25.     boolean inQ = false;
  26.  
  27.     while (isConnected) {
  28.  
  29.       System.out.println("####################################");
  30.       if(!inQ){
  31.  
  32.         System.out.println("# 1) Take Number                   #");
  33.       }
  34.       System.out.println("# 2) Show number being expedited   #");
  35.       System.out.println("# 3) I've been served   #");
  36.       System.out.println("# 4) Disconnect from server        #");
  37.  
  38.       System.out.println("####################################");
  39.       selection = scanner.nextInt();
  40.  
  41.  
  42.       switch (selection) {
  43.         case 1:
  44.           number = Integer.parseInt(client.sendMessage("takeNumber"));
  45.           System.out.println(number);
  46.           inQ = true;
  47.           break;
  48.  
  49.         case 2:
  50.           expiditing = Integer.parseInt(client.sendMessage("expediting"));
  51.           System.out.println(expiditing);
  52.           break;
  53.  
  54.         case 3:
  55.           String done = client.sendMessage("done");
  56.           System.out.println(done);
  57.  
  58.         case 4:
  59.           String msg = client.sendMessage("disconnect");
  60.           if(msg.equals("bye"))
  61.             isConnected = false;
  62.  
  63.           System.out.println("Disconnected from server");
  64.           break;
  65.         default:
  66.           System.out.println("Error. Please try again");
  67.       }
  68.     }
  69.   }
  70.  
  71.   public void startConnection(String ip, int port) throws IOException {
  72.     clientSocket = new Socket(ip, port);
  73.     out = new PrintWriter(clientSocket.getOutputStream(), true);
  74.     in = new BufferedReader(new InputStreamReader
  75.         (clientSocket.getInputStream()));
  76.     isConnected = true;
  77.   }
  78.  
  79.   public String sendMessage(String msg) throws IOException {
  80.     out.println(msg);
  81.     String resp = in.readLine();
  82.     return resp;
  83.   }
  84.  
  85.   public void stopConnection() throws IOException {
  86.     in.close();
  87.     out.close();
  88.     clientSocket.close();
  89.     isConnected = false;
  90.   }
  91.  
  92.   public String read() throws IOException {
  93.     String line = null;
  94.     line = in.readLine();
  95.     while (isConnected) {
  96.       if (line.startsWith("disc")) {
  97.         isConnected = false;
  98.       }
  99.     }
  100.     return line;
  101.   }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement