Homework assignment to make a simple client/server program in which the user has commands to ask for the line of a text file, ask for a random line, count how many lines, and leave the server.
So here is the server child, the problem I'm having for this is that when the client outputs the command "Bye" I need to server or client to end their connection, while allowing other clients to stay on. I'm not entirely sure what to put in the method exit(), I tried NameOfClientClass.socket.close() but funny exceptions start occurring. If you wanted to leave the server what would you do?
package homework_3; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.NoSuchElementException; import java.util.Random; import java.util.Scanner; import java.util.StringTokenizer; public class OneLinerChild extends Thread { private Socket socket = null; private int clientNum; private static Scanner in = null; private static PrintWriter out = null; public OneLinerChild(Socket socket, int clientNum) { this.socket = socket; this.clientNum = clientNum; } public void run() { try { in = new Scanner(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); String serverInput; while ((serverInput = in.nextLine()) != null) { if (serverInput.equals("Count") || serverInput.equals("count")) { count(); } else if (serverInput.equals("Fetch") || serverInput.equals("fetch")) { fetch(); } else if (serverInput.contains("Fetch") || serverInput.contains("fetch")) { fetch_n(serverInput); } else if (serverInput.equals("Bye") || serverInput.equals("bye")) { exit(); } else { out.println("Invalid command!"); } } } catch (IOException e) { e.printStackTrace(); } catch (NoSuchElementException e) { } try { socket.close(); in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } private void fetch_n(String input) { Scanner oneLiners; try { oneLiners = new Scanner(new FileReader("src/OneLiners.txt")); StringTokenizer st = new StringTokenizer(input); st.nextToken(); int n = Integer.parseInt(st.nextToken()); if (n <= 59 && n >= 1) { for (int i = 1; i <= n; ++i) { String fetchedLine = oneLiners.nextLine(); if (i == n) { out.println(fetchedLine); } } } else { out.println("The line you are looking for does not exist!"); } } catch (FileNotFoundException e) { out.println("OneLiners.txt cannot be found!"); } catch (NumberFormatException e) { out.println("The line you are looking for does not exist!"); } } private void fetch() { Scanner oneLiners; try { oneLiners = new Scanner(new FileReader("src/OneLiners.txt")); int n = new Random().nextInt(59) + 1; for (int i = 1; i <= n; ++i) { String fetchedLine = oneLiners.nextLine(); if (i == n) { out.println(fetchedLine); } } } catch (FileNotFoundException e) { out.println("OneLiners.txt cannot be found!"); } } private void exit() { } private void count() { Scanner oneLiners; try { oneLiners = new Scanner(new FileReader("src/OneLiners.txt")); int lines = 0; while (oneLiners.hasNextLine()) { ++lines; oneLiners.nextLine(); } out.println(lines); } catch (FileNotFoundException e) { out.println("OneLiners.txt cannot be found!"); } } }