-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server.cpp
More file actions
57 lines (46 loc) · 1.44 KB
/
test_server.cpp
File metadata and controls
57 lines (46 loc) · 1.44 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
#include <iostream>
#include <netinet/in.h>
using namespace std;
#include "tcp_listener.h"
int main() {
TcpListener listener;
listener.listen(8888);
while (true) {
TcpConnection conn = listener.accept();
string msg;
if ((msg = conn.blocking_receive_line()).size() > 0) {
cout << "server received: " << msg;
conn.blocking_send(msg);
}
// decode message struct
char buf[128];
uint32_t f1_length, f2_length;
int rc;
rc = conn.blocking_receive_n((char *) &f1_length, sizeof(f1_length));
if (rc != sizeof(f1_length)) {
cerr << "receive f1_length error\n";
continue;
}
f1_length = ntohl(f1_length);
rc = conn.blocking_receive_n((char *) &f2_length, sizeof(f2_length));
if (rc != sizeof(f2_length)) {
cerr << "receive f2_length error\n";
continue;
}
f2_length = ntohl(f2_length);
rc = conn.blocking_receive_n(buf, f1_length);
if (rc != f1_length) {
cerr << "receive field1 error\n";
continue;
}
string f1(buf);
cout << "server received: " << f1 << endl;
rc = conn.blocking_receive_n(buf, f2_length);
if (rc != f2_length) {
cerr << "receive field2 error\n";
continue;
}
string f2(buf);
cout << "server received: " << f2 << endl;
}
}