-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathasync_server2.cpp
More file actions
63 lines (43 loc) · 1.44 KB
/
async_server2.cpp
File metadata and controls
63 lines (43 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
58
59
60
61
62
#include <iostream>
#include <boost/asio.hpp>
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/strand.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
//using namespace boost::asio
//using ip::tcp
using std::string;
using std::cout;
using std::endl;
int main()
{
int port = 1233;
boost::asio::io_service io_service_;
boost::asio::ip::tcp::acceptor acceptor_(io_service_,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
char packet_type[4];
boost::asio::ip::tcp::socket socket_(io_service_);
std::function<void(boost::system::error_code)> fun2;
std::function<void(boost::asio::ip::tcp::socket)> asyn_acc;
fun2 = [&](boost::system::error_code ec){};
asyn_acc = [&,&asyn_acc](boost::asio::ip::tcp::socket socket_)
{
acceptor_.async_accept(socket_, [&](boost::system::error_code ec){
boost::asio::async_read(socket_, boost::asio::buffer(packet_type, 4),
[&](boost::system::error_code ec, std::size_t N)
{
std::cout << "Received: '";
std::cout.write(packet_type, sizeof(packet_type)) << "'\n";
write(socket_, boost::asio::buffer(packet_type),ec);
});
});
//boost::asio::ip::tcp::socket socket(io_service_);
//asyn_acc(std::move(socket));
};
asyn_acc(std::move(socket_));
//auto fun2 = [&](boost::system::error_code ec){};
io_service_.run();
return 0;
}