Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 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.         int portNumber = 43954;
  8.  
  9.         try (
  10.             ServerSocket serverSocket = new ServerSocket(portNumber);
  11.             Socket clientSocket = serverSocket.accept();
  12.             PrintWriter out =
  13.                 new PrintWriter(clientSocket.getOutputStream(), true);
  14.             BufferedReader in = new BufferedReader(
  15.                 new InputStreamReader(clientSocket.getInputStream()));
  16.         ) {
  17.          
  18.             String inputLine, outputLine;
  19.              
  20.             // Initiate conversation with client
  21.             KnockKnockProtocol kkp = new KnockKnockProtocol();
  22.             outputLine = kkp.processInput(null);
  23.             out.println(outputLine);
  24.  
  25.             while ((inputLine = in.readLine()) != null) {
  26.                 outputLine = kkp.processInput(inputLine);
  27.                 out.println(outputLine);
  28.                 if (outputLine.equals("Bye."))
  29.                     break;
  30.             }
  31.         } catch (IOException e) {
  32.             System.out.println("Exception caught when trying to listen on port "
  33.                 + portNumber + " or listening for a connection");
  34.             System.out.println(e.getMessage());
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement