Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Server implements Runnable {
- public static final int PORTNUMBER = 8540;
- public static final int MAX_CLIENTS = 3;
- public static boolean shutdownFlag = false;
- public static void main(String[] args) {
- Server s = new Server();
- s.run();
- }
- @Override
- public void run() {
- ExecutorService executor = null;
- try (ServerSocket serverSocket = new ServerSocket(PORTNUMBER);) {
- executor = Executors.newFixedThreadPool(MAX_CLIENTS);
- System.out.println("Waiting for clients");
- while (true) {
- Socket clientSocket = serverSocket.accept();
- //clientSocket.setSoTimeout(10000);
- Runnable worker = new RequestHandler(clientSocket);
- executor.execute(worker);
- System.out.println("Only printed if new Client arrives");
- if (shutdownFlag) {
- System.out.println("Executing shutdown");
- executor.shutdown();
- serverSocket.close();
- }
- }
- } catch (IOException e) {
- System.out
- .println("Exception caught when trying to listen on port "
- + PORTNUMBER + " or listening for a connection");
- System.out.println(e.getMessage());
- } finally {
- if (executor != null) {
- executor.shutdown();
- }
- }
- }
- public class Client {
- public static final String HOSTNAME = "localhost";
- public static final int PORTNUMBER = 8540;
- public static void main(String[] args) throws IOException {
- try (Socket echoSocket = new Socket(HOSTNAME, PORTNUMBER);
- PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
- // InputStream test = echoSocket.getInputStream();
- BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
- BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
- String userInput;
- while ((userInput = stdIn.readLine()) != null) {
- out.println(userInput);
- System.out.println("echo: " + in.readLine());
- if(userInput.equals("BYE")){
- break;
- }
- }
- }catch (UnknownHostException e) {
- System.err.println("Don't know about host " + HOSTNAME);
- System.exit(1);
- }catch (IOException e) {
- System.err.println("Couldn't get I/O for the connection to " + HOSTNAME);
- System.exit(1);
- }
- }
- }
- public class RequestHandler implements Runnable {
- //private final String password = "passwort";
- private final Socket client;
- ServerSocket serverSocket = null;
- public RequestHandler(Socket client) {
- this.client = client;
- }
- @Override
- public void run() {
- try (BufferedReader in = new BufferedReader(new InputStreamReader(
- client.getInputStream()));
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(client.getOutputStream()));) {
- System.out.println("Thread started with name:"
- + Thread.currentThread().getName());
- String userInput;
- String serverResponse;
- while ((userInput = in.readLine()) != null) {
- serverResponse = processInput(userInput);
- System.out.println("Received message from "
- + Thread.currentThread().getName() + " : " + userInput);
- writer.write("Sever Response : " + serverResponse);
- writer.newLine();
- writer.flush();
- }
- } catch (IOException e) {
- System.out.println("I/O exception: " + e);
- } catch (Exception ex) {
- System.out.println("Exception in Thread Run. Exception : " + ex);
- }
- }
- public String processInput(String input) {
- boolean commandFound = false;
- String output = "";
- try {
- if (input.getBytes("UTF-8").length > 255)
- output = "Max string length exceeded";
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Pattern allPattern = Pattern
- .compile("(?<lower>^LOWERCASE\s.+)|(?<upper>^UPPERCASE\s.+)|(?<reverse>^REVERSE\s.+)|(?<bye>^BYE)|(?<shutdown>^SHUTDOWN passwort)");
- Matcher allMatcher = allPattern.matcher(input);
- if (allMatcher.find()) {
- String lower = allMatcher.group("lower");
- String upper = allMatcher.group("upper");
- String reverse = allMatcher.group("reverse");
- String bye = allMatcher.group("bye");
- String shutdown = allMatcher.group("shutdown");
- commandFound = true;
- if (lower != null) {
- output = lower.substring(10).toLowerCase();
- }
- if (upper != null) {
- output = upper.substring(10).toUpperCase();
- }
- if (reverse != null) {
- output = new StringBuilder(reverse.substring(8)).reverse()
- .toString();
- }
- if (bye != null) {
- output = "BYE";
- }
- if (shutdown != null) {
- output = "SHUTDOWN";
- Server.shutdownFlag = true;
- }
- } else {
- commandFound = false;
- output = "UNKNOWN COMMAND";
- }
- if (commandFound) {
- output = "OK ".concat(output);
- } else {
- output = "ERROR ".concat(output);
- }
- return output;
- }
- }
- class Server {
- private boolean shutdownFlag = false; // This can't be static anymore.
- public static final Server SERVER = new Server();
- public static void main(String[] args) {
- SERVER.run();
- }
- private void run() {
- // Here goes everything that used to be inside main...
- // Now you have the Server.SERVER instance to use outside the class
- // to shut things down or whatever ...
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement