forked from jvm-coder/Java_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDayTimeServer.java
More file actions
46 lines (35 loc) · 1.37 KB
/
DayTimeServer.java
File metadata and controls
46 lines (35 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.net.*;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DayTimeServer {
final static int port = 13;
public static void main(String args[]) {
while (true) {
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
Socket s = ss.accept();//establishes connection
System.out.println("Client accepted");
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// creating Date object
Date date = new Date();
try {
DateFormat fordate = new SimpleDateFormat("yyyy/MM/dd");
DateFormat fortime = new SimpleDateFormat("hh:mm:ss");
String toreturnDate = fordate.format(date);
String toreturnTime = fortime.format(date);
dos.writeUTF(toreturnDate + "\n" + toreturnTime);
} catch (IOException i) {
System.out.println(i);
}
System.out.println("Closing connection");
ss.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
}