-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathblock_echo_server.cpp
70 lines (63 loc) · 2.17 KB
/
block_echo_server.cpp
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
/*
* Copyright (c) 2022, Alibaba Group Holding Limited;
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdlib>
#include <iostream>
#include "asio_util.hpp"
using asio::ip::tcp;
void session(tcp::socket sock) {
int msg_index = 0;
for (;;) {
const size_t max_length = 1024;
char data[max_length];
auto [error, length] = read_some(sock, asio::buffer(data, max_length));
msg_index++;
if (error == asio::error::eof) {
std::cout << "Remote client closed at message index: "
<< msg_index - 1 << ".\n";
break;
} else if (error) {
std::cout << error.message() << '\n';
throw asio::system_error(error);
}
write(sock, asio::buffer(data, length));
}
std::cout << "Finished echo message, total: " << msg_index - 1 << ".\n";
}
void start_server(asio::io_context& io_context, unsigned short port) {
tcp::acceptor a(io_context, tcp::endpoint(tcp::v4(), port));
std::cout << "Listen port " << port << " successfully.\n";
for (;;) {
auto [error, socket] = accept(a);
if (error) {
std::cout << "Accept failed, error: " << error.message() << '\n';
continue;
}
std::cout << "New client coming.\n";
session(std::move(socket));
std::error_code ec;
socket.shutdown(asio::ip::tcp::socket::shutdown_both, ec);
socket.close(ec);
}
}
int main(int argc, char* argv[]) {
try {
asio::io_context io_context;
start_server(io_context, 9980);
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}