boost socket 用法

boost socket 用法
要用得明白,必须还要了解endpoint,它用来设置ip,端口,协议类型.


1.asio::ip::tcp::acceptor  侦听类socket用法
用法示例:

 

boost::asio::io_service io_service_;
boost::asio::ip::tcp::acceptor acc(io_service_);
//这步创建了一个有效的socket.
acc.open(boost::asio::ip::tcp::v4());
boost::asio::ip::tcp::endpoint endpoint_(boost::asio::ip::tcp::v4(),20001);
//这步绑定了要侦听的端口
acc.bind(endpoint_);
//开始侦听
acc.listen();

 

2. asio::ip::tcp::socket  连接类socket用法
用法示例:

 

boost::asio::ip::tcp::socket sock_(io_service_);
sock_.open(boost::asio::ip::tcp::v4());
boost::asio::ip::tcp::endpoint endpoint_1(boost::asio::ip::tcp::v4(),20001);
boost::asio::ip::address addr = boost::asio::ip::address::from_string("127.0.0.1");
endpoint_1.address(addr);
boost::system::error_code err;
sock_.connect(endpoint_1, err);

 

3. asio::ip::tcp::socket  被接受连接类socket用法
用法示例:


boost::asio::ip::tcp::socket sock_(io_service_);
acc.accept(sock_);

by 老洪@2011-3-30 23:58

你可能感兴趣的:(boost socket 用法)