Question: Can someone help me with these Java questions over networking package edu.lmu.cs.networking; import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; /** * A
Can someone help me with these Java questions over networking
package edu.lmu.cs.networking; import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; /** * A TCP server that runs on port 9090. When a client connects, it * sends the client the current date and time, then closes the * connection with that client. Arguably just about the simplest * server you can write. */ public class DateServer { /** * Runs the server. */ public static void main(String[] args) throws IOException {
// 1. What is a ServerSocket?
// 2. What is 9090? ServerSocket listener = new ServerSocket(9090); try {
// 3. What does the following loop conditional mean about this program? while (true) { // 4. What is a Socket
// 5. What does accept() do? When is it executed?
Socket socket = listener.accept(); try {
// 6. Where is the toString of the Date object printed (sent)? PrintWriter out = new PrintWriter(socket.getOutputStream(), true); out.println(new Date().toString()); } finally { socket.close(); } } } finally { listener.close(); } } }
The client
DateClient.java
package edu.lmu.cs.networking; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; import javax.swing.JOptionPane; /** * Trivial client for the date server. */ public class DateClient { /** * Runs the client as an application. First it displays a dialog * box asking for the IP address or hostname of a host running * the date server, then connects to it and displays the date that * it serves. */ public static void main(String[] args) throws IOException {
// 7. What is the purpose of the JOtionPane? String serverAddress = JOptionPane.showInputDialog( "Enter IP Address of a machine that is " + "running the date service on port 9090:");
// 8. What is 9090? Is this an arbitrary number, meaning could I change it to 8085 or some other number? Socket s = new Socket(serverAddress, 9090);
// 9. Where is answer coming from? BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); String answer = input.readLine(); JOptionPane.showMessageDialog(null, answer);
// 10. How many time will this program loop? System.exit(0); } }
You can also test the server with telnet.
A Capitalization Server and Client
The server
CapitalizeServer.java
package edu.lmu.cs.networking; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; /** * A server program which accepts requests from clients to * capitalize strings. When clients connect, a new thread is * started to handle an interactive dialog in which the client * sends in a string and the server thread sends back the * capitalized version of the string. * * The program is runs in an infinite loop, so shutdown in platform * dependent. If you ran it from a console window with the "java" * interpreter, Ctrl+C generally will shut it down. */ public class CapitalizeServer { /** * Application method to run the server runs in an infinite loop * listening on port 9898. When a connection is requested, it * spawns a new thread to do the servicing and immediately returns * to listening. The server keeps a unique client number for each * client that connects just to show interesting logging * messages. It is certainly not necessary to do this. */ public static void main(String[] args) throws Exception { System.out.println("The capitalization server is running."); int clientNumber = 0; ServerSocket listener = new ServerSocket(9898); try { while (true) {
// 11. This question will take some thought and testing the programs
// When does the following line run? new Capitalizer(listener.accept(), clientNumber++).start(); } } finally { listener.close(); } } /** * A private thread to handle capitalization requests on a particular * socket. The client terminates the dialogue by sending a single line * containing only a period. */
// 12. What do you expect to see in this code when you read the following class definition? private static class Capitalizer extends Thread { private Socket socket; private int clientNumber; public Capitalizer(Socket socket, int clientNumber) { this.socket = socket; this.clientNumber = clientNumber; log("New connection with client# " + clientNumber + " at " + socket); } /** * Services this thread's client by first sending the * client a welcome message then repeatedly reading strings * and sending back the capitalized version of the string. */ public void run() { try { // Decorate the streams so we can send characters // and not just bytes. Ensure output is flushed // after every newline. BufferedReader in = new BufferedReader( new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); // Send a welcome message to the client. out.println("Hello, you are client #" + clientNumber + "."); out.println("Enter a line with only a period to quit "); // Get messages from the client, line by line; return them // capitalized
// 13. When the following infinite loop breaks, this instance of Capitalizer ends (stops running)
// What comes from the client indicating the end.
while (true) { String input = in.readLine(); if (input == null || input.equals(".")) { break; } out.println(input.toUpperCase()); } } catch (IOException e) { log("Error handling client# " + clientNumber + ": " + e); } finally { try { socket.close(); } catch (IOException e) { log("Couldn't close a socket, what's going on?"); } log("Connection with client# " + clientNumber + " closed"); } } /** * Logs a simple message. In this case we just write the * message to the server applications standard output. */ private void log(String message) { System.out.println(message); } } }
The client
CapitalizeClient.java
package edu.lmu.cs.networking; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /** * A simple Swing-based client for the capitalization server. * It has a main frame window with a text field for entering * strings and a textarea to see the results of capitalizing * them. */ public class CapitalizeClient { private BufferedReader in; private PrintWriter out; private JFrame frame = new JFrame("Capitalize Client"); private JTextField dataField = new JTextField(40); private JTextArea messageArea = new JTextArea(8, 60); /** * Constructs the client by laying out the GUI and registering a * listener with the textfield so that pressing Enter in the * listener sends the textfield contents to the server. */ public CapitalizeClient() { // Layout GUI messageArea.setEditable(false); frame.getContentPane().add(dataField, "North"); frame.getContentPane().add(new JScrollPane(messageArea), "Center"); // Add Listeners dataField.addActionListener(new ActionListener() { /** * Responds to pressing the enter key in the textfield * by sending the contents of the text field to the * server and displaying the response from the server * in the text area. If the response is "." we exit * the whole application, which closes all sockets, * streams and windows. */ public void actionPerformed(ActionEvent e) {
// 14. What is out? Meaning where is the text inside dataField going out.println(dataField.getText()); String response; try {
// 15. What is in? Meaning where is the string going into response coming from? response = in.readLine(); if (response == null || response.equals("")) { System.exit(0); } } catch (IOException ex) { response = "Error: " + ex; } messageArea.append(response + " "); dataField.selectAll(); } }); } /** * Implements the connection logic by prompting the end user for * the server's IP address, connecting, setting up streams, and * consuming the welcome messages from the server. The Capitalizer * protocol says that the server sends three lines of text to the * client immediately after establishing a connection. */ public void connectToServer() throws IOException { // Get the server address from a dialog box. String serverAddress = JOptionPane.showInputDialog( frame, "Enter IP Address of the Server:", "Welcome to the Capitalization Program", JOptionPane.QUESTION_MESSAGE); // Make connection and initialize streams Socket socket = new Socket(serverAddress, 9898); in = new BufferedReader( new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // Consume the initial welcoming messages from the server // 16. What is the initial welcoming message?
for (int i = 0; i < 3; i++) { messageArea.append(in.readLine() + " "); } } /** * Runs the client application. */ public static void main(String[] args) throws Exception {
CapitalizeClient client = new CapitalizeClient(); client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); client.frame.pack(); client.frame.setVisible(true); client.connectToServer(); } }
// 17. If one computer ran java cp . networkexample.CapitalizeServer. And, three computers each ran java
// cp . networkexample.CapitalizeClient. How many threads would be running and from what program?
// 18. How do you stop CapitalizeClient running? How do you stop CapitalizeServer running?
A Two-Player Networked Tic-Tac-Toe Game
The server
TicTacToeServer.java
package edu.lmu.cs.networking; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; /** * A server for a network multi-player tic tac toe game. Modified and * extended from the class presented in Deitel and Deitel "Java How to * Program" book. I made a bunch of enhancements and rewrote large sections * of the code. The main change is instead of passing *data* between the * client and server, I made a TTTP (tic tac toe protocol) which is totally * plain text, so you can test the game with Telnet (always a good idea.) * The strings that are sent in TTTP are: * * Client -> Server Server -> Client * ---------------- ---------------- * MOVE
// 19. When this program starts what will cause it to go from this line (X) to the following line (O)? Game.Player playerX = game.new Player(listener.accept(), 'X');\
// 20. What will cause it to go from this line (O) to the rest of the code? Game.Player playerO = game.new Player(listener.accept(), 'O'); playerX.setOpponent(playerO); playerO.setOpponent(playerX); game.currentPlayer = playerX;
// 21. What do the .starts do? playerX.start(); playerO.start(); } } finally { listener.close(); } } } /** * A two-player game. */ class Game { /** * A board has nine squares. Each square is either unowned or * it is owned by a player. So we use a simple array of player * references. If null, the corresponding square is unowned, * otherwise the array cell stores a reference to the player that * owns it. */ private Player[] board = { null, null, null, null, null, null, null, null, null}; /** * The current player. */ Player currentPlayer; /** * Returns whether the current state of the board is such that one * of the players is a winner. */ public boolean hasWinner() { return (board[0] != null && board[0] == board[1] && board[0] == board[2]) ||(board[3] != null && board[3] == board[4] && board[3] == board[5]) ||(board[6] != null && board[6] == board[7] && board[6] == board[8]) ||(board[0] != null && board[0] == board[3] && board[0] == board[6]) ||(board[1] != null && board[1] == board[4] && board[1] == board[7]) ||(board[2] != null && board[2] == board[5] && board[2] == board[8]) ||(board[0] != null && board[0] == board[4] && board[0] == board[8]) ||(board[2] != null && board[2] == board[4] && board[2] == board[6]); } /** * Returns whether there are no more empty squares. */ public boolean boardFilledUp() { for (int i = 0; i < board.length; i++) { if (board[i] == null) { return false; } } return true; } /** * Called by the player threads when a player tries to make a * move. This method checks to see if the move is legal: that * is, the player requesting the move must be the current player * and the square in which she is trying to move must not already * be occupied. If the move is legal the game state is updated * (the square is set and the next player becomes current) and * the other player is notified of the move so it can update its * client. */ // 22. What does it mean that this method is synchronized? Why is this important?
public synchronized boolean legalMove(int location, Player player) { if (player == currentPlayer && board[location] == null) { board[location] = currentPlayer; currentPlayer = currentPlayer.opponent; currentPlayer.otherPlayerMoved(location); return true; } return false; } /** * The class for the helper threads in this multithreaded server * application. A Player is identified by a character mark * which is either 'X' or 'O'. For communication with the * client the player has a socket with its input and output * streams. Since only text is being communicated we use a * reader and a writer. */ class Player extends Thread { char mark; Player opponent; Socket socket; BufferedReader input; PrintWriter output; /** * Constructs a handler thread for a given socket and mark * initializes the stream fields, displays the first two * welcoming messages. */ public Player(Socket socket, char mark) { this.socket = socket; this.mark = mark; try { input = new BufferedReader( new InputStreamReader(socket.getInputStream())); output = new PrintWriter(socket.getOutputStream(), true); output.println("WELCOME " + mark); output.println("MESSAGE Waiting for opponent to connect"); } catch (IOException e) { System.out.println("Player died: " + e); } } /** * Accepts notification of who the opponent is. */ public void setOpponent(Player opponent) { this.opponent = opponent; } /** * Handles the otherPlayerMoved message. */ public void otherPlayerMoved(int location) { output.println("OPPONENT_MOVED " + location); output.println( hasWinner() ? "DEFEAT" : boardFilledUp() ? "TIE" : ""); } /** * The run method of this thread. */ public void run() { try { // The thread is only started after everyone connects. output.println("MESSAGE All players connected"); // Tell the first player that it is her turn. if (mark == 'X') { output.println("MESSAGE Your move"); } // Repeatedly get commands from the client and process them. while (true) { String command = input.readLine(); if (command.startsWith("MOVE")) { int location = Integer.parseInt(command.substring(5)); if (legalMove(location, this)) { output.println("VALID_MOVE"); output.println(hasWinner() ? "VICTORY" : boardFilledUp() ? "TIE" : ""); } else { output.println("MESSAGE ?"); } } else if (command.startsWith("QUIT")) { return; } } } catch (IOException e) { System.out.println("Player died: " + e); } finally { try {socket.close();} catch (IOException e) {} } } } }
The client
TicTacToeClient.java
package edu.lmu.cs.networking; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; /** * A client for the TicTacToe game, modified and extended from the * class presented in Deitel and Deitel "Java How to Program" book. * I made a bunch of enhancements and rewrote large sections of the * code. In particular I created the TTTP (Tic Tac Toe Protocol) * which is entirely text based. Here are the strings that are sent: * * Client -> Server Server -> Client * ---------------- ---------------- * MOVE
// 23. What is the purpose of the following line? String serverAddress = (args.length == 0) ? "localhost" : args[1]; TicTacToeClient client = new TicTacToeClient(serverAddress); client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); client.frame.setSize(240, 160); client.frame.setVisible(true); client.frame.setResizable(false); client.play(); if (!client.wantsToPlayAgain()) { break; } } } }
// 24. If one computer ran java cp . networkexample.TicTacToeServer. And, three computers each ran java
// cp . networkexample.TicTacToeClient (order of running person1, person2 then person3). How many threads // would be running and from what program?
// 25. What would happen when person1 and person2 finished playing and both selected the option to play
// again?
A Multi-User Chat Application
The server
ChatServer.java
package edu.lmu.cs.networking; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.HashSet; /** * A multithreaded chat room server. When a client connects the * server requests a screen name by sending the client the * text "SUBMITNAME", and keeps requesting a name until * a unique one is received. After a client submits a unique * name, the server acknowledges with "NAMEACCEPTED". Then * all messages from that client will be broadcast to all other * clients that have submitted a unique screen name. The * broadcast messages are prefixed with "MESSAGE ". * * Because this is just a teaching example to illustrate a simple * chat server, there are a few features that have been left out. * Two are very useful and belong in production code: * * 1. The protocol should be enhanced so that the client can * send clean disconnect messages to the server. * * 2. The server should do some logging. */ public class ChatServer { /** * The port that the server listens on. */ private static final int PORT = 9001; /** * The set of all names of clients in the chat room. Maintained * so that we can check that new clients are not registering name * already in use. */ private static HashSet
The client
ChatClient.java
package edu.lmu.cs.networking; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; /** * A simple Swing-based client for the chat server. Graphically * it is a frame with a text field for entering messages and a * textarea to see the whole dialog. * * The client follows the Chat Protocol which is as follows. * When the server sends "SUBMITNAME" the client replies with the * desired screen name. The server will keep sending "SUBMITNAME" * requests as long as the client submits screen names that are * already in use. When the server sends a line beginning * with "NAMEACCEPTED" the client is now allowed to start * sending the server arbitrary strings to be broadcast to all * chatters connected to the server. When the server sends a * line beginning with "MESSAGE " then all characters following * this string should be displayed in its message area. */ public class ChatClient { BufferedReader in; PrintWriter out; JFrame frame = new JFrame("Chatter"); JTextField textField = new JTextField(40); JTextArea messageArea = new JTextArea(8, 40); /** * Constructs the client by laying out the GUI and registering a * listener with the textfield so that pressing Return in the * listener sends the textfield contents to the server. Note * however that the textfield is initially NOT editable, and * only becomes editable AFTER the client receives the NAMEACCEPTED * message from the server. */ public ChatClient() { // Layout GUI textField.setEditable(false); messageArea.setEditable(false); frame.getContentPane().add(textField, "North"); frame.getContentPane().add(new JScrollPane(messageArea), "Center"); frame.pack(); // Add Listeners textField.addActionListener(new ActionListener() { /** * Responds to pressing the enter key in the textfield by sending * the contents of the text field to the server. Then clear * the text area in preparation for the next message. */ public void actionPerformed(ActionEvent e) { out.println(textField.getText()); textField.setText(""); } }); } /** * Prompt for and return the address of the server. */ private String getServerAddress() { return JOptionPane.showInputDialog( frame, "Enter IP Address of the Server:", "Welcome to the Chatter", JOptionPane.QUESTION_MESSAGE); } /** * Prompt for and return the desired screen name. */ private String getName() { return JOptionPane.showInputDialog( frame, "Choose a screen name:", "Screen name selection", JOptionPane.PLAIN_MESSAGE); } /** * Connects to the server then enters the processing loop. */ private void run() throws IOException { // Make connection and initialize streams String serverAddress = getServerAddress(); Socket socket = new Socket(serverAddress, 9001); in = new BufferedReader(new InputStreamReader( socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); // Process all messages from server, according to the protocol. while (true) { String line = in.readLine(); if (line.startsWith("SUBMITNAME")) { out.println(getName()); } else if (line.startsWith("NAMEACCEPTED")) { textField.setEditable(true); } else if (line.startsWith("MESSAGE")) { messageArea.append(line.substring(8) + " "); } } } /** * Runs the client as an application with a closeable frame. */ public static void main(String[] args) throws Exception { ChatClient client = new ChatClient(); client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); client.frame.setVisible(true); client.run(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
