-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.java
86 lines (63 loc) · 1.94 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.time.LocalDate;
import java.time.LocalTime;
public class Server{
static final int PORT = 3443;
private ArrayList<ClientHandler> clients = new ArrayList<ClientHandler>();
private ArrayList<LocalTime> times = new ArrayList<LocalTime>();
private ArrayList<LocalDate> dates = new ArrayList<LocalDate>();
public Server(){
Socket client_socket = null;
ServerSocket server = null;
try{
server = new ServerSocket(PORT);
System.out.println("Server created and ready to use");
while (true){
client_socket = server.accept();
LocalTime time = LocalTime.now();
LocalDate date = LocalDate.now();
ClientHandler client = new ClientHandler(client_socket, this);
clients.add(client);
times.add(time);
dates.add(date);
System.out.println("new client");
System.out.println("name-code: " + client);
System.out.println("list of clients:");
System.out.println(" NAME-CODE | TIME | DATE ");
System.out.println("______________________________________________________________");
for (int i = 0; i < clients.size(); i++){
ClientHandler cl = clients.get(i);
LocalTime ti = times.get(i);
LocalDate da = dates.get(i);
System.out.println(cl + " " + ti + " " + da);
}
new Thread(client).start();
}
}
catch (IOException ex){
ex.printStackTrace();
}
finally{
try{
client_socket.close();
server.close();
System.out.println("Server interrupted");
}
catch (IOException ex){
ex.printStackTrace();
}
}
}
public void sendData(String string){
for (ClientHandler cl : clients){
cl.sendString(string);
}
}
public void removeClient(ClientHandler cl){
clients.remove(cl);
}
}