class ssserver { public static void main (string [] args) throws ioexception { system.out.println ("server starting.../n");
// create a server socket that listens for incoming connection // requests on port 10000.
serversocket server = new serversocket (10000);
while (true) { // listen for incoming connection requests from client // programs, establish a connection, and return a socket // object that represents this connection.
socket s = server.accept ();
system.out.println ("accepting connection.../n");
// start a thread to handle the connection.
new serverthread (s).start (); } } }
class serverthread extends thread { private socket s;
serverthread (socket s) { this.s = s; }
public void run () { bufferedreader br = null; printwriter pw = null;
try { // create an input stream reader that chains to the socket's // byte-oriented input stream. the input stream reader // converts bytes read from the socket to characters. the // conversion is based on the platform's default character // set.
inputstreamreader isr; isr = new inputstreamreader (s.getinputstream ());
// create a buffered reader that chains to the input stream // reader. the buffered reader supplies a convenient method // for reading entire lines of text.
br = new bufferedreader (isr);
// create a print writer that chains to the socket's byte- // oriented output stream. the print writer creates an // intermediate output stream writer that converts // characters sent to the socket to bytes. the conversion // is based on the platform's default character set.
pw = new printwriter (s.getoutputstream (), true);
// create a calendar that makes it possible to obtain date // and time information.
calendar c = calendar.getinstance ();
// because the client program may send multiple commands, a // loop is required. keep looping until the client either // explicitly requests termination by sending a command // beginning with letters bye or implicitly requests // termination by closing its output stream.
do { // obtain the client program's next command.
string cmd = br.readline ();
// exit if client program has closed its output stream.
if (cmd == null) break;
// convert command to uppercase, for ease of comparison.
cmd = cmd.touppercase ();
// if client program sends bye command, terminate.
if (cmd.startswith ("bye")) break;
// if client program sends date or time command, return // current date/time to the client program.
if (cmd.startswith ("date") || cmd.startswith ("time")) pw.println (c.gettime ().tostring ());
// if client program sends dom (day of month) command, // return current day of month to the client program.
if (cmd.startswith ("dom")) pw.println ("" + c.get (calendar.day_of_month));
// if client program sends dow (day of week) command, // return current weekday (as a string) to the client // program.
if (cmd.startswith ("dow")) switch (c.get (calendar.day_of_week)) { case calendar.sunday : pw.println ("sunday"); break;
case calendar.monday : pw.println ("monday"); break;
case calendar.tuesday : pw.println ("tuesday"); break;
case calendar.wednesday: pw.println ("wednesday"); break;
case calendar.thursday : pw.println ("thursday"); break;
case calendar.friday : pw.println ("friday"); break;
case calendar.saturday : pw.println ("saturday"); }
// if client program sends doy (day of year) command, // return current day of year to the client program.
if (cmd.startswith ("doy")) pw.println ("" + c.get (calendar.day_of_year));
// if client program sends pause command, sleep for three // seconds.