Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
600 lines (551 loc) · 21.5 KB

File metadata and controls

600 lines (551 loc) · 21.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
package Client_Oliver;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import Client_Oliver.C_Oliver;
import Server.FileSend;
import Server.User2;
public class CFrame_Oliver extends javax.swing.JFrame {
public static boolean UDPExit = false;
public static boolean isOnline = false;
public static boolean ClientExit = false;
public static boolean flag = true;
public static boolean sendPoint = true;
private C_Oliver client = null;
public static Vector<User2> vt = new Vector<>();
public static Vector vt1 = new Vector();
public static SystemTray sysTray;
public static TrayIcon trayIcon;
public static ImageIcon icon = null;
public static Image image = null;
// Variables declaration - do not modify
public static javax.swing.JList<String> jList1;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextArea jTextArea1;
public static javax.swing.JTextArea jTextArea2;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JFileChooser jFileChooser;
private boolean Morechooes = false;
public static int index = 0;
public static boolean flag2 = false; //
public static int count = 0;
public static int file_port_count = 0;
public CFrame_Oliver(C_Oliver client) {
this.client = client;
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jScrollPane2 = new javax.swing.JScrollPane();
jTextArea2 = new javax.swing.JTextArea();
jScrollPane3 = new javax.swing.JScrollPane();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jList1 = new javax.swing.JList<>();
jList1.setFixedCellHeight(31);
jRadioButton1 = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jTextArea1.setColumns(20);
jTextArea1.setRows(2);
jScrollPane1.setViewportView(jTextArea1);
jTextArea2.setColumns(20);
jTextArea2.setRows(5);
jScrollPane2.setViewportView(jTextArea2);
jTextArea2.setEditable(false);
jTextArea1.setLineWrap(true);
jTextArea1.setWrapStyleWord(true);
jTextArea2.setLineWrap(true);
jTextArea2.setWrapStyleWord(true);
jButton2.setText("登录(E)");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
jButton2ActionPerformed(evt);
} catch (InterruptedException e) {
}
}
});
jButton3.setText("退出(Q)");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
try {
jButton3ActionPerformed(evt);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
jRadioButton1.setText("群发(t)");
jScrollPane3.setViewportView(jList1);
jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jRadioButton1ActionPerformed(evt);
}
});
jFileChooser = new javax.swing.JFileChooser();
jButton4 = new javax.swing.JButton();
jButton4.setText("发送文件");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
jButton4ActionPerformed(evt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup().addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 144,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup().addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2))
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 123,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 351,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 351,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup().addGap(1, 1, 1)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 83,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 30,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 227,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 75,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane3)))
.addGroup(jPanel1Layout.createSequentialGroup().addGap(350, 350, 350)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3).addComponent(jButton1)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jRadioButton1))))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] { jButton2, jButton3 });
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 524, javax.swing.GroupLayout.PREFERRED_SIZE));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE));
MouseListener Danxuan = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
if (!Morechooes) { // 单发状态
jList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Oliver_ReadClient.isNewMessage = false;//解除托盘闪动状态//解除托盘闪动状态
if (jList1.isSelectionEmpty()) {
System.out.println("没有客户端");
return;
} else {
index = jList1.locationToIndex(e.getPoint());
jTextArea2.setText(vt.get(index).getstring());
flag = true;
System.out.println(vt.get(index).toString() + " 被选中");
// index是选择的客户的标志
}
}
}
}
};
jList1.addMouseListener(Danxuan);
MouseListener Duoxuan = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
if (Morechooes) { // 群发状态
jList1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
if (jList1.isSelectionEmpty()) {
jList1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
System.out.println("没有客户端");
return;
}
int index = jList1.locationToIndex(e.getPoint());
jTextArea2.setText(vt.get(index).getstring());
}
}
}
};
jList1.addMouseListener(Duoxuan);
jButton1.setText("发送");
jButton1.addActionListener((new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
try {
if (!isOnline) {
JOptionPane.showMessageDialog(null, "Error:请先登录", "WARNING", JOptionPane.WARNING_MESSAGE);
return;
} else if (jList1.isSelectionEmpty()) {
JOptionPane.showMessageDialog(null, "Error:没有选中要发送的客户", "WARNING", JOptionPane.WARNING_MESSAGE);
return;
} else if (!Morechooes)
Danfa(index, jTextArea1.getText());
else
Qunfa();
} catch (IOException e) {
}
}
}));
jButton1.setMnemonic(java.awt.event.KeyEvent.VK_ENTER);
jButton2.setMnemonic(java.awt.event.KeyEvent.VK_E);
jButton3.setMnemonic(java.awt.event.KeyEvent.VK_Q);
jRadioButton1.setMnemonic(java.awt.event.KeyEvent.VK_T);
pack();
}
public void Qunfa() throws IOException {
String send = jTextArea1.getText();
String sendbyMe = "\t\t\t Me: " + send + "\r\n";
jTextArea2.append(sendbyMe);
jTextArea2.selectAll();
int[] indexs = jList1.getSelectedIndices();
for (int i = 0; i < indexs.length; i++) {
vt.get(indexs[i]).setstring(sendbyMe);
InetAddress address = InetAddress.getByName("localhost");
int port = vt.get(indexs[i]).getUserport();
String send2 = client.getUsername() + " : " + send;
byte[] bufsend = send2.getBytes();
DatagramPacket packet = new DatagramPacket(bufsend, 0, bufsend.length, address, port);
DatagramSocket socket = new DatagramSocket(); // 动态端口仅仅是为了发数据报
socket.send(packet);
socket.close();
}
jTextArea1.setText("");
}
public void Danfa(int i, String send) throws IOException { // UDP通信
String sendbyMe = "\t\t\t Me: " + send + "\r\n";
vt.get(i).setstring(sendbyMe);
jTextArea2.append(sendbyMe);
jTextArea2.selectAll();
InetAddress address = InetAddress.getByName("localhost");
int port = vt.get(i).getUserport(); // 选中的用户对应socket的输出流
send = client.getUsername() + " : " + send;
byte[] bufsend = send.getBytes();
DatagramPacket packet = new DatagramPacket(bufsend, 0, bufsend.length, address, port);
DatagramSocket socket = new DatagramSocket(); // 动态端口仅仅是为了发数据报
socket.send(packet);
socket.close();
jTextArea1.setText("");
}
// 登录按钮
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) throws InterruptedException {
if (client.getSocket() == null && !flag) {
JOptionPane.showMessageDialog(null, "Error:当前正在尝试连接,请稍等...", "WARNING", JOptionPane.WARNING_MESSAGE);
return;
} else if (client.getSocket() == null && flag) { // 登录按钮
// 当一次都没连上的时候调用这个
jTextArea2.append("正在连接...请稍后...\r\n");
flag = false;
ClientExit = false;
sendPoint = true;
Thread connecting = new Thread(client); // 连接的线程
connecting.start();
Thread sendPoint = new SendPoint(); // 异步显示*****的线程
sendPoint.start();
} else if (!(!client.getSocket().isClosed() && client.getSocket().isConnected())) {
// 连上第一次后再次断开则调用这个
jTextArea2.append("正在连接...请稍后...\r\n");
jTextArea2.selectAll();
flag = false;
ClientExit = false;
sendPoint = true;
Thread connecting = new Thread(client); // 连接的线程
connecting.start();
Thread sendPoint = new SendPoint(); // 异步显示*****的线程
sendPoint.start();
} else {
JOptionPane.showMessageDialog(null, "Error:您已经在线了!", "WARNING", JOptionPane.WARNING_MESSAGE);
return;
}
}
// 退出按钮
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) throws IOException, InterruptedException {
if (UDPExit) {
JOptionPane.showMessageDialog(null, "Error:(1)与服务器断开连接\n (2)已经释放UDP监听的端口", "WARNING",
JOptionPane.WARNING_MESSAGE);
return;
}
if (!client.isInterruptWithServer()) { // 先判断是否服务器先关闭
if (client.getSocket() == null) {
JOptionPane.showMessageDialog(null, "Error:当前没有连接服务器", "WARNING", JOptionPane.WARNING_MESSAGE);
return;
} else if (!(!client.getSocket().isClosed() && client.getSocket().isConnected())) {
JOptionPane.showMessageDialog(null, "Error:您已经与服务器断开连接", "WARNING", JOptionPane.WARNING_MESSAGE);
return;
} else {
System.out.println("用户主动退出服务器...");
jTextArea2.append("用户主动退出服务器...\r\n");
jTextArea2.selectAll();
ClientExit = true;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getSocket().getOutputStream()));
bw.write("qwertyuiop[]asd\r\n");// 好友刷新信号
bw.flush();
try {
Thread.sleep(350); // 必须缓冲!不然来不及更新
client.getSocket().close();
Oliver_ReadClient.socket.close();
isOnline = false;
} catch (IOException e) {
}
}
} else {
Oliver_ReadClient.socket.close(); // 释放UDP的端口
jTextArea2.append("用户释放UDP通信的端口...\r\n");
isOnline = false;
UDPExit = true;
jTextArea2.selectAll();
}
Thread.sleep(50);
Vector Empty = new Vector();
Empty.removeAllElements();
jList1.setListData(Empty); // 退出后清空好友列表
}
// 发送文件
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) throws IOException, InterruptedException {
if (!isOnline) {
JOptionPane.showMessageDialog(null, "Error:请先登录", "WARNING", JOptionPane.WARNING_MESSAGE);
return;
} else if (jList1.isSelectionEmpty()) {
JOptionPane.showMessageDialog(null, "Error:没有选中要发送的客户", "WARNING", JOptionPane.WARNING_MESSAGE);
return;
} else if (Morechooes) {
JOptionPane.showMessageDialog(null, "Error:发送文件不能群发", "WARNING", JOptionPane.WARNING_MESSAGE);
return;
} else // 正式发送文件
{
file_port_count++;
Thread thread = new Thread(new SendFile(client, jFileChooser));
thread.start();
}
}
// 是否群发按钮
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (jRadioButton1.isSelected()) {
Morechooes = true;
jTextArea2.append("请把您要群发的客户选上...\r\n");
} else
Morechooes = false;
}
public static void main(String args[]) throws InterruptedException, AWTException {
if (System.getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1)
{
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
C_Oliver client = new C_Oliver();
CFrame_Oliver clientFrame = new CFrame_Oliver(client);
clientFrame.setTitle("Client_Oliver");
clientFrame.setResizable(false);
clientFrame.setLocation(350, 200);
clientFrame.setVisible(true);
image = clientFrame.getToolkit().getImage(clientFrame.getClass().getClassLoader().getResource("com/img/Oliver.gif"));
clientFrame.setIconImage(image); //图标
clientFrame.createTrayIcon(clientFrame);//创了托盘
sysTray.add(trayIcon);
new Thread(new notify2()).start(); //开启托盘自动闪动的线程
while (true) { //看有没有好友列表的刷新
Thread.sleep(100); // 利用线程的睡眠减轻cpu的负担,不用这个方法的时候cpu占30%
if (client.getIsadd()) {
jList1.setListData(vt1);
client.setIsadd(false);
}
}
}
public void createTrayIcon(final CFrame_Oliver clientFrame) {
sysTray = SystemTray.getSystemTray();
PopupMenu popupMenu = new PopupMenu();
MenuItem mi = new MenuItem("打开");
MenuItem exit = new MenuItem("退出");
popupMenu.add(mi);
popupMenu.add(exit);
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Oliver_ReadClient.isNewMessage = false;//解除托盘闪动状态
clientFrame.setExtendedState(JFrame.NORMAL);
clientFrame.setVisible(true); // 显示窗口
clientFrame.toFront(); // 显示窗口在最前端
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
trayIcon = new TrayIcon(image, "消息盒子", popupMenu);
trayIcon.setImageAutoSize(true);
trayIcon.addMouseListener(new MouseAdapter() { //单击托盘时
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
Oliver_ReadClient.isNewMessage = false;//解除托盘闪动状态
clientFrame.setExtendedState(JFrame.NORMAL);
clientFrame.setVisible(true);
clientFrame.toFront();
}
}
});
}
}
class SendFile implements Runnable {
private C_Oliver client;
private javax.swing.JFileChooser jFileChooser;
public SendFile(C_Oliver client, javax.swing.JFileChooser jFileChooser) {
this.client = client;
this.jFileChooser = jFileChooser;
}
@Override
public void run() {
//传文件的步骤
int count = CFrame_Oliver.file_port_count;
User2 c = CFrame_Oliver.vt.get(CFrame_Oliver.index);
jFileChooser.setFileSelectionMode(jFileChooser.FILES_ONLY);
int returnVal2 = jFileChooser.showDialog(new JLabel(), "发送");
if (returnVal2 == jFileChooser.APPROVE_OPTION) {
byte[] bufsend = ((client.getLocalPort() + count) + "," + "qazxswedcvfr,"
+ client.getUsername() + "," + jFileChooser.getSelectedFile().getName()
+ "," + (client.getLocalPort() - count)).getBytes();
try {
DatagramPacket packet = new DatagramPacket(bufsend, 0, bufsend.length,
InetAddress.getByName("localhost"), c.getUserport());
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
socket.close();
} catch (Exception e) {
}
} else {
return; // 添加点击撤销按钮后的事件
}
DatagramSocket socket2 = null; //对方是否接收文件,属于反馈信息
try {
socket2 = new DatagramSocket(client.getLocalPort() - count);
} catch (SocketException e) {
}
byte[] buf = new byte[2048];
DatagramPacket packet2 = new DatagramPacket(buf, 0, buf.length);
try {
socket2.receive(packet2);
} catch (IOException e) {
}
String info = new String(buf, 0, packet2.getLength());//反馈信息
if (info.contains("sureRec"))// 开始发送
{
String[] temp = info.split("c");
int remoteport = Integer.parseInt(temp[1]);
String path = jFileChooser.getSelectedFile().getAbsolutePath();
FileSend send = new FileSend(remoteport, client.getLocalPort() + count, path,
"localhost");// 添加点击打开按钮后的事件
send.start();
CFrame_Oliver.jTextArea2.append("您向用户 " + c.toString() +"发送文件:" + jFileChooser.getSelectedFile().getName()+"\r\n");
}
socket2.close();
}
}
class SendPoint extends Thread { // 异步_登录显示点
public void run() {
while (CFrame_Oliver.sendPoint)// 当操作完成就停止这个
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
CFrame_Oliver.jTextArea2.append("…");
CFrame_Oliver.jTextArea2.selectAll();
}
CFrame_Oliver.jTextArea2.append("\r\n");
}
}
class notify2 implements Runnable { //托盘闪动
@Override
public void run() {
while (true) {
if (Oliver_ReadClient.isNewMessage) {
try {
CFrame_Oliver.trayIcon.setImage(new ImageIcon("").getImage());
Thread.sleep(400);
CFrame_Oliver.trayIcon.setImage(CFrame_Oliver.image);
Thread.sleep(400);
} catch (Exception e) {
}
} else
CFrame_Oliver.trayIcon.setImage(CFrame_Oliver.image);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.