Skip to content

Commit 5df51a4

Browse files
committed
Day time server
1 parent 70f42f6 commit 5df51a4

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

DayTimeServer.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

0 commit comments

Comments
 (0)