Advertisement
Guest User

IDK

a guest
Feb 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3.  
  4. public class KnockKnockServer {
  5.     public static void main(String[] args) throws IOException {
  6.  
  7.         ServerSocket serverSocket = null;
  8.         try {
  9.             serverSocket = new ServerSocket(4444);
  10.         } catch (IOException e) {
  11.             System.err.println("Could not listen on port: 4444.");
  12.             System.exit(1);
  13.         }
  14.  
  15.         Socket clientSocket = null;
  16.         try {
  17.             clientSocket = serverSocket.accept();
  18.         } catch (IOException e) {
  19.             System.err.println("Accept failed.");
  20.             System.exit(1);
  21.         }
  22.  
  23.         PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  24.         BufferedReader in = new BufferedReader(
  25.                 new InputStreamReader(
  26.                 clientSocket.getInputStream()));
  27.         String inputLine, outputLine;
  28.         KnockKnockProtocol kkp = new KnockKnockProtocol();
  29.  
  30.         outputLine = kkp.processInput(null);
  31.         out.println(outputLine);
  32.  
  33.         while ((inputLine = in.readLine()) != null) {
  34.              outputLine = kkp.processInput(inputLine);
  35.              out.println(outputLine);
  36.              if (outputLine.equals("Bye."))
  37.                 break;
  38.         }
  39.         out.close();
  40.         in.close();
  41.         clientSocket.close();
  42.         serverSocket.close();
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement