-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientChatRunnable.java
More file actions
57 lines (44 loc) · 1.45 KB
/
Copy pathClientChatRunnable.java
File metadata and controls
57 lines (44 loc) · 1.45 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
package Chat;
import java.io.*;
import java.net.Socket;
import java.util.Map;
/**
* @author wukai
* @date 2019/6/14
*/
public class ClientChatRunnable implements Runnable{
private Socket socket;
private BufferedReader reader;
private OutputStream writer;
public ClientChatRunnable(Socket socket) {
this.socket = socket;
}
public void run() {
try {
String msg = null;
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = socket.getOutputStream();
writer.write("请输入用户名:\n".getBytes());
msg = reader.readLine();
System.out.println("用户名: " + msg);
writer.write("请输入密码:\n".getBytes());
msg = reader.readLine();
System.out.println("密码: " + msg);
writer.write("登陆成功:\n".getBytes());
while ((msg = reader.readLine()) != null) {
msg += "\n";
for (Socket item : ChatServer.clientList) {
if (item != socket) {
System.out.println("发送消息["+msg+"]到 "+item);
item.getOutputStream().write(msg.getBytes());
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void printLine(String string) {
System.out.println(string);
}
}