Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample.client;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.Scanner;
- public class PharmaClient {
- private static boolean isConnected;
- private Socket clientSocket;
- private PrintWriter out;
- private BufferedReader in;
- private static int number;
- private static int expiditing;
- public static void main(String[] args) throws IOException {
- PharmaClient client = new PharmaClient();
- client.startConnection("localhost", 6666);
- Scanner scanner = new Scanner(System.in);
- int selection = 0;
- boolean inQ = false;
- while (isConnected) {
- System.out.println("####################################");
- if(!inQ){
- System.out.println("# 1) Take Number #");
- }
- System.out.println("# 2) Show number being expedited #");
- System.out.println("# 3) I've been served #");
- System.out.println("# 4) Disconnect from server #");
- System.out.println("####################################");
- selection = scanner.nextInt();
- switch (selection) {
- case 1:
- number = Integer.parseInt(client.sendMessage("takeNumber"));
- System.out.println(number);
- inQ = true;
- break;
- case 2:
- expiditing = Integer.parseInt(client.sendMessage("expediting"));
- System.out.println(expiditing);
- break;
- case 3:
- String done = client.sendMessage("done");
- System.out.println(done);
- case 4:
- String msg = client.sendMessage("disconnect");
- if(msg.equals("bye"))
- isConnected = false;
- System.out.println("Disconnected from server");
- break;
- default:
- System.out.println("Error. Please try again");
- }
- }
- }
- public void startConnection(String ip, int port) throws IOException {
- clientSocket = new Socket(ip, port);
- out = new PrintWriter(clientSocket.getOutputStream(), true);
- in = new BufferedReader(new InputStreamReader
- (clientSocket.getInputStream()));
- isConnected = true;
- }
- public String sendMessage(String msg) throws IOException {
- out.println(msg);
- String resp = in.readLine();
- return resp;
- }
- public void stopConnection() throws IOException {
- in.close();
- out.close();
- clientSocket.close();
- isConnected = false;
- }
- public String read() throws IOException {
- String line = null;
- line = in.readLine();
- while (isConnected) {
- if (line.startsWith("disc")) {
- isConnected = false;
- }
- }
- return line;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement