Network and Client/Server Applications

Chapter 15 introduces some resources used to create network and client/server applications. These resources include IP addresses, sockets, and threads. Here is an example chat between a server program and a client program from the book.

// Server program code

socketOutput.println("Welcome to the chatroom. " +
        "Type a message or 'bye' to quit.");
while (true){
    String clientInput = keyboardReader.nextLine();
    System.out.println(clientInput);
    if (clientInput.equalsIgnoreCase("bye"))
        break;
    System.out.print("> ");
    String userInput = keyboardReader.nextLine();
    socketOutput.println(serverInput);
    if (userInput.equalsIgnoreCase("bye"))
        break;
}
Server Terminal
// Client program code

String greeting = socketReader.readLine();
System.out.println(greeting); 
while (true){
    System.out.print("> ");
    String userInput = keyboardReader.nextLine();
    socketOutput.println(userInput);
    if (userInput.equalsIgnoreCase("bye"))
        break;
    String serverInput = socketReader.readLine();
    System.out.println(serverInput);
    if (serverInput.equalsIgnoreCase("bye"))
        break;
}

 

Client Terminal