-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerHelper.java
52 lines (44 loc) · 1.11 KB
/
ServerHelper.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.net.*;
import java.io.*;
import java.util.*;
class ServerHelper extends Thread{
private Socket sock;
private DataOutputStream remoteOut;
private ChatServer server;
private boolean connected = true;
private DataInputStream remoteIn;
ServerHelper(Socket sock, DataOutputStream remoteOut, ChatServer server)throws IOException{
this.sock = sock;
this.remoteOut = remoteOut;
this.server = server;
remoteIn = new DataInputStream(sock.getInputStream());
}
public synchronized void run(){
String s;
try{
while(connected){
s = remoteIn.readUTF();
broadcast(s);
}
}
catch(IOException e){
System.out.println(e.getMessage() + ": failed");
}
}
private void broadcast(String s){
Vector clients = server.getClients();
DataOutputStream dataOut = null;
for(Enumeration e = clients.elements(); e.hasMoreElements();){
dataOut = (DataOutputStream)(e.nextElement());
if(!dataOut.equals(remoteOut)){
try{
dataOut.writeUTF(s);
}
catch(IOException a){
System.out.println(a.getMessage() + ": failed");
server.removeFromClients(dataOut);
}
}
}
}
}