This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFUP.cs
533 lines (439 loc) · 13.3 KB
/
FUP.cs
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
namespace FUP
{
public class CONSTANTS
{
public const uint REQ_FILE_SEND = 0x01;
public const uint REP_FILE_SEND = 0x02;
public const uint FILE_SEND_DATA = 0x03;
public const uint FILE_SEND_RES = 0x04;
public const byte NOT_FRAGMENTED = 0x00;
public const byte FRAGMENTED = 0x01;
public const byte NOT_LASTMSG = 0x00;
public const byte LASTMSG = 0x01;
public const byte ACCEPTED = 0x00;
public const byte DENIED = 0x01;
public const byte FAIL = 0x00;
public const byte SUCCESS = 0x01;
}
public interface ISerializable
{
byte[] GetBytes();
int GetSize();
}
public class Message : ISerializable
{
public Header Header { get; set; }
public ISerializable Body { get; set; }
public byte[] GetBytes()
{
byte[] bytes = new byte[GetSize()];
Header.GetBytes().CopyTo(bytes, 0);
Body.GetBytes().CopyTo(bytes, Header.GetSize());
return bytes;
}
public int GetSize()
{
return Header.GetSize() + Body.GetSize();
}
}
public class Header : ISerializable
{
public uint MSGID { get; set; }
public uint MSGTYPE { get; set; }
public uint BODYLEN { get; set; }
public byte FRAGMENTED { get; set; }
public byte LASTMSG { get; set; }
public ushort SEQ { get; set; }
public Header() { }
public Header(byte[] bytes)
{
MSGID = System.BitConverter.ToUInt32(bytes, 0);
MSGTYPE = System.BitConverter.ToUInt32(bytes, 4);
BODYLEN = System.BitConverter.ToUInt32(bytes, 8);
FRAGMENTED = bytes[12];
LASTMSG = bytes[13];
SEQ = System.BitConverter.ToUInt16(bytes, 14);
}
public byte[] GetBytes()
{
byte[] bytes = new byte[16];
byte[] temp = System.BitConverter.GetBytes(MSGID);
System.Array.Copy(temp, 0, bytes, 0, temp.Length);
temp = System.BitConverter.GetBytes(MSGTYPE);
System.Array.Copy(temp, 0, bytes, 4, temp.Length);
temp = System.BitConverter.GetBytes(BODYLEN);
System.Array.Copy(temp, 0, bytes, 8, temp.Length);
bytes[12] = FRAGMENTED;
bytes[13] = LASTMSG;
temp = System.BitConverter.GetBytes(SEQ);
System.Array.Copy(temp, 0, bytes, 14, temp.Length);
return bytes;
}
public int GetSize()
{
return 16;
}
}
public class BodyRequest : ISerializable
{
public long FILESIZE;
public byte[] FILENAME;
public BodyRequest() { }
public BodyRequest(byte[] bytes)
{
FILESIZE = System.BitConverter.ToInt64(bytes, 0);
FILENAME = new byte[bytes.Length - sizeof(long)];
System.Array.Copy(bytes, sizeof(long), FILENAME, 0, FILENAME.Length);
}
public byte[] GetBytes()
{
byte[] bytes = new byte[GetSize()];
byte[] temp = System.BitConverter.GetBytes(FILESIZE);
System.Array.Copy(temp, 0, bytes, 0, temp.Length);
System.Array.Copy(FILENAME, 0, bytes, temp.Length, FILENAME.Length);
return bytes;
}
public int GetSize()
{
return sizeof(long) + FILENAME.Length;
}
}
public class BodyResponse : ISerializable
{
public uint MSGID;
public byte RESPONSE;
public BodyResponse() { }
public BodyResponse(byte[] bytes)
{
MSGID = System.BitConverter.ToUInt32(bytes, 0);
RESPONSE = bytes[4];
}
public byte[] GetBytes()
{
byte[] bytes = new byte[GetSize()];
byte[] temp = System.BitConverter.GetBytes(MSGID);
System.Array.Copy(temp, 0, bytes, 0, temp.Length);
bytes[temp.Length] = RESPONSE;
return bytes;
}
public int GetSize()
{
return sizeof(uint) + sizeof(byte);
}
}
public class BodyData : ISerializable
{
public byte[] DATA;
public BodyData(byte[] bytes)
{
DATA = new byte[bytes.Length];
bytes.CopyTo(DATA, 0);
}
public byte[] GetBytes()
{
return DATA;
}
public int GetSize()
{
return DATA.Length;
}
}
public class BodyResult : ISerializable
{
public uint MSGID;
public byte RESULT;
public BodyResult() { }
public BodyResult(byte[] bytes)
{
MSGID = System.BitConverter.ToUInt32(bytes, 0);
RESULT = bytes[4];
}
public byte[] GetBytes()
{
byte[] bytes = new byte[GetSize()];
byte[] temp = System.BitConverter.GetBytes(MSGID);
System.Array.Copy(temp, 0, bytes, 0, temp.Length);
bytes[temp.Length] = RESULT;
return bytes;
}
public int GetSize()
{
return sizeof(uint) + sizeof(byte);
}
}
public class MessageUtil
{
public static void Send(System.IO.Stream writer, Message msg)
{
writer.Write(msg.GetBytes(), 0, msg.GetSize());
}
public static Message Receive(System.IO.Stream reader)
{
int totalRecv = 0;
int sizeToRead = 16;
byte[] hBuffer = new byte[sizeToRead];
while (sizeToRead > 0)
{
byte[] buffer = new byte[sizeToRead];
int recv = reader.Read(buffer, 0, sizeToRead);
if (recv == 0)
return null;
buffer.CopyTo(hBuffer, totalRecv);
totalRecv += recv;
sizeToRead -= recv;
}
Header header = new Header(hBuffer);
totalRecv = 0;
byte[] bBuffer = new byte[header.BODYLEN];
sizeToRead = (int)header.BODYLEN;
while (sizeToRead > 0)
{
byte[] buffer = new byte[sizeToRead];
int recv = reader.Read(buffer, 0, sizeToRead);
if (recv == 0)
return null;
buffer.CopyTo(bBuffer, totalRecv);
totalRecv += recv;
sizeToRead -= recv;
}
ISerializable body = null;
switch (header.MSGTYPE)
{
case CONSTANTS.REQ_FILE_SEND:
body = new BodyRequest(bBuffer);
break;
case CONSTANTS.REP_FILE_SEND:
body = new BodyResponse(bBuffer);
break;
case CONSTANTS.FILE_SEND_DATA:
body = new BodyData(bBuffer);
break;
case CONSTANTS.FILE_SEND_RES:
body = new BodyResult(bBuffer);
break;
default:
throw new System.Exception(System.String.Format("Unknown MSGTYPE : {0}" + header.MSGTYPE));
}
return new Message() { Header = header, Body = body };
}
}
}
namespace FUPApp
{
class MainApp
{
static void Main(string[] args)
{
const int PORT = 8810;
if (args.Length == 1)
{
uint msgId = 0;
string dir = args[0];
System.Net.Sockets.TcpListener server = null;
try
{
System.Net.IPEndPoint localAddress = new System.Net.IPEndPoint(0, PORT);
server = new System.Net.Sockets.TcpListener(localAddress);
server.Start();
System.Console.WriteLine("파일 다운로드 대기 중...");
while (true)
{
System.Net.Sockets.TcpClient client = server.AcceptTcpClient();
System.Console.WriteLine("{0} 연결", ((System.Net.IPEndPoint)client.Client.RemoteEndPoint).ToString());
System.Net.Sockets.NetworkStream stream = client.GetStream();
FUP.Message reqMsg = FUP.MessageUtil.Receive(stream);
if (reqMsg.Header.MSGTYPE != FUP.CONSTANTS.REQ_FILE_SEND)
{
stream.Close();
client.Close();
continue;
}
FUP.BodyRequest reqBody = (FUP.BodyRequest)reqMsg.Body;
System.Console.WriteLine("파일 전송 요청이 왔습니다. 수락하시겠습니까? (yes/no)");
string answer = System.Console.ReadLine();
FUP.Message rspMsg = new FUP.Message();
rspMsg.Body = new FUP.BodyResponse() {
MSGID = reqMsg.Header.MSGID,
RESPONSE = FUP.CONSTANTS.ACCEPTED
};
rspMsg.Header = new FUP.Header() {
MSGID = msgId++,
MSGTYPE = FUP.CONSTANTS.REP_FILE_SEND,
BODYLEN = (uint)rspMsg.Body.GetSize(),
FRAGMENTED = FUP.CONSTANTS.NOT_FRAGMENTED,
LASTMSG = FUP.CONSTANTS.LASTMSG,
SEQ = 0
};
if (answer != "yes" && answer != "y")
{
rspMsg.Body = new FUP.BodyResponse() {
MSGID = reqMsg.Header.MSGID,
RESPONSE = FUP.CONSTANTS.DENIED
};
FUP.MessageUtil.Send(stream, rspMsg);
stream.Close();
client.Close();
continue;
}
else
FUP.MessageUtil.Send(stream, rspMsg);
System.Console.WriteLine("파일 전송을 시작합니다.");
if (System.IO.Directory.Exists(dir) == false)
System.IO.Directory.CreateDirectory(dir);
long fileSize = reqBody.FILESIZE;
string fileName = System.Text.Encoding.Default.GetString(reqBody.FILENAME);
System.IO.FileStream file = new System.IO.FileStream(dir + "\\" + fileName, System.IO.FileMode.Create);
uint? dataMsgId = null;
ushort prevSeq = 0;
while ((reqMsg = FUP.MessageUtil.Receive(stream)) != null)
{
System.Console.Write("#");
if (reqMsg.Header.MSGTYPE != FUP.CONSTANTS.FILE_SEND_DATA)
break;
if (dataMsgId == null)
dataMsgId = reqMsg.Header.MSGID;
else
{
if (dataMsgId != reqMsg.Header.MSGID)
break;
}
if (prevSeq++ != reqMsg.Header.SEQ)
{
System.Console.WriteLine("{0}, {1}", prevSeq, reqMsg.Header.SEQ);
break;
}
file.Write(reqMsg.Body.GetBytes(), 0, reqMsg.Body.GetSize());
if (reqMsg.Header.FRAGMENTED == FUP.CONSTANTS.NOT_FRAGMENTED)
break;
if (reqMsg.Header.LASTMSG == FUP.CONSTANTS.LASTMSG)
break;
}
long recvFileSize = file.Length;
file.Close();
System.Console.WriteLine();
System.Console.WriteLine("수신 파일 크기 : {0}byte", recvFileSize);
FUP.Message rstMsg = new FUP.Message();
rstMsg.Body = new FUP.BodyResult() {
MSGID = reqMsg.Header.MSGID,
RESULT = FUP.CONSTANTS.SUCCESS
};
rstMsg.Header = new FUP.Header() {
MSGID = msgId++,
MSGTYPE = FUP.CONSTANTS.FILE_SEND_RES,
BODYLEN = (uint)rstMsg.Body.GetSize(),
FRAGMENTED = FUP.CONSTANTS.NOT_FRAGMENTED,
LASTMSG = FUP.CONSTANTS.LASTMSG,
SEQ = 0
};
if (fileSize == recvFileSize)
FUP.MessageUtil.Send(stream, rstMsg);
else
{
rstMsg.Body = new FUP.BodyResult() {
MSGID = reqMsg.Header.MSGID,
RESULT = FUP.CONSTANTS.FAIL
};
FUP.MessageUtil.Send(stream, rstMsg);
}
System.Console.WriteLine("파일 전송을 마쳤습니다.");
stream.Close();
client.Close();
}
}
catch (System.Net.Sockets.SocketException e)
{
System.Console.WriteLine(e);
}
finally
{
server.Stop();
}
}
else if (args.Length == 2)
{
const int CHUNK_SIZE = 4096;
string serverIp = args[0];
string filepath = args[1];
try
{
System.Net.IPEndPoint clientAddress = new System.Net.IPEndPoint(0, 0);
System.Net.IPEndPoint serverAddress = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(serverIp), PORT);
System.IO.FileInfo fileinfo = new System.IO.FileInfo(filepath);
System.Console.WriteLine("클라이언트: {0}, 서버: {0}", clientAddress.ToString(), serverAddress.ToString());
uint msgId = 0;
FUP.Message reqMsg = new FUP.Message();
reqMsg.Body = new FUP.BodyRequest() {
FILESIZE = fileinfo.Length,
FILENAME = System.Text.Encoding.Default.GetBytes(fileinfo.Name)
};
reqMsg.Header = new FUP.Header() {
MSGID = msgId++,
MSGTYPE = FUP.CONSTANTS.REQ_FILE_SEND,
BODYLEN = (uint)reqMsg.Body.GetSize(),
FRAGMENTED = FUP.CONSTANTS.NOT_FRAGMENTED,
LASTMSG = FUP.CONSTANTS.LASTMSG,
SEQ = 0
};
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(clientAddress);
client.Connect(serverAddress);
System.Net.Sockets.NetworkStream stream = client.GetStream();
FUP.MessageUtil.Send(stream, reqMsg);
FUP.Message rspMsg = FUP.MessageUtil.Receive(stream);
if (rspMsg.Header.MSGTYPE != FUP.CONSTANTS.REP_FILE_SEND)
{
System.Console.WriteLine("정상적인 서버 응답이 아닙니다. {0}", rspMsg.Header.MSGTYPE);
return;
}
if (((FUP.BodyResponse)rspMsg.Body).RESPONSE == FUP.CONSTANTS.DENIED)
{
System.Console.WriteLine("서버에서 파일 전송을 거부했습니다.");
return;
}
using (System.IO.Stream fileStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open))
{
byte[] rbytes = new byte[CHUNK_SIZE];
long readValue = System.BitConverter.ToInt64(rbytes, 0);
int totalRead = 0;
ushort msgSeq = 0;
byte fragmented = (fileStream.Length < CHUNK_SIZE) ? FUP.CONSTANTS.NOT_FRAGMENTED : FUP.CONSTANTS.FRAGMENTED;
while (totalRead < fileStream.Length)
{
int read = fileStream.Read(rbytes, 0, CHUNK_SIZE);
totalRead += read;
FUP.Message fileMsg = new FUP.Message();
byte[] sendBytes = new byte[read];
System.Array.Copy(rbytes, 0, sendBytes, 0, read);
fileMsg.Body = new FUP.BodyData(sendBytes);
fileMsg.Header = new FUP.Header() {
MSGID = msgId,
MSGTYPE = FUP.CONSTANTS.FILE_SEND_DATA,
BODYLEN = (uint)fileMsg.Body.GetSize(),
FRAGMENTED = fragmented,
LASTMSG = (totalRead < fileStream.Length) ? FUP.CONSTANTS.NOT_LASTMSG : FUP.CONSTANTS.LASTMSG,
SEQ = msgSeq++
};
System.Console.Write("#");
FUP.MessageUtil.Send(stream, fileMsg);
}
System.Console.WriteLine();
FUP.Message rstMsg = FUP.MessageUtil.Receive(stream);
FUP.BodyResult result = (FUP.BodyResult)rstMsg.Body;
System.Console.WriteLine("파일 전송을 마쳤습니다.");
}
stream.Close();
client.Close();
}
catch (System.Net.Sockets.SocketException e)
{
System.Console.WriteLine(e);
}
}
else
{
System.Console.WriteLine("파일 받는 법:\t{0} \"다운로드 경로\"", System.Diagnostics.Process.GetCurrentProcess().ProcessName);
System.Console.WriteLine("파일 보내는 법:\t{0} \"보낼 IP\" \"업로드할 파일\"", System.Diagnostics.Process.GetCurrentProcess().ProcessName);
}
return;
}
}
}