Repository navigation
Expand file tree
/
Copy pathChatServer.java
More file actions
46 lines (37 loc) · 1.44 KB
/
Copy pathChatServer.java
File metadata and controls
46 lines (37 loc) · 1.44 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
/******************************************************************************
* Compilation: javac ChatServer.java
* Execution: java ChatServer
* Dependencies: In.java Out.java Connection.java ConnectionListener.java
*
* Creates a server to listen for incoming connection requests on
* port 4444.
*
* % java ChatServer
*
* Remark
* -------
* - Use Vector instead of ArrayList since it's synchronized.
*
******************************************************************************/
import java.net.Socket;
import java.net.ServerSocket;
import java.util.Vector;
public class ChatServer {
public static void main(String[] args) throws Exception {
Vector<Connection> connections = new Vector<Connection>();
ServerSocket serverSocket = new ServerSocket(4444);
ConnectionListener connectionListener = new ConnectionListener(connections);
// thread that broadcasts messages to clients
connectionListener.start();
System.err.println("ChatServer started");
while (true) {
// wait for next client connection request
Socket clientSocket = serverSocket.accept();
System.err.println("Created socket with client");
// listen to client in a separate thread
Connection connection = new Connection(clientSocket);
connections.add(connection);
connection.start();
}
}
}