为什么boost :: asio似乎什么都不做

时间:2010-07-04 03:25:06

标签: c++ boost-asio irc

我正在尝试以非常基本的方式实现irc协议。我的第一次尝试是使用boost :: asio并连接到服务器并阅读motd。 AFAIK将motd连接到每个客户端。我做了以下测试程序,似乎没有做任何事情。这不是一段非常好的代码,但这是我在几个令人沮丧的时间之后所拥有的全部内容。

#define _WIN32_WINNT 0x0501
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <queue>



class IrcConnection
{
public:
    IrcConnection(const std::string& server, int port, boost::function<void (const std::string&)> onMessage, boost::asio::io_service& io_service): s(server), p(port), onM(onMessage), ios(io_service), socket(io_service)
    {
    }
    void connect() 
    {
        somethingHappened = false;
        //DNS stuff
        boost::asio::ip::tcp::resolver resolver(ios);
        boost::asio::ip::tcp::resolver::query query(s , "0");
        boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query);
        boost::asio::ip::tcp::resolver::iterator end;

        boost::system::error_code error;

        while(iter != end)
        {
            boost::asio::ip::tcp::endpoint endpoint = iter->endpoint();
            endpoint.port(p);
            socket.close();
            socket.connect(endpoint, error);
            iter++;
        }
        if(error)
        {
            std::cout << "ERROR" << std::endl;
            std::cout << error << std::endl;
        }

        std::cout << "Connected to: " << socket.remote_endpoint().address().to_string() << std::endl; 

        //read from the socket until a space is found
        boost::asio::async_read_until(socket, currentData, ' ', boost::bind(&IrcConnection::onReceiveFinished, this, boost::asio::placeholders::error));
    }
    void disconnect() 
    {
        socket.close();
    }
    void send(int priority, const std::string& message) 
    {
    }
bool somethingHappened;

private:
    std::string s;
    int p;
    boost::function<void (const std::string&)> onM;
    boost::asio::io_service& ios;
    boost::asio::ip::tcp::socket socket;
    std::priority_queue<std::string> outQueue;
    boost::asio::streambuf currentData;


    void onReceiveFinished(const boost::system::error_code& error)
    {
        if(error)
        {
            disconnect();
            std::cout << "ERRORRRR" << std::endl;
            std::cout << error << std::endl;
        }

        std::cout << "STUFF IS OCCURING" << std::endl;
        somethingHappened = true;

         std::istream stream(&currentData);
         std::string data;
         std::getline(stream, data);

         boost::asio::async_read_until(socket, currentData, ' ', boost::bind(&IrcConnection::onReceiveFinished, this, boost::asio::placeholders::error));
         ios.dispatch(boost::bind(onM, data));
    }


};


void onMe(const std::string& x)
{
    std::cout << "MESSAGE" << std::endl;
    std::cout << x << std::endl;
}

int main()
{
    boost::asio::io_service ios;
    std::string server = "irc.efnet.org";
    int port = 6667;

    IrcConnection x(server, port, onMe, ios);

    x.connect();

    while(!x.somethingHappened)
    {
        //
    }

    return 0;

}

程序尝试连接到irc.efnet.org并读取,直到发送一个空格,然后将该数据吐出到终端。但是我运行这个,所有打印的是我连接的IP地址,然后程序不会终止。

我需要做些什么才能获得缩进行为?

2 个答案:

答案 0 :(得分:2)

如果您正在进行异步呼叫,则必须将请求发布到io_service。在您"x.connect();"后的情况下,您应该致电"ios.run();"

请记住,如果在异步回调返回后您没有再发帖,则运行将退出/返回。

btw "ios.dispatch(boost::bind(onM, data));"是onM意味着在我身上?

答案 1 :(得分:-1)

您正在混合异步(io_service::postasync_read_until)和同步(ip::tcp::socket::connectip::tcp::endpoint::resolve)操作。这可以工作,但我真的不建议这种方法。坚持异步操作,使用ip::tcp::socket::async_connect而不是ip::tcp::socket::connectip::tcp::socket::async_resolve而不是ip::tcp::socket::resolve。您需要调用io_service::run或同等对象,这就是您的代码connect之后无效的原因。 Boost.Asio文档有numerous examples,展示了如何实现这一目标。