Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.util.Scanner;
- import java.io.*;
- class ClientHandler extends Thread
- {
- private Socket conn;
- private boolean wantToQuit;
- private Scanner reader;
- private ProcessCommand process;
- ClientHandler(Socket conn)
- {
- this.conn = conn;
- reader = new Scanner(System.in);
- wantToQuit = false;
- process = new ProcessCommand();
- }
- public void run()
- {
- String line , input = "";
- try
- {
- //get socket writing and reading streams
- DataInputStream in = new DataInputStream(conn.getInputStream());
- PrintStream out = new PrintStream(conn.getOutputStream());
- //Send welcome message to client
- out.println("Welcome to the Server");
- out.println("Author: Hallvard Honningsvåg Halkjelsvik");
- out.println("Type help for a list of commands and a description.");
- out.println("Type quit to close the connection");
- //Now start reading input from client
- while(wantToQuit == false)
- {
- String word1 = null;
- String word2 = null;
- line = reader.nextLine();
- // Find up to two words on the line.
- Scanner tokenizer = new Scanner(line);
- if(tokenizer.hasNext()) {
- word1 = tokenizer.next(); // get first word
- if(tokenizer.hasNext()) {
- word2 = tokenizer.next(); // get second word
- // note: we just ignore the rest of the input line.
- }
- }
- if(word1 != null && word2 == null){
- process.process(word1, null);
- }
- else if(word1 == "quit"){
- wantToQuit = true;
- }
- }
- //client disconnected, so close socket
- conn.close();
- }
- catch (IOException e)
- {
- System.out.println("IOException on socket : " + e);
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement