-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
161 lines (146 loc) · 3.02 KB
/
Server.java
File metadata and controls
161 lines (146 loc) · 3.02 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package secondTry;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Server {
//Map不允许key相同
private Map<String,ServerThread> cs;
public void startup()
{
try {
ServerSocket ss = new ServerSocket(1236);
cs = new HashMap<String, ServerThread>();
while(true)
{
Socket s = ss.accept();
new Thread(new ServerThread(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server().startup();
}
class ServerThread implements Runnable
{
BufferedReader br = null;
PrintWriter out = null;
Socket s = null;
String name;
public ServerThread(Socket s) throws IOException
{
this.s = s;
br= new BufferedReader(new InputStreamReader(s.getInputStream()));
out= new PrintWriter(s.getOutputStream(),true);
name = br.readLine();
name+="["+s.getInetAddress()+"/"+"]";
cs.put(name,this);
send(name+"上线了");
//获取用户List
sendUserList();
}
//发送消息
public void send(String msg)
{
Set<String> s = cs.keySet();
for (String name:s)
{
cs.get(name).out.println(msg);
}
}
public void sendUserList()
{
Set<String> s = cs.keySet();
String user="connect:";
for (String users:s)
{
user+=users+",";
}
//更新用户列表
for (String name:s)
{
cs.get(name).out.println(user);
}
}
//提取私聊用户
public String userByMsg(String msg)
{
String str=null;
try {
str = msg.substring("to:".length(),msg.indexOf(":end"));
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
//私聊
public void sendPrivateuser(String user,String msg)
{
String users[] = user.split(",");
for (String u:users)
{
cs.get(u).out.println(name+": "+msg+"(私聊)");
}
}
//截取聊天信息
public String sendmsg(String msg)
{
String str = msg.substring(msg.indexOf(":end")+":end".length());
return str;
}
public void run()
{
try
{
while(true)
{
String msg = br.readLine();
if (msg.equals("disconnect:"))
{
//将下线的用户移除掉
cs.remove(name);
send(name+"下线了");
}
else if (msg.contains("to:"))
{
String us = userByMsg(msg);//用户
String str = sendmsg(msg);//消息
if (us.equals("all"))
{
send(name+str+"(群聊)");
}
else
{
sendPrivateuser(us,str);
}
}
else
{
send(name+msg+"(群聊)");
}
//获取用户List
sendUserList();
}
}
catch (SocketException se){}
catch (IOException e) {
e.printStackTrace();
}
finally{
try{
if (s!=null) s.close();
if (br!=null) br.close();
}
catch (IOException e) {e.printStackTrace();}
}
}
}
}