Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import java.net.*;
  2. import java.util.Scanner;
  3. import java.io.*;
  4.  
  5. class ClientHandler extends Thread
  6. {
  7. private Socket conn;
  8. private boolean wantToQuit;
  9. private Scanner reader;
  10. private ProcessCommand process;
  11. ClientHandler(Socket conn)
  12. {
  13. this.conn = conn;
  14. reader = new Scanner(System.in);
  15. wantToQuit = false;
  16. process = new ProcessCommand();
  17. }
  18.  
  19. public void run()
  20. {
  21. String line , input = "";
  22.  
  23.  
  24.  
  25. try
  26. {
  27. //get socket writing and reading streams
  28. DataInputStream in = new DataInputStream(conn.getInputStream());
  29. PrintStream out = new PrintStream(conn.getOutputStream());
  30.  
  31. //Send welcome message to client
  32. out.println("Welcome to the Server");
  33. out.println("Author: Hallvard Honningsvåg Halkjelsvik");
  34. out.println("Type help for a list of commands and a description.");
  35. out.println("Type quit to close the connection");
  36.  
  37. //Now start reading input from client
  38. while(wantToQuit == false)
  39. {
  40. String word1 = null;
  41. String word2 = null;
  42. line = reader.nextLine();
  43.  
  44. // Find up to two words on the line.
  45. Scanner tokenizer = new Scanner(line);
  46. if(tokenizer.hasNext()) {
  47. word1 = tokenizer.next(); // get first word
  48. if(tokenizer.hasNext()) {
  49. word2 = tokenizer.next(); // get second word
  50. // note: we just ignore the rest of the input line.
  51. }
  52.  
  53. }
  54.  
  55. if(word1 != null && word2 == null){
  56. process.process(word1, null);
  57. }
  58.  
  59. else if(word1 == "quit"){
  60. wantToQuit = true;
  61. }
  62.  
  63. }
  64.  
  65. //client disconnected, so close socket
  66. conn.close();
  67. }
  68.  
  69. catch (IOException e)
  70. {
  71. System.out.println("IOException on socket : " + e);
  72. e.printStackTrace();
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement