-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyThread.java
73 lines (67 loc) · 2.5 KB
/
MyThread.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
import java.util.Iterator;
import java.io.DataOutputStream;
import java.util.Date;
import java.io.DataInputStream;
import java.util.ArrayList;
import java.net.Socket;
class MyThread implements Runnable
{
Socket s;
ArrayList al;
ArrayList users;
String username;
MyThread(final Socket socket, final ArrayList al, final ArrayList users) {
this.s = socket;
this.al = al;
this.users = users;
try {
this.username = new DataInputStream(socket.getInputStream()).readUTF();
al.add(socket);
users.add(this.username);
this.tellEveryOne("****** " + this.username + " Logged in at " + new Date() + " ******");
this.sendNewUserList();
}
catch (Exception obj) {
System.err.println("MyThread constructor " + obj);
}
}
public void run() {
try {
final DataInputStream dataInputStream = new DataInputStream(this.s.getInputStream());
while (true) {
final String utf = dataInputStream.readUTF();
if (utf.toLowerCase().equals("@@logoutme@@:")) {
break;
}
this.tellEveryOne(this.username + " said: " + " : " + utf);
}
final DataOutputStream dataOutputStream = new DataOutputStream(this.s.getOutputStream());
dataOutputStream.writeUTF("@@logoutme@@:");
dataOutputStream.flush();
this.users.remove(this.username);
this.tellEveryOne("****** " + this.username + " Logged out at " + new Date() + " ******");
this.sendNewUserList();
this.al.remove(this.s);
this.s.close();
}
catch (Exception obj) {
System.out.println("MyThread Run" + obj);
}
}
public void sendNewUserList() {
this.tellEveryOne("updateuserslist:" + this.users.toString());
}
public void tellEveryOne(final String str) {
final Iterator<Socket> iterator = this.al.iterator();
while (iterator.hasNext()) {
try {
final DataOutputStream dataOutputStream = new DataOutputStream(iterator.next().getOutputStream());
dataOutputStream.writeUTF(str);
dataOutputStream.flush();
}
catch (Exception obj) {
System.err.println("TellEveryOne " + obj);
}
}
}
}