boost: tcp client sample

3128 ワード

#include <boost/asio.hpp>
#include <iostream>

using namespace std;
using namespace boost::asio;

void client(io_service &ios)
{
    try
    {
        cout << "client start." << endl;

        ip::tcp::socket sock(ios);
        ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"),6688);

        sock.connect(ep);

        vector<char> str(100,0);
        sock.read_some(buffer(str));
        cout << "receive from " << sock.remote_endpoint().address();
        cout << &str[0] << endl;
    }
    catch (std::exception& e)
    {
        cout << e.what() << endl;
    }
}

void print(const boost::system::error_code&)
{
    cout << "test wait..." << endl;
}

int main()
{
    io_service ios;
    deadline_timer at(ios, boost::posix_time::seconds(5));
    at.async_wait(print);

    cout << "it show before at exired" <<endl;
    ios.run();
    return 0;
}