-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.cpp
More file actions
95 lines (72 loc) · 1.77 KB
/
Copy pathserver.cpp
File metadata and controls
95 lines (72 loc) · 1.77 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
#include "UdpServerSocket.h"
#include "TcpServerSocket.h"
#define UDP_SERV_PORT 7718
#define TCP_SERV_PORT 7799
char data_list[][1024] = { {"1. train data\n"},
{"2. test data\n"},
{"3. README\n"} };
char data_path[][1024] = { {"data/train"},
{"data/test"},
{"data/README.txt"} };
int main()
{
char* message;
char buf[BUFSIZE];
UdpServerSocket udp(UDP_SERV_PORT);
TcpServerSocket tcp(TCP_SERV_PORT);
udp.createSocket();
udp.bindSocket();
tcp.createSocket();
tcp.bindSocket();
tcp.listenSocket();
while(true)
{
udp.clearFileList();
tcp.clearFileList();
printf("\nWaiting...\n");
tcp.acceptSocket();
message = udp.receiveMessage();
printf("%s\n", message);
// send data list
strcpy(buf, data_list[0]);
for(int i=1; i<=2; i++)
strcat(buf, data_list[i]);
udp.sendMessage(buf);
// receive select number
strcpy(buf, udp.receiveMessage());
int select = atoi(buf);
// data path
strcpy(buf, data_path[select-1]);
udp.searchFiles(buf);
// send file
vector< pair<int, pair< string, string> > > file_list = udp.getFileList();
int size = file_list.size();
// send file list size
sprintf(buf, "%d", size);
udp.sendMessage(buf);
for(int i=0; i<size; i++)
{
int file_size = file_list[i].first;
char* file_name = (char*)file_list[i].second.first.c_str();
char* send_file_name = (char*)file_list[i].second.second.c_str();
// send file size
sprintf(buf, "%d", file_size);
udp.sendMessage(buf);
if(file_size <= 1024*64)
{
// send save file name
strcpy(buf, send_file_name);
udp.sendMessage(buf);
udp.sendFile(file_name);
}
else
{
// send save file name
strcpy(buf, send_file_name);
tcp.sendMessage(buf);
tcp.sendFile(file_name);
}
}
}
return 0;
}