Java Network programming Question:

Download Job Interview Questions and Answers PDF

Explain A Daytime Server?

Java Network programming Interview Question
Java Network programming Interview Question

Answer:

Example: A Daytime Server

import java.net.*;
import java.io.*;
import java.util.Date;

public class DaytimeServer {

public final static int DEFAULT_PORT = 13;

public static void main(String[] args) {

int port = DEFAULT_PORT;
if (args.length > 0) {
try {
port = Integer.parseInt(args[0]);
if (port < 0 || port >= 65536) {
System.out.println("Port must between 0 and
65535");
return;
}
}
catch (NumberFormatException e) {
// use default port
}

}

try {

ServerSocket server = new ServerSocket(port);

Socket connection = null;
while (true) {

try {
connection = server.accept( );
OutputStreamWriter out
= new OutputStreamWriter
(connection.getOutputStream( ));
Date now = new Date( );
out.write(now.toString( ) +"rn");
out.flush( );
connection.close( );
}
catch (IOException e) {}
finally {
try {
if (connection != null) connection.close( );
}
catch (IOException e) {}
}

} // end while

} // end try
catch (IOException e) {
System.err.println(e);
} // end catch

} // end main

} // end DaytimeServer

Example is straightforward. The first three lines import the usual packages, java.io and java.net, as well as java.util.Date so we can get the time. There is a single public final static int field (i.e., a constant) in the class DEFAULT_PORT, which is set to the well-known port for a daytime server (port 13). The class has a single method, main( ), which does all the work. If the port is specified on the command-line, then it's read from args[0]. Otherwise, the default port is used.

The outer try block traps any IOExceptions that may arise while the ServerSocket server is constructed on the daytime port or when it accepts connections. The inner try block watches for exceptions thrown while the connections are accepted and processed. The accept( ) method is called within an infinite loop to watch for new connections; like many servers, this program never terminates but continues listening until an exception is thrown or you stop it manually.[1]

When a client makes a connection, accept( ) returns a Socket, which is stored in the local variable connection, and the program continues. We call getOutputStream( ) to get the output stream associated with that Socket and then chain that output stream to a new OutputStreamWriter, out. To get the current date, we construct a new Date object and send it to the client by writing its string representation on out with write( ).

Finally, after the data is sent or an exception has been thrown, we close connection inside the finally block. Always close a socket when you're finished with it. In the previous chapter, we said that a client shouldn't rely on the other side of a connection to close the socket. That goes triple for servers. Clients can time out or crash; users can cancel transactions; networks can go down in high-traffic periods. For any of these or a dozen more reasons, you cannot rely on clients to close sockets, even when the protocol requires them to (which it doesn't in this case).

Download Java Network programming Interview Questions And Answers PDF

Previous QuestionNext Question
To create a Socket, you need to know the Internet host to which you want to connect?Explain A Time Server?