-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
51 lines (48 loc) · 1.7 KB
/
ChatServer.java
File metadata and controls
51 lines (48 loc) · 1.7 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
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer implements Runnable{
private static ServerSocket serverSocket;
private static ArrayList<Socket> clients = new ArrayList<Socket>();
public static int port = 8080;
public ChatServer() throws IOException{
serverSocket = new ServerSocket(port);
serverSocket.setSoTimeout(60000);
System.out.println("[ChatServer]: Waiting for client on port " + serverSocket.getLocalPort() + "...");
new Thread(this).start();
}
public void run(){
while(true){
try{
Socket server = serverSocket.accept();
System.out.println("[ChatServer]: Just connected to " + server.getRemoteSocketAddress());
clients.add(server);
Thread msgHandler = new Thread(new Thread(){
public void run(){
while(true){
try{
DataInputStream in = new DataInputStream(server.getInputStream());
String msg = in.readUTF();
for(int j = 0 ; j < clients.size() ; j++){
DataOutputStream out = new DataOutputStream(clients.get(j).getOutputStream());
/* Send data to the ClientSocket */
out.writeUTF(msg);
out.flush();
}
}catch(SocketTimeoutException s){
System.out.println("[ChatServer]: Socket timed out!");
break;
}catch(IOException er){
clients.remove(server);
break;
}
}
}
});
msgHandler.start();
}catch(IOException e){
break;
}
}
}
}