-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyQQ.java
199 lines (192 loc) · 6.12 KB
/
MyQQ.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.net.*;
public class MyQQ {
public static void main (String[] args) throws IOException {
Frame frame = new Frame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class Frame extends JFrame {
public Frame() throws IOException{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenW = screenSize.width;
int screenH = screenSize.height;
int frameW = 600;
int frameH = 650;
int x = screenW / 4;
int y = screenH / 4;
setSize(frameW, frameH);
setLocationRelativeTo(null);
setTitle("chat~");
Panel panel = new Panel(this);
add(panel);
}
}
class Panel extends JPanel implements ActionListener {
public Panel(Frame parent) {
setLayout(null);
pt = parent;
JLabel nameLb = new JLabel("昵称");
JScrollPane ssay = new JScrollPane(say);
JScrollPane slog = new JScrollPane(log);
add(nameLb);
add(name);
add(ssay);
add(slog);
add(sendMsg);
add(sendFile);
add(accept);
nameLb.setBounds(10, 410, 50, 20);
name.setBounds(60, 410, 100, 20);
ssay.setBounds(10, 430, 490, 200);
slog.setBounds(10, 10, 490, 400);
sendMsg.setBounds(610, 630, 60, 20);
sendFile.setBounds(550, 630, 60, 20);
accept.setBounds(480, 630, 60, 20);
try {
Scanner confile = new Scanner(new File("/home/soufii/JavaExp/config"));
address = confile.nextLine();
localTextPort = Integer.parseInt(confile.nextLine());
localFilePort = Integer.parseInt(confile.nextLine());
targetTextPort = Integer.parseInt(confile.nextLine());
targetFilePort = Integer.parseInt(confile.nextLine());
} catch (IOException er) {}
sendMsg.addActionListener(this);
sendFile.addActionListener(this);
accept.addActionListener(this);
try {
ss = new ServerSocket(localFilePort);
} catch (IOException err) {}
try {
dataSocket = new DatagramSocket(localTextPort);;
} catch (IOException e) {}
Runnable get = new ReviceMsg();
Thread t = new Thread(get);
t.start();
}
private DatagramSocket dataSocket = null;
private ServerSocket ss = null;
private String address = "";
private int targetTextPort,
localTextPort,
targetFilePort,
localFilePort;
private JTextField name = new JTextField();
private JTextArea say = new JTextArea(500, 400);
private JTextArea log = new JTextArea(500, 100);
private JButton sendMsg = new JButton("发送消息"),
sendFile = new JButton("传送文件"),
accept = new JButton("接收文件");
public Frame pt;
public void actionPerformed(ActionEvent e) {
if (e.getSource() == sendMsg) {
String msg = name.getText() + "说:" + say.getText() + "\n";
log.append(msg);
Runnable st = new SendMsg();
Thread t = new Thread(st);
t.start();
}
if (e.getSource() == sendFile) {
Runnable sf = new SendThread();
Thread t = new Thread(sf);
t.start();
}
if (e.getSource() == accept) {
Runnable af = new AcceptThread();
Thread t1 = new Thread(af);
t1.start();
}
}
private class SendMsg implements Runnable {
private DatagramPacket dataPacket;
public void run() {
try {
byte[] sendDataByte = new byte[1024];
String sendStr = name.getText() + "说:" + say.getText() + '\n';
sendDataByte = sendStr.getBytes();
dataPacket = new DatagramPacket(sendDataByte,
sendDataByte.length,
InetAddress.getByName(address),
targetTextPort);
dataSocket.send(dataPacket);
} catch (Exception err) {}
}
}
private class ReviceMsg implements Runnable {
public void run() {
try {
byte[] receiveByte = new byte[1024];
DatagramPacket dataPacket = new DatagramPacket(receiveByte, receiveByte.length);
String receiveStr;
while (true) {
dataSocket.receive(dataPacket);
receiveStr = new String(receiveByte, 0, dataPacket.getLength());
log.append(receiveStr);
}
} catch (Exception e) {}
}
}
private class SendThread implements Runnable {
JFileChooser fileChooser = new JFileChooser();
File sourcefile = null;
String path;
public void run() {
fileChooser.setCurrentDirectory(new File("."));
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
sourcefile = fileChooser.getSelectedFile();
path = sourcefile.getPath();
}
try {
Socket socket = new Socket(address, targetFilePort);
FileInputStream fs = new FileInputStream(path);
byte[] bytes = new byte[1024];
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
int length = 0;
while ((length = fs.read(bytes)) != -1) {
bos.write(bytes, 0, length);
}
bos.close();
fs.close();
socket.close();
log.append("文件传送完毕!\n");
} catch(Exception err) {}
}
}
private class AcceptThread implements Runnable {
JFileChooser fileChooser = new JFileChooser();
File targetfile = null;
String path;
public void run() {
fileChooser.setCurrentDirectory(new File("."));
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = fileChooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
targetfile = fileChooser.getSelectedFile();
path = targetfile.getPath();
}
try {
Socket socket = ss.accept();
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = new byte[1024];
FileOutputStream fos = new FileOutputStream(path + "/recivedfile");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len = 0;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bos.close();
fos.close();
bis.close();
socket.close();
log.append("文件接收成功!\n");
} catch(Exception err) {}
}
}
}