-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
57 lines (50 loc) · 1.49 KB
/
Server.java
File metadata and controls
57 lines (50 loc) · 1.49 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
47
48
49
50
51
52
53
54
55
56
57
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
private final ServerSocket serverSocket;
private static ArrayList<String> usersOnline=new ArrayList<>();
protected int count=0;
public Server(ServerSocket serverSocket)
{
this.serverSocket=serverSocket;
}
public static String getUsersList()
{
return String.join(", ", usersOnline);
}
public void startServer()
{
try {
while(!serverSocket.isClosed())
{
Socket socket=serverSocket.accept();
ClientHandler clientHandler=new ClientHandler(socket);
System.out.println(clientHandler.getClientUsername()+ " has connected");
usersOnline.add(clientHandler.getClientUsername());
count++;
Thread thread =new Thread(clientHandler);
thread.start();
}
}catch (IOException ex){
System.out.println("Error");
}
}
public void closeServerSocket()
{
try{
if(serverSocket!=null)
{
serverSocket.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException{
ServerSocket serverSocket=new ServerSocket(1234);
Server server=new Server(serverSocket);
server.startServer();
}
}