-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nap.java
357 lines (299 loc) · 9.2 KB
/
Nap.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
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
import java.io.*;
import java.net.*;
//用于计算hash值
import org.apache.commons.codec.digest.DigestUtils;
class Global
{
public static String path = "";
}
class peer_server implements Runnable
{
// 实现一个基本的文件服务器
public void run()
{
try
{
// 预定义
String path = Global.path;
// 建立用于接收服务器消息的ServerSocket
ServerSocket comServSock = new ServerSocket(7701);
// 建立用于接收另一个peer消息、传输文件的ServerSocket
ServerSocket fileServSock = new ServerSocket(7702);
while(true)
{
Socket socket = comServSock.accept();
//创建输入输出流
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), false);
String response = "";
String[] respArray;
// 循环监听连接请求,直到接收到"HELLO"或者"QUIT"握手消息
while(!response.equals("HELLO") && !response.equals("QUIT"))
{
response = in.readLine();
// 如果接收到的握手消息是OPEN,回复确认消息HELLO
if(response.equals("HELLO"))
{
out.println("ACCEPT");
out.flush();
}
}
// 循环监听连接请求,直到收到QUIT握手消息
while(!response.equals("QUIT"))
{
response = in.readLine();
//请求参数分段处理
respArray = response.split(" ");
// syntax: GET [filename]
if(respArray[0].equals("GET"))
{
try
{
// 请求的文件名不为空
if(!respArray[1].isEmpty())
{
// 新建一个用于文件传输的socket
Socket fileSocket = fileServSock.accept();
File peerfile = new File(path + File.separator + respArray[1]);
byte[] buffer = new byte[(int)peerfile.length()];
BufferedInputStream fileIn = new BufferedInputStream(new FileInputStream(peerfile));
fileIn.read(buffer, 0, buffer.length);
BufferedOutputStream fileOut = new BufferedOutputStream(fileSocket.getOutputStream());
fileOut.write(buffer, 0, buffer.length);
fileOut.flush();
fileIn.close();
fileOut.close();
fileSocket.close();
out.println("OK");
out.flush();
}
}
catch (Exception e)
{
out.print("ERROR "+e);
out.flush();
}
}
else if(response.equals("CLOSE"))
{
continue;
}
}
out.print("GOODBYE");
out.flush();
socket.close();
}
}
catch (Exception e)
{
System.out.println("\033[1;31m[错误] >>\033[0m "+e);
System.exit(-1);
}
}
}
public class Nap
{
public static void error_handler(String err)
{
System.out.println("\033[1;31m[错误] >>\033[0m " + err.substring(6));
System.exit(-1);
}
// Main method
public static void main(String[] args)
{
try
{
System.out.println("Nap客户端");
Socket socket;
BufferedReader in;
PrintWriter out;
// 初始化用于接收用户输入的stdin
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String server;//P2P服务器IP
int port;//P2P服务器端口
String path;//本地P2P工作目录
String request = "";
String[] reqArray;
String response;
String[] respArray;
//获取几个必要信息
System.out.print("服务器的IP地址 >> ");
server = stdin.readLine();
System.out.print("服务器的端口号 >> ");
port = Integer.parseInt(stdin.readLine());
System.out.print("本机的工作目录 >> ");
path = stdin.readLine();
Global.path = path;
socket = new Socket(server, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//服务器返回的消息
out = new PrintWriter(socket.getOutputStream(), false);//发送给服务器的消息
// 打印服务器返回的消息
System.out.println(in.readLine());
// 发送握手消息"CONNECT",开始握手
out.print("CONNECT");
out.flush();
response = in.readLine();
// 接收确认信息
if(!response.equals("ACCEPT"))
{
System.out.println("\033[1;31m[错误] >>\033[0m 向服务端发送的握手信息未能接收到正确的确认包");
System.exit(-1);
}
else
{
System.out.println("\033[1;32m[成功] >>\033[0m 成功连接到Napd服务器 " + server + ":" + port);
}
File folder = new File(path);
File[] files = folder.listFiles();
FileInputStream f_stream;
String filename;
String filehash;
String filesize;
System.out.println("[信息] 正在为工作目录 " + path + " 建立文件索引...");
int index_total = 0;
for(int i = 0; i < files.length; i++)
{
if(files[i].isFile())
{
filename = files[i].getName();
f_stream = new FileInputStream(files[i]);
filehash = DigestUtils.md5Hex(f_stream);
f_stream.close();
filesize = String.valueOf(files[i].length());
out.print("ADD " + filename + " " + filehash + " " + filesize);
out.flush();
response = in.readLine();
if(!response.equals("OK"))
error_handler(response);
else
{
System.out.print(". ");
index_total++;
}
}
}
System.out.println("\n\033[1;32m[成功] >>\033[0m 成功添加 " + index_total + " 个文件信息到服务器");
// 开启文件服务器线程
Runnable run = new peer_server();
Thread thread = new Thread(run);
thread.start();
System.out.println("[信息] 等待用户输入");
do
{
System.out.print(">> ");
request = stdin.readLine();
reqArray = request.split(" ");
if(request.equals("list"))
{
System.out.println("[信息] 正在向服务器请求文件列表...");
// 发送LIST命令
out.print("LIST");
out.flush();
int list_total = 0;
response = in.readLine();
respArray = response.split(" ");
while((!respArray[0].equals("OK")) && (!respArray[0].equals("ERROR")))
{
list_total++;
System.out.println(String.format("[%2d] : %20s [文件大小: %10s]", new Object[] { new Integer(list_total), respArray[0], respArray[1] }));
response = in.readLine();
respArray = response.split(" ");
}
System.out.println("[信息] 一共获取到 " + list_total + " 个文件");
if(!response.equals("OK"))
error_handler(response);
}
else if(reqArray[0].equals("request"))
{
try
{
if(!reqArray[1].isEmpty())
{
//发送REQUEST
out.print("REQUEST " + reqArray[1]);
out.flush();
response = in.readLine();
respArray = response.split(" ");
if(respArray[0].equals("OK"))
System.out.println("\033[1;31m[错误] >>\033[0m 在服务器上并未找到文件'" + reqArray[1]);
while((!respArray[0].equals("OK")) && (!respArray[0].equals("ERROR")))
{
//respArray格式:peer的IP+文件大小
Socket comSocket = new Socket(respArray[0], 7701);
String comResponse;
BufferedReader comIn = new BufferedReader(new InputStreamReader(comSocket.getInputStream()));
PrintWriter comOut = new PrintWriter(comSocket.getOutputStream(), false);
//验证身份
comOut.println("HELLO");
comOut.flush();
comResponse = comIn.readLine();
//确认
if(!comResponse.equals("ACCEPT"))
{
System.out.println("\033[1;31m[错误] >>\033[0m 客户端握手消息验证失败");
System.exit(-1);
}
Socket fileSocket = new Socket(respArray[0], 7702);
comOut.println("GET " + reqArray[1]);
comOut.flush();
InputStream fileIn = fileSocket.getInputStream();
File f = new File(path+File.separator+"recv");
if (!f.exists())
{
f.mkdirs();
}
BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream(path+File.separator+"recv"+File.separator + reqArray[1]));
int bytesRead,current = 0;
byte[] buffer = new byte[Integer.parseInt(respArray[1])];
bytesRead = fileIn.read(buffer, 0, buffer.length);
current = bytesRead;
System.out.println("[信息] 开始传输文件...");
do
{
System.out.print(". ");
bytesRead = fileIn.read(buffer, current, (buffer.length - current));
if(bytesRead >= 0)
current += bytesRead;
} while(bytesRead > -1 && buffer.length != current);
fileOut.write(buffer, 0, current);
fileOut.flush();
System.out.println("\n\033[1;32m[成功] >>\033[0m 文件传输成功");
fileIn.close();
fileOut.close();
fileSocket.close();
respArray[0] = "OK";
response = in.readLine();
respArray = response.split(" ");
}
if(!respArray[0].equals("OK"))
error_handler(response);
}
}
catch (Exception e)
{
System.out.println("\033[1;31m[错误] >>\033[0m "+e);
}
}
} while(!request.equals("quit"));
out.print("QUIT");
out.flush();
response = in.readLine();
if(!response.equals("GOODBYE"))
{
System.out.println("\033[1;31m[错误] >>\033[0m 程序未正常退出: " + response);
System.exit(-1);
}
else
{
System.out.println("\033[1;32m[成功] >>\033[0m 成功关闭连接");
}
in.close();
out.close();
socket.close();
}
catch (Exception e)
{
System.out.println("\033[1;31m[错误] >>\033[0m "+e);
}
}
}