固定!聊天服务器使用boost时出现奇怪错误

时间:2015-10-20 14:22:50

标签: c++ multithreading server

我必须使用boost制作同步聊天服务器。我几乎完成了服务器代码,它可能仍然远离它应该是什么,但我有一个非常奇怪的错误,我在互联网上找不到任何东西。我真的想在继续之前解决它,但我不知道如何。这是代码:

#include<iostream>
#include<list>
#include<map>
#include<queue>
#include<vector>
#include<cstdlib>
#include<ctime>

#include<boost/thread.hpp>
#include<boost/bind.hpp>
#include<boost/asio.hpp>
#include<boost/asio/ip/tcp.hpp>

using namespace std;
using namespace boost::asio;
using namespace boost::asio::ip;

typedef boost::shared_ptr<tcp::socket> socket_ptr;
typedef boost::shared_ptr<string> string_ptr;
typedef boost::shared_ptr< list<socket_ptr> > clientList_ptr;
typedef boost::shared_ptr< list<string> > nameList_ptr;

const int THREADS = 1;

io_service service;
tcp::acceptor acceptor(service, tcp::endpoint(tcp::v4(), 30001));
boost::mutex mtx;
clientList_ptr clientList(new list<socket_ptr>);
nameList_ptr nameList(new list<string>);
boost::asio::streambuf buff;
time_t timer;

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

bool clientSentExit(string_ptr message)
{
    return message->find("exit") != string::npos;
}

void disconnectClient(socket_ptr clientSock, const boost::system::error_code& error)
{
    boost::system::error_code ec = error;
    auto position = find(clientList->begin(), clientList->end(), clientSock);

    clientSock->shutdown(tcp::socket::shutdown_both, ec);
    clientSock->close(ec);
    clientList->erase(position);

    cout << "Client Disconnected! " << clientList->size() << " total clients" << endl;
}

void do_processing(socket_ptr sock, const boost::system::error_code& error)
{
    if(!clientList->empty())
        {
            mtx.lock();

            boost::asio::streambuf buff;
            size_t bytes_transferred = boost::asio::read_until(*sock, buff, '\n');

            buff.commit(bytes_transferred);
            std::istream istrm(&buff);

            string_ptr msg(new string(""));
            std::getline(istrm, *msg);
            msg->push_back('\n');
            buff.consume(buff.size());

            if(clientSentExit(msg) || error)
            {
                disconnectClient(sock, error);
            }
            else
            {
                cout << "ChatLog: " << *msg << endl;

                for(auto& cliSock : *clientList)
                {
                    if (cliSock->is_open() && cliSock != sock)
                    {
                        cout << "Sending: " << time(&timer) << endl;
                        sock->write_some(buffer(*msg));
                    }
                }
            }

            mtx.unlock();
        }
}

int main(int argc, char *argv[])
{
    boost::thread_group threads;

    while (true)
    {
        cout << "Waiting for clients...\n";
        socket_ptr clientSock(new tcp::socket(service));

        acceptor.accept(*clientSock);
        clientList->emplace_back(clientSock);
        threads.create_thread(boost::bind(do_processing, clientSock, boost::asio::placeholders::error));
    }

    threads.join_all();
    getc(stdin);

     return 0;
}

以下是错误消息:

In file included from /sapmnt/HOME/i322722/usr/boost/include/boost/bind.hpp:22:0,
                 from /sapmnt/HOME/i322722/usr/boost/include/boost/thread/detail/thread.hpp:30,
                 from /sapmnt/HOME/i322722/usr/boost/include/boost/thread/thread_only.hpp:22,
                 from /sapmnt/HOME/i322722/usr/boost/include/boost/thread/thread.hpp:12,
                 from /sapmnt/HOME/i322722/usr/boost/include/boost/thread.hpp:13,
                 from server.cpp:9:
/sapmnt/HOME/i322722/usr/boost/include/boost/bind/bind.hpp: In instantiation of \u2018void boost::_bi::list2<A1, A2>::operator()(boost::_bi::type<void>, F&, A&, int) [with F = void (*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >, const boost::system::error_code&); A = boost::_bi::list0; A1 = boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > >; A2 = boost::arg<1> (*)()]\u2019:
/sapmnt/HOME/i322722/usr/boost/include/boost/bind/bind.hpp:895:50:   required from \u2018boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()() [with R = void; F = void (*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >, const boost::system::error_code&); L = boost::_bi::list2<boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > >, boost::arg<1> (*)()>; boost::_bi::bind_t<R, F, L>::result_type = void]\u2019
/sapmnt/HOME/i322722/usr/boost/include/boost/thread/detail/thread.hpp:116:17:   required from \u2018void boost::detail::thread_data<F>::run() [with F = boost::_bi::bind_t<void, void (*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp> >, const boost::system::error_code&), boost::_bi::list2<boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp> > >, boost::arg<1> (*)()> >]\u2019
**server.cpp:218:1:   required from here
/sapmnt/HOME/i322722/usr/boost/include/boost/bind/bind.hpp:315:56: error: no match for \u2018operator[]\u2019 (operand types are \u2018boost::_bi::list0\u2019 and \u2018boost::arg<1>()\u2019)
         unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);**

“从此处必需”语句中的数字是错误的,因为我想在IDE中保留许多注释,但在此处删除。发生错误的实际行是代码的最后一行 - 使用'}'关闭主函数。

我会感激任何答案。

1 个答案:

答案 0 :(得分:0)

现在已经修好了。正如他们告诉我的那样,这就像是

之间的对抗
const boost::system::error_code& error

boost::asio::placeholders::error.

他们告诉我不需要这些错误代码,如果聊天服务器是异步的,则需要这些错误代码,但此处线程组无法发送错误代码。

我不确定我是否完全理解了所有内容,如果答案看起来很糟糕,我很抱歉。删除除以下各处的错误代码:

boost::system::error_code ec;

修复了问题。