File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .net .*;
2+ import java .io .*;
3+ import java .text .DateFormat ;
4+ import java .text .SimpleDateFormat ;
5+ import java .util .Date ;
6+
7+ public class DayTimeServer {
8+ final static int port = 13 ;
9+
10+ public static void main (String args []) {
11+ while (true ) {
12+ try {
13+ ServerSocket ss = new ServerSocket (port );
14+ System .out .println ("Server started" );
15+
16+ System .out .println ("Waiting for a client ..." );
17+
18+ Socket s = ss .accept ();//establishes connection
19+ System .out .println ("Client accepted" );
20+
21+ DataOutputStream dos = new DataOutputStream (s .getOutputStream ());
22+
23+ // creating Date object
24+ Date date = new Date ();
25+
26+
27+ try {
28+ DateFormat fordate = new SimpleDateFormat ("yyyy/MM/dd" );
29+ DateFormat fortime = new SimpleDateFormat ("hh:mm:ss" );
30+ String toreturnDate = fordate .format (date );
31+ String toreturnTime = fortime .format (date );
32+
33+ dos .writeUTF (toreturnDate + "\n " + toreturnTime );
34+
35+ } catch (IOException i ) {
36+ System .out .println (i );
37+ }
38+ System .out .println ("Closing connection" );
39+ ss .close ();
40+
41+ } catch (Exception e ) {
42+ System .out .println (e );
43+ }
44+ }
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments