#include <ctime>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std;
time_t now = time(0);
return ctime(&now);
}
/*
从enable_shared_from_this 派生的类,
成员函数shared_from_this返回shared_ptr
tcp_connection类管理新连接的socket,连接后发送时间字符串至客户端
*/
class tcp_connection
: public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service)
{
return pointer(new tcp_connection(io_service));
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
message_ = make_daytime_string();
/*
*向新连接socket异步写数据
*注意bind的用法(可以查看有关资料),这里是绑定类成员函数
*第一个参数是类成员函数地址,第二个是该类变量指针或智能指针或类变量
*后面是传递给类成员函数的各个参数,有几个就传递几个
*/
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write,shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
private:
tcp_connection(boost::asio::io_service& io_service)
: socket_(io_service)
{
}
void handle_write(const boost::asio::error&/*error*/,
size_t/*bytes_transferfed*/)
{
}
tcp::socket socket_;
std::string message_;
};
class tcp_server
{
public:
tcp_server(boost::asio::io_service& io)
: acceptor_(io, tcp::endpoint(tcp::v4(),13))
{
start_accept();
}
private:
void start_accept()
{
//创建新tcp_connection对象,成员变量socket_将会作为acceptor的参数
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.io_service());
//等待接受新连接
acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept,this,
new_connection,boost::asio::placeholders::error));
}
/*
*当async_accept接受新连接完成后呼叫handle_accept
*我们在这个函数中响应客户请求,并等待下个连接
*/
void handle_accept(tcp_connection::pointer new_connection,
boost::asio::error&error)
{
if(!error)
{
new_connection->start();
start_accept();
}
}
private:
/*
/// TCP socket 类型.
typedef basic_stream_socket<tcp> socket;
/// TCP acceptor 类型.
typedef basic_socket_acceptor<tcp> acceptor;
*/
tcp::acceptor acceptor_;
};
/*
io_service 类为异步i/o对象的用户提供核心 i/o功能(在windows2000以上环境使用完成端口)
如:
boost::asio::ip::tcp::socket
boost::asio::ip::tcp::acceptor
boost::asio::ip::udp::socket
boost::asio::deadline_timer
*/
int _tmain(int argc, _TCHAR* argv[])
{
try{
boost::asio::io_service io;
tcp_server server(io);
io.run();
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
#include <iostream>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std;
time_t now = time(0);
return ctime(&now);
}
/*
从enable_shared_from_this 派生的类,
成员函数shared_from_this返回shared_ptr
tcp_connection类管理新连接的socket,连接后发送时间字符串至客户端
*/
class tcp_connection
: public boost::enable_shared_from_this<tcp_connection>
{
public:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service)
{
return pointer(new tcp_connection(io_service));
}
tcp::socket& socket()
{
return socket_;
}
void start()
{
message_ = make_daytime_string();
/*
*向新连接socket异步写数据
*注意bind的用法(可以查看有关资料),这里是绑定类成员函数
*第一个参数是类成员函数地址,第二个是该类变量指针或智能指针或类变量
*后面是传递给类成员函数的各个参数,有几个就传递几个
*/
boost::asio::async_write(socket_, boost::asio::buffer(message_),
boost::bind(&tcp_connection::handle_write,shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
private:
tcp_connection(boost::asio::io_service& io_service)
: socket_(io_service)
{
}
void handle_write(const boost::asio::error&/*error*/,
size_t/*bytes_transferfed*/)
{
}
tcp::socket socket_;
std::string message_;
};
class tcp_server
{
public:
tcp_server(boost::asio::io_service& io)
: acceptor_(io, tcp::endpoint(tcp::v4(),13))
{
start_accept();
}
private:
void start_accept()
{
//创建新tcp_connection对象,成员变量socket_将会作为acceptor的参数
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.io_service());
//等待接受新连接
acceptor_.async_accept(new_connection->socket(),
boost::bind(&tcp_server::handle_accept,this,
new_connection,boost::asio::placeholders::error));
}
/*
*当async_accept接受新连接完成后呼叫handle_accept
*我们在这个函数中响应客户请求,并等待下个连接
*/
void handle_accept(tcp_connection::pointer new_connection,
boost::asio::error&error)
{
if(!error)
{
new_connection->start();
start_accept();
}
}
private:
/*
/// TCP socket 类型.
typedef basic_stream_socket<tcp> socket;
/// TCP acceptor 类型.
typedef basic_socket_acceptor<tcp> acceptor;
*/
tcp::acceptor acceptor_;
};
/*
io_service 类为异步i/o对象的用户提供核心 i/o功能(在windows2000以上环境使用完成端口)
如:
boost::asio::ip::tcp::socket
boost::asio::ip::tcp::acceptor
boost::asio::ip::udp::socket
boost::asio::deadline_timer
*/
int _tmain(int argc, _TCHAR* argv[])
{
try{
boost::asio::io_service io;
tcp_server server(io);
io.run();
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}