-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
52 lines (42 loc) · 1.39 KB
/
Server.java
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
47
48
49
50
51
52
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Server {
private ServerSocket ss;
public Server(ServerSocket ss) {
this.ss = ss;
}
public void startServer() {
try {
while(true) {
LocalDateTime currentTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss yyyy-MM-dd");
String formattedTime = currentTime.format(formatter);
Socket socket = ss.accept();
System.out.println("A new client joined at " + formattedTime);
ClientHandler clientHandler = new ClientHandler(socket);
Thread thread = new Thread(clientHandler);
thread.start();
}
} catch (IOException e) {
}
}
public void closeServerSocket() {
try {
if(ss != null) {
ss.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(1234);
Server server = new Server(ss);
System.out.println(InetAddress.getLocalHost());
server.startServer();
}
}