-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.java
245 lines (222 loc) · 7.46 KB
/
server.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package com.example.wuziqiserver;
import java.awt.Point;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class Server {
//private BufferedReader in;
private ObjectInputStream inObj;
private PrintWriter out;
private List<Socket> sockets=new ArrayList<Socket>();
private Map<Socket,Socket> maps=new HashMap<Socket, Socket>();
public static void main(String[] args) {
// TODO Auto-generated method stub
new Server();
}
public Server(){
try {
ServerSocket serverSocket=new ServerSocket(1888);
System.out.println("服务器运行成功");
Socket client=null;
BufferedReader in=null;
while(true){
client=serverSocket.accept();
sockets.add(client);
in=new BufferedReader(new InputStreamReader(client.getInputStream(),"UTF-8"));
System.out.println("ip:"+client.getRemoteSocketAddress().toString());
new Thread(new ReceiveThread(client,in)).start();
}
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("服务器运行异常");
e.printStackTrace();
}
}
class ReceiveThread implements Runnable{
private Socket socket;
private BufferedReader br;
public ReceiveThread(Socket socket,BufferedReader br){
this.socket=socket;
this.br=br;
//System.out.println("启动接收消息线程");
}
public void run() {
// TODO Auto-generated method stub
String msg=null;
while(true){
try {
while((msg=br.readLine())!=null){
System.out.println("收到客户端信息:"+msg);
handleClientMessage(msg,socket);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void handleClientMessage(String msg,Socket socket){
int index=-1;
Socket value=null;
if(msg.equals("match")){
//随机匹配有效用户,添加到maps
//System.out.println("系统正在为你匹配玩家");
value=getValidUser(socket);
if(value==null){
send(socket, "没有匹配到玩家");
}else{
//System.out.println("匹配成功");
maps.put(socket, value);
//send(value, "匹配成功");
//send(socket, "匹配成功");
//随机让一个玩家先手
int r=getRandom(2);
System.out.println("r="+r);
if(r==0){
send(socket,"你先手");
send(value,"对方先手");
}
else if(r==1){
send(socket, "对方先手");
send(value, "你先手");
}
}
}else if(msg.equals("match_failed")){
sockets.remove(socket);
}else if(msg.equals("exit") || msg.equals("leave")){
//System.out.println("退出的socket是:"+socket.toString());
send(getMyMatch(socket), msg);
deleteMatch(socket);
sockets.remove(socket);
Thread.currentThread().interrupt();//终止线程
/*try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}else if((index=msg.indexOf(","))>-1){
send(getMyMatch(socket), msg.substring(0, index)+","+msg.substring(index+1));
}else if(msg.startsWith("chat")){
//int n=Integer.parseInt(msg.substring(3));
send(getMyMatch(socket), msg);
}else if(msg.equals("timeout_no_play")){
send(getMyMatch(socket), "oppo_timeout");
}else if(msg.equals("continue")){
int r=getRandom(2);
System.out.println("r="+r);
if(r==0){
send(socket,"你先手");
send(getMyMatch(socket),"对方先手");
}
else if(r==1){
send(socket, "对方先手");
send(getMyMatch(socket), "你先手");
}
}
}
//找到对手
private Socket getMyMatch(Socket socket){
for(Map.Entry<Socket, Socket> entry : maps.entrySet()){
if(entry.getKey()==socket){
return entry.getValue();
}else if(entry.getValue()==socket){
return entry.getKey();
}
}
return null;
}
private int getRandom(int max){
Random r=new Random();
return r.nextInt(max);
}
private Socket getValidUser(Socket socket){
//int index;
Socket value=null;
Socket temp=null;
/*if(sockets.size()>1){
do{
index=getRandom(sockets.size());
//System.out.println(index);
value=sockets.get(index);
}while((value==socket || isMatchOther(value)) && isMatched(socket)==false);
}*/
//循环匹配
for(int i=0;i<sockets.size();i++){
temp=sockets.get(i);
if(temp!=socket && isMatchOther(temp)==false && isMatchOther(socket)==false){
value=temp;
break;
}
}
return value;
}
//已经匹配了别人
private boolean isMatchOther(Socket socket){
for(Map.Entry<Socket, Socket> entry : maps.entrySet()){
if(entry.getKey()==socket || entry.getValue()==socket){
return true;
}
/*if(entry.getValue()==socket && entry.getKey()!=null){
return true;
}*/
}
return false;
}
//自己是否已匹配
/*private boolean isMatched(Socket socket){
for(Map.Entry<Socket, Socket> entry : maps.entrySet()){
if(entry.getKey()==socket || entry.getValue()==socket ){
//send(socket, "你已经在匹配中");
return true;
}
}
return false;
}*/
//删除匹配用户
//socket是退出的socket
private void deleteMatch(Socket socket){
for(Map.Entry<Socket, Socket> entry : maps.entrySet()){
Socket key=entry.getKey();
Socket value=entry.getValue();
//System.out.println(key.toString()+","+value.toString());
if(key!=null && key==socket){
//System.out.println("key=socket");
maps.remove(key,value);
//if(value!=null)
//send(value, "对方离线");
}
if(value!=null && value==socket){
//System.out.println("value=socket");
maps.remove(key,value);
//send(key, "对方离线");
}
}
}
private void send(Socket socket,String msg){
try {
if(socket!=null){
System.out.println("发送->"+msg);
out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8")));
out.println(msg);
out.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}