Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- public class KnockKnockServer {
- public static void main(String[] args) throws IOException {
- int portNumber = 43954;
- try (
- ServerSocket serverSocket = new ServerSocket(portNumber);
- Socket clientSocket = serverSocket.accept();
- PrintWriter out =
- new PrintWriter(clientSocket.getOutputStream(), true);
- BufferedReader in = new BufferedReader(
- new InputStreamReader(clientSocket.getInputStream()));
- ) {
- String inputLine, outputLine;
- // Initiate conversation with client
- KnockKnockProtocol kkp = new KnockKnockProtocol();
- outputLine = kkp.processInput(null);
- out.println(outputLine);
- while ((inputLine = in.readLine()) != null) {
- outputLine = kkp.processInput(inputLine);
- out.println(outputLine);
- if (outputLine.equals("Bye."))
- break;
- }
- } 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());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement