main.cpp中的for循环调用一个使用boost :: mutex并使用read_until从套接字读取的函数,只运行一次,之后它就像被阻塞一样。我试过在结束括号之前放一个继续然后它崩溃了。它可能与线程有关。
// 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;
}
答案 0 :(得分: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;
}