main.cpp中的for循环调用使用boost :: mutex的函数,并使用read_until从套接字读取数据,仅运行一次,之后就好像被阻塞了。我试过在继续括号前放置一个continue,然后崩溃。这可能与线程有关。

// MAIN.CPP

int main(int argc, char* argv[])
{
    std::cout << "Enter port number: ";
    std::string port;
    std::getline(std::cin, port);
    int tempPort = std::stoi(port);
    Network * network = new Network(tempPort);

    int it = 0;
    boost::thread * t1;
    t1 = new boost::thread([&network, &it]
    {
        while (true)
        {
            boost::asio::ip::tcp::socket * sock = new boost::asio::ip::tcp::socket(network->io_service);
            network->accept(*sock);

            if (network->socketList.size() > 0)
            {
                for (boost::asio::ip::tcp::socket * s : network->socketList)
                {
                    if (s->remote_endpoint().address().to_string() == sock->remote_endpoint().address().to_string())
                    {
                        continue;
                    }
                    else {
                        network->socketList.push_back(sock);
                        std::cout << s->remote_endpoint().address().to_string() << " connected." << std::endl;
                    }
                }
            }
            else {
                network->socketList.push_back(sock);
                std::cout << sock->remote_endpoint().address().to_string() << " connected." << std::endl;
            }
        }
    });

    while (true)
    {
        for (boost::asio::ip::tcp::socket * sock : network->socketList)
        {
            std::cout << "on range-based for loop" << std::endl;
            network->readChatMessage(*(sock));
        }
    }
    t1->join();

    return 0;
}

// NETWORK.CPP
int Network::sendChatMessage(boost::asio::ip::tcp::socket & socket, ChatMessage & message)
{
    try
    {
        boost::system::error_code err;

        boost::asio::streambuf buf;
        {
            std::ostream out(&buf);
            boost::archive::text_oarchive oa(out);
            oa & message;

            std::cout << std::string(message.text.begin(), message.text.end()) << std::endl;
        }

        m.lock();
        write(socket, buf, err);

        if (err)
        {
            std::cout << err.message() << std::endl;
        }

        m.unlock();

        std::cout << "Mensagem enviada com sucesso!" << std::endl;
    }
    catch (std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

int Network::readChatMessage(boost::asio::ip::tcp::socket & socket)
{
    std::cout << "in readChatMessage()" << std::endl;

    boost::system::error_code err;

    boost::asio::streambuf buf;

    m.lock();
    boost::asio::read_until(socket, buf, '\0', err);

    if (err)
    {
        std::cout << err.message() << std::endl;
    }

    m.unlock();

    std::istream in(&buf);
    ChatMessage message;

    boost::archive::text_iarchive ia(in);
    ia & message;

    std::cout << std::string(message.text.begin(), message.text.end()) << std::endl;
    this->sendChatMessage(socket, message);

    return 0;
}

最佳答案

经过一点调试和编辑代码后,我得以解决该问题,然后我发现发生了一个错误:input stream error由于序列化了输入/输出。我已正确处理错误,在发生错误时将互斥锁解锁,并且不让互斥锁撤消。

片段:

int Network::readChatMessage(boost::asio::ip::tcp::socket & socket)
{
    std::cout << "in readChatMessage()" << std::endl;

    boost::system::error_code err;

    boost::asio::streambuf buf;

    m.lock();
    boost::asio::read_until(socket, buf, '\0', err);

    if (err)
    {
        m.unlock();
        std::cout << err.message() << std::endl;
        return 0;
    }
    else {

        m.unlock();

        std::istream in(&buf);
        ChatMessage message;

        boost::archive::text_iarchive ia(in);
        ia & message;

        std::cout << std::string(message.text.begin(), message.text.end()) << std::endl;
        this->sendChatMessage(socket, message);
        return 0;
    }
    m.unlock();

    return 0;
}

10-01 17:03