-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUser.java
More file actions
419 lines (365 loc) · 10.1 KB
/
User.java
File metadata and controls
419 lines (365 loc) · 10.1 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package Project;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.Vector;
import Server.User2;
import dao.userDao;
import model.userMessage;
public class User implements Runnable {
private String ip = "";
private int port = 0;
private String flagString;
private OutputStream os;
private BufferedWriter bw;
private InputStream input;
private BufferedReader in;
private InputStreamReader reader;
private Socket socket = null;
private Thread thread = null;
private boolean sendOK = false;
private boolean reconnect = false;
private String username;
private int localPort = 0;
private boolean isadd = false;
private boolean interruptWithServer = false;
private userMessage userMessage = null;
public User(userMessage userMessage) {
ip = "localhost";
port = 8888;
this.userMessage = userMessage;
username = this.userMessage.getName();
localPort = this.userMessage.getAccnumber();
}
public void run() { // 手动登录,后面有一个重新连接(类似)
try {
Thread.sleep(2000);
InetAddress inetAddress = InetAddress.getByName(ip);
socket = new Socket(inetAddress, port);
CFrame.sendPoint = false; // 不发点了
if (socket.isConnected() && !socket.isClosed()) {
Thread.sleep(1000);
flagString = "***********************成功连接服务器***********************";
CFrame.jTextArea2.append(flagString + "\r\n");
CFrame.jTextArea2.selectAll();
CFrame.isOnline = true; //已上线
userDao dao = new userDao();
try {
dao.updateOnlineflag(this.userMessage.getAccnumber());
} catch (SQLException e) {
}
CFrame.UDPExit = false; //判断UDP是否监听
setInterruptWithServer(false); //判断是否服务器断开连接
os = socket.getOutputStream();
bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write(username + "," + localPort + "\n");
bw.flush(); // 发送用户名 和本地监听的端口
input = socket.getInputStream();
in = new BufferedReader(new InputStreamReader(input));
// 接收服务器信息的线程
Thread thread = new Thread(new Read(this));
thread.start();
Thread thread2 = new Thread(new ReadClient(localPort,this.userMessage));
thread2.start();
} else {
flagString = "连接失败...";
}
} catch (UnknownHostException e) {
flagString = "连接失败...";
} catch (IOException e) {
// flagString = "连接失败...";
CFrame.sendPoint = false; // 不发点了
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
CFrame.jTextArea2.append("Error:连接超时......\r\n");
CFrame.jTextArea2.selectAll();
System.out.println("连接超时");
CFrame.flag = true; // flag用来控制能否点登录按钮
} catch (InterruptedException e) {
}
}
public void write(String send) throws SQLException {
try {
if (socket.isConnected() && !socket.isClosed()) { // 判断是否连接成功,是否传送成功
bw.write(send);
bw.flush();
setsendOK(true);
System.out.println("发送成功");
}
} catch (IOException e) {
System.out.println("发送失败");
CFrame.jTextArea2.append("已经与客户端断开连接,发送失败...\r\n");
CFrame.jTextArea2.selectAll();
}
}
public void read() throws IOException, InterruptedException {
try {
for (String string = in.readLine(); string != null; string = in.readLine()) {
if (string.equals("qwertyuiop[]")) {
System.out.println("刷新好友");
String ClientUsername = in.readLine();// 读取上线的好友的名字
if (!ClientUsername.equals(username)) {
CFrame.jTextArea2.append("您的好友 " + ClientUsername + " 已上线!\r\n");
}
ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
try {
Object obj = oi.readObject();
Vector<User2> vt = (Vector<User2>) obj;
Object obj2 = oi.readObject();
Vector vt1 = (Vector) obj2;
int RepeatIndex = vt1.indexOf(username);
vt.remove(RepeatIndex);
vt1.remove(RepeatIndex);
CFrame.vt = vt;
CFrame.vt1 = vt1;
setIsadd(true);
} catch (ClassNotFoundException e) {
}
} else if (string.equals("qwertyuiop[]clientexit")) {
System.out.println("刷新好友");
String ClientUsername = in.readLine();
if (!ClientUsername.equals(username)) {
CFrame.jTextArea2.append("您的好友 " + ClientUsername + " 已下线!\r\n");
}
ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
try {
Object obj = oi.readObject();
Vector<User2> vt = (Vector<User2>) obj;
Object obj2 = oi.readObject();
Vector vt1 = (Vector) obj2;
int RepeatIndex = vt1.indexOf(username);
try {
vt.remove(RepeatIndex);
vt1.remove(RepeatIndex);
} catch (Exception e) {
}
CFrame.vt = vt;
CFrame.vt1 = vt1;
setIsadd(true);
} catch (ClassNotFoundException e) {
}
} else {
CFrame.jTextArea2.append(string + "\r\n"); // 收到信息
CFrame.jTextArea2.selectAll();// 注意!
}
}
} catch (IOException e) {
if(!CFrame.ClientExit){
CFrame.jTextArea2.append("提示(1):服务器已中断……不影响与好友通信\r\n");
CFrame.jTextArea2.append("提示(2):与服务器失去连接,失去好友下线提示功能\r\n");
CFrame.jTextArea2.append("提示(3):重新登录并且连上服务器才能刷新好友列表\r\n");
CFrame.jTextArea2.selectAll();
setInterruptWithServer(true);
socket.close();
}
}
}
public userMessage getUserMessage() {
return userMessage;
}
public void setUserMessage(userMessage userMessage) {
this.userMessage = userMessage;
}
public int getLocalPort() {
return localPort;
}
public void setLocalPort(int localPort) {
this.localPort = localPort;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getflagString() {
return flagString;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public boolean getsendOK() {
return sendOK;
}
public void setsendOK(boolean sendOK) {
this.sendOK = sendOK;
}
public String getFlagString() {
return flagString;
}
public void setFlagString(String flagString) {
this.flagString = flagString;
}
public boolean getIsadd() {
return isadd;
}
public void setIsadd(boolean isadd) {
this.isadd = isadd;
}
public boolean isInterruptWithServer() {
return interruptWithServer;
}
public void setInterruptWithServer(boolean interruptWithServer) {
this.interruptWithServer = interruptWithServer;
}
}
class Read implements Runnable {
private User client;
public Read(User client) {
this.client = client;
}
@Override
public void run() {
try {
client.read();
} catch (IOException e) {
e.printStackTrace();
System.out.println("错误1");
} catch (InterruptedException e) {
System.out.println("错误2");
}
}
}
class ReadClient implements Runnable { //UDP通信
public static DatagramSocket socket = null;
private int localport = 0;
private userMessage userMessage = null;
public ReadClient(int localport,userMessage userMessage) {
this.localport = localport;
this.userMessage = userMessage;
}
@Override
public void run() {
try {
socket = new DatagramSocket(localport); // 监听这个端口
} catch (SocketException e1) {
System.out.println("错误11");
}
while (true) {
if (!socket.isClosed()) {
byte[] buf = new byte[2048];
DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
try {
socket.receive(packet);
String info = new String(buf, 0, packet.getLength());
int temp = info.indexOf(':');
String temp2 = info.substring(0, temp-1); //temp2是用户名
System.out.println(temp2);
int temp3 = CFrame.vt1.indexOf(temp2);
Thread notifyThread = new Thread(new notify(temp3));
notifyThread.start();
CFrame.jTextArea2.append("提示: "+ temp2 + "向您发了一条消息……\r\n");
CFrame.vt.get(temp3).setstring(info + "\r\n");
CFrame.jTextArea2.selectAll();
userDao dao = new userDao();
try {
//聊天记录
dao.updateChatlog(this.userMessage, info + "\r\n");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (IOException e) {
}
}
else {
System.out.println("socket已关闭,并且关闭了端口");
break;
}
}
}
}
class notify implements Runnable{
private int index;
public notify(int index){
this.index = index;
}
@Override
public void run() {
CFrame.jList1.setSelectedIndex(index);
try {
Thread.sleep(90);
} catch (InterruptedException e) {
}
CFrame.jList1.clearSelection();
try {
Thread.sleep(90);
} catch (InterruptedException e) {
}
CFrame.jList1.setSelectedIndex(index);
try {
Thread.sleep(90);
} catch (InterruptedException e) {
}
CFrame.jList1.clearSelection();
try {
Thread.sleep(90);
} catch (InterruptedException e) {
}
CFrame.jList1.setSelectedIndex(index);
try {
Thread.sleep(90);
} catch (InterruptedException e) {
}
CFrame.jList1.clearSelection();
try {
Thread.sleep(90);
} catch (InterruptedException e) {
}
CFrame.jList1.setSelectedIndex(index);
try {
Thread.sleep(90);
} catch (InterruptedException e) {
}
CFrame.jList1.clearSelection();
try {
Thread.sleep(90);
} catch (InterruptedException e) {
}
CFrame.jList1.setSelectedIndex(index);
try {
Thread.sleep(90);
} catch (InterruptedException e) {
}
CFrame.jList1.clearSelection();
try {
Thread.sleep(90);
} catch (InterruptedException e) {
}
if(CFrame.index==0){
CFrame.jList1.clearSelection();
}
else{
CFrame.jList1.setSelectedIndex(CFrame.index);
}
}
}