// TimerServer.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _CRT_SECURE_NO_WARNINGS
#include<ctime>
#include <iostream>
#include <string>
#include <functional>
#include <memory>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
//返回时间
static std::string make_daytime_string()
{
using namespace std;
auto now = time(nullptr);
return ctime(&now);
}
//连接类
class tcp_connection :public std::enable_shared_from_this<tcp_connection>
{
public:
using pointer=std::shared_ptr<tcp_connection> ;
static pointer create(boost::asio::io_context& io)
{
return pointer(new tcp_connection(io));
//因为构造函数为private的 所以make_shared不能使用
//return std::make_shared<tcp_connection>(io);
}
tcp::socket& socket() { return socket_; };
void start()
{
message_ = make_daytime_string();
//boost::asio::async_write(
// socket_, boost::asio::buffer(message_),
// std::bind(&tcp_connection::handle_write,shared_from_this(),boost::asio::placeholders::error,
// boost::asio::placeholders::bytes_transferred)
//);
auto self = shared_from_this();
boost::asio::async_write(
socket_, boost::asio::buffer(message_),
[self=std::move(self)](auto err,auto bytes_tran){self->handle_write(err, bytes_tran); }
);
}
private:
//发送失败回调函数
void handle_write(boost::system::error_code & err,size_t ){
printf("%s", err.message().c_str());
}
//创建socket连接
tcp_connection(boost::asio::io_context& io) :socket_(io) {};
tcp::socket socket_;
std::string message_;
};
class tcp_server {
public:
tcp_server(boost::asio::io_context& io):acceptor_(io,tcp::endpoint(tcp::v4(),13))
{
start_accept();
};
private:
void start_accept() {
auto new_connection = tcp_connection::create((boost::asio::io_context&)acceptor_.get_executor().context());
acceptor_.async_accept(new_connection->socket(),
[this, new_connection](auto& err) {
this->handle_accept(new_connection,err);
}
);
}
void handle_accept(tcp_connection::pointer new_connection, const boost::system::error_code& error) {
if (!error)
{
new_connection->start();
}
start_accept();
}
private:
tcp::acceptor acceptor_;
};
//同步
//int main()
//{
// try
// {
// boost::asio::io_context io;
// tcp::acceptor acc(io, tcp::endpoint(tcp::v4(), 13));
// for (;;)
// {
// tcp::socket sock(io);
// acc.accept(sock);
// std::string message = make_daytime_string();
// boost::system::error_code error;
// boost::asio::write(sock, boost::asio::buffer(message), error);
// }
// }
// catch (const std::exception& e)
// {
// std::cerr << e.what() << std::endl;
//
// }
// return 0;
std::cout << "Hello World!\n";
//}
//异步
int main()
{
try
{
boost::asio::io_context io;
tcp_server tcp_(io);
io.run();
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
客户端测试代码
// TimerClient.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
int main(int argc,char* argv[])
{
try {
if (argc != 2)
{
std::cerr << "Usage:client <host>" << std::endl;
return 1;
}
boost::asio::io_service io_service;
//resolver 解析地址
tcp::resolver r(io_service);
tcp::resolver::query q(argv[1], "daytime");
tcp::resolver::iterator endpoint_it = r.resolve(q);
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_it);
for (;;)
{
boost::array<char, 128> buff;
boost::system::error_code error;
size_t len = socket.read_some(boost::asio::buffer(buff), error);
if (error == boost::asio::error::eof)break;
else if (error)
throw boost::system::system_error(error);
std::cout.write(buff.data(), len);
}
}
catch(std::exception &e){
std::cerr << e.what() << std::endl;
}
return 0;
}