gpt4 book ai didi

c++ - 为什么 boost::asio 似乎什么都不做

转载 作者:行者123 更新时间:2023-11-28 08:28:21 25 4
gpt4 key购买 nike

我正在尝试以非常基本的方式实现 irc 协议(protocol)。我的第一次尝试是使用 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 地址,然后程序不会终止。

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

最佳答案

如果您正在进行异步调用,则必须向 io_service 发送请求。在您的情况下,在 "x.connect();" 之后,您应该调用 "ios.run();"

请记住,如果您在异步回调返回后不进行任何进一步的发布,run 将退出/返回。

顺便说一句,在 "ios.dispatch(boost::bind(onM, data));" 中,onM 意味着 onMe 吗?

关于c++ - 为什么 boost::asio 似乎什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3173630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com