| // Ssserver. Java
Import java. Io .*;
Import java.net .*;
Import java. util .*;
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.
// 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
// 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,
// 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 example, 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.
If (CMD. startswith ("pause "))
Try
{
Thread. Sleep (3000 );
}
Catch (interruptedexception E)
{
}
}
While (true );
{
Catch (ioexception E)
{
System. Out. println (E. tostring ());
}
Finally
{
System. Out. println ("Closing connection.../N ");
Try
{
If (BR! = NULL)
BR. Close ();
If (PW! = NULL)
PW. Close ();
If (s! = NULL)
S. Close ();
}
Catch (ioexception E)
{
}
}
}
} |