import java.net.* import java.uti1.Scanner; import java.io.; import java.io.*; *
ID: 3670635 • Letter: I
Question
import java.net.* import java.uti1.Scanner; import java.io.; import java.io.*; *This program opens a connection to a computer specified * as the first command-line argument. If no command-line * argument is given, it prompts the user for a computer *to connect to. The connection is made to * the port specified by LISTENING PORT. The program reads one * line of text from the connection and then closes the * connection. It displays the text that it read on * standard output. This program is meant to be used with *the server program, DataServer, which sends the current * date and time on the computer where the server is running. public class DateClient public static final int LISTENING-PORT = 32007 ;Explanation / Answer
Hi Please find below the solution:-
import java.net.*;
import java.io.*;
import java.util.Date;
/**
* This program is a server that takes connection requests on
* the port specified by the constant LISTENING_PORT. When a
* connection is opened, the program sends the current time to
* the connected socket. The program will continue to receive
* and process connections until it is killed (by a CONTROL-C,
* for example). Note that this server processes each connection
* as it is received, rather than creating a separate thread
* to process the connection.
*/
public class DateServer {
public static final int LISTENING_PORT = 32007;
public static void main(String[] args) {
ServerSocket listener; // Listens for incoming connections.
Socket connection; // For communication with the connecting program.
/* Accept and process connections forever, or until some error occurs.
(Note that errors that occur while communicating with a connected
program are caught and handled in the sendDate() routine, so
they will not crash the server.) */
try {
listener = new ServerSocket(LISTENING_PORT);
System.out.println("Listening on port " + LISTENING_PORT);
while (true) {
// Accept next connection request and handle it.
connection = listener.accept();
sendDate(connection);
}
}
catch (Exception e) {
System.out.println("Sorry, the server has shut down.");
System.out.println("Error: " + e);
return;
}
} // end main()
/**
* The parameter, client, is a socket that is already connected to another
* program. Get an output stream for the connection, send the current time,
* and close the connection.
*/
private static void sendDate(Socket client) {
try {
System.out.println("Connection from " +
client.getInetAddress().toString() );
Date now = new Date(); // The current date and time.
PrintWriter outgoing; // Stream for sending data.
outgoing = new PrintWriter( client.getOutputStream() );
outgoing.println( now.toString() );
outgoing.flush(); // Make sure the data is actually sent!
client.close();
}
catch (Exception e){
System.out.println("Error: " + e);
}
} // end sendDate()
} //end class DateServer
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.