#include "boost/asio.hpp" #include "boost/bind.hpp" #include "boost/thread.hpp" #include #include using namespace std; boost::asio::io_service ios_; boost::asio::ip::tcp::endpoint endpoint_; boost::asio::ip::tcp::socket socket_(ios_); void HandleConnect(const boost::system::error_code& err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); void HandleWriteRequest(const boost::system::error_code& err); void WriteRequest(); bool is_connect = false; string request = "GET /index.html\r\nHosts: 129.79.245.252\r\nPort: 80\r\n\r\n"; void HandleConnect(const boost::system::error_code& err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator) { if (!err) { is_connect = true; WriteRequest(); } else { cout << "Connect Error : " << err.message().c_str() << endl; } } void WriteRequest() { boost::asio::async_write(socket_, boost::asio::buffer(request), boost::bind( &HandleWriteRequest, boost::asio::placeholders::error)); } void HandleWriteRequest(const boost::system::error_code& err) { if (!err) { } else { cout << "WriteRequest error" << endl; } } void main(int argc, char* argv[]) { boost::asio::io_service::work * m_work = new boost::asio::io_service::work(ios_); new boost::thread(boost::bind(&boost::asio::io_service::run, &ios_)); while(1) { boost::system::error_code err; boost::asio::ip::address_v4 addr = boost::asio::ip::address_v4::from_string("129.79.245.252", err); boost::asio::ip::tcp::resolver::iterator endpoint_iter; endpoint_ = boost::asio::ip::tcp::endpoint(addr, 80); socket_.async_connect(endpoint_, boost::bind(HandleConnect, boost::asio::placeholders::error, endpoint_iter)); Sleep(500); socket_.close(); is_connect = false; } }