-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmid_1_server.cpp
More file actions
268 lines (195 loc) · 5.86 KB
/
mid_1_server.cpp
File metadata and controls
268 lines (195 loc) · 5.86 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
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
/*
* NCYU 109 Network Programming mid 1 server
* Created by linwebs on 2021/4/19.
*/
#include <iostream>
#include <chrono>
#include <cctype>
#include <cstring>
#include <ctime>
#include <winsock.h>
#define MAXLINE 1024
using namespace std;
/*
* 執行內容
* int status => 執行狀態
* int recv_head_len => 接收到的標頭長度
* int recv_content_len => 接收到的內容長度
* string page => 取得的頁面
* string recv_head => 接收到的標頭長度
* string recv_content => 接收到的內容長度
*/
struct content {
int status = -1;
int recv_head_len = 0;
int recv_content_len = 0;
string page;
string recv_head;
string recv_content;
};
content *get_page(const string &page);
int main() {
SOCKET serv_sd;
SOCKET cli_sd;
SOCKET http_sd;
int cli_len;
char str[MAXLINE];
char str_r[MAXLINE];
struct sockaddr_in serv{};
struct sockaddr_in cli{};
struct sockaddr_in http{};
WSADATA wsadata;
// server's ip address
const char server_ip[16] = "127.0.0.1";
// server's port number
u_short server_port = 1234;
// receive bytes
int rec_len;
// send bytes
int send_len;
// bind status
int bind_status;
// Call WSAStartup() to Register "WinSock DLL"
WSAStartup(0x101, (LPWSADATA) &wsadata);
// Open a TCP socket
serv_sd = socket(AF_INET, SOCK_STREAM, 0);
// Prepare for connect.
// Include sockaddr_ing struct (serv)
serv.sin_family = AF_INET;
// server's ip address
serv.sin_addr.s_addr = inet_addr(server_ip);
// server's port number
// htons: host to network
serv.sin_port = htons(server_port);
// bind
bind_status = bind(serv_sd, (LPSOCKADDR) &serv, sizeof(serv));
if (bind_status == SOCKET_ERROR) {
cout << "bind function failed with error: " << WSAGetLastError() << endl;
closesocket(serv_sd);
WSACleanup();
return 1;
}
// call listen() function to let socket enter listen mode
listen(serv_sd, 5);
cli_len = sizeof(cli);
// accept connect
cout << "Server is waiting for client." << endl;
cli_sd = accept(serv_sd, (LPSOCKADDR) &cli, &cli_len);
if (cli_sd == SOCKET_ERROR) {
cout << "can't accept: " << WSAGetLastError() << endl;
}
while (true) {
// receive from client
rec_len = recv(cli_sd, str, MAXLINE, 0);
cout << "Recv:" << str << endl;
//cout << "server recv: " << str << "(" << rec_len << " bytes)" << endl;
if (rec_len == SOCKET_ERROR) {
cout << "receive error: " << WSAGetLastError() << endl;
break;
}
// (1) time
if (!strcmp(str, "time")) {
auto time = chrono::system_clock::now();
time_t now_time = std::chrono::system_clock::to_time_t(time);
string result_str = ctime(&now_time);
strcpy(str_r, result_str.c_str());
cout << "Send time to client." << endl;
} else if (rec_len > 8) {
string recv_str = str;
//cout << "rec_len" << rec_len << endl;
if (recv_str.substr(0, 6) == "ask://") {
// (2) ask
recv_str = recv_str.substr(6);
strcpy(str_r, recv_str.c_str());
struct in_addr sAddr{};
LPHOSTENT hp;
sAddr.s_addr = inet_addr(recv_str.c_str());
hp = gethostbyaddr((LPSTR) &sAddr, sizeof(sAddr), AF_INET);
if (hp == nullptr) {
// other
cout << "get hp error, code:" << WSAGetLastError() << endl;
strcpy(str_r, "");
cout << "Send nothing." << endl;
} else {
//printf("host name:%s\n", hp->h_name);
//printf("host nickname:%s\n", hp->h_aliases[0]);
// printf("host IP:%s\n\n", inet_ntoa(*(LPIN_ADDR) (hp->h_addr)));
strcpy(str_r, (hp->h_name));
cout << "Send domain name to client." << endl;
}
} else if (recv_str.substr(0, 7) == "http://") {
// (3) http
recv_str = recv_str.substr(7);
strcpy(str_r, recv_str.c_str());
// Open a TCP socket
http_sd = socket(AF_INET, SOCK_STREAM, 0);
// Prepare for connect.
// Include sockaddr_ing struct (serv)
http.sin_family = AF_INET;
// server's ip address
http.sin_addr.s_addr = inet_addr(recv_str.c_str());
// server's port number
// htons: host to network
http.sin_port = htons(80);
int conn_status = connect(http_sd, (LPSOCKADDR) &http, sizeof(http));
if (conn_status == SOCKET_ERROR) {
cout << "connect error: " << WSAGetLastError() << endl;
closesocket(http_sd);
WSACleanup();
break;
}
string request = "GET / HTTP/1.1\r\nHost: " + recv_str + "\r\nConnection: close\r\n\r\n";
int send_status = send(http_sd, request.c_str(), int(request.size()) + 1, 0);
if (send_status == SOCKET_ERROR) {
cout << "send error: " << WSAGetLastError() << endl;
break;
}
char str_http[MAXLINE] = "";
int rec_http_len = recv(http_sd, str_http, MAXLINE, 0);
if (rec_http_len == SOCKET_ERROR) {
cout << "receive error: " << WSAGetLastError() << endl;
break;
}
strcpy(str_r, str_http);
cout << "Send web page to client." << endl;
} else {
// other
strcpy(str_r, "");
cout << "Send nothing." << endl;
}
} else if (!strcmp(str, "end")) {
// (4) end
strcpy(str_r, "Test completes.");
cout << "Complete service for the client." << endl;
} else {
// other
strcpy(str_r, "");
cout << "Send nothing." << endl;
}
// send from server
send_len = send(cli_sd, str_r, int(strlen(str_r) + 1), 0);
if (send_len == SOCKET_ERROR) {
cout << "send error: " << WSAGetLastError() << endl;
break;
}
//cout << "server reply: " << str_r << "(" << send_len << " bytes)" << endl;
if (!strcmp(str, "end")) {
closesocket(cli_sd);
// call listen() function to let socket enter listen mode
listen(serv_sd, 5);
cli_len = sizeof(cli);
// accept connect
cout << endl << "Server is waiting for client." << endl;
cli_sd = accept(serv_sd, (LPSOCKADDR) &cli, &cli_len);
if (cli_sd == SOCKET_ERROR) {
cout << "can't accept: " << WSAGetLastError() << endl;
}
}
}
// close TCP socket
closesocket(serv_sd);
// finish "WinSock DLL"
WSACleanup();
//system("pause");
return 0;
}