connect()失败:连接被拒绝同时从两个节点接收1MB数据的错误

时间:2016-07-11 01:32:28

标签: c++ multithreading c++11 tcp network-programming

我有一个树结构,其中父节点将数据发送到两个节点,子节点将相同的数据发送回父节点。使用两个线程向两个孩子发送,并且两个孩子都收到消息。 Parent等待孩子们发回消息并为每个连接生成一个线程。如果我的数据小于1MB,我的孩子都会获取数据并将其发送回父母,父母从两个孩子那里获取数据。但是当数据为1MB或更多时,我的一个孩子会收到connect() failed: Connection refused错误,父母会从一个孩子那里收到数据,并一直等待其他孩子发送数据。

这是在每个连接上生成新线程的方法。

void Node::listenForMultipleReplies(int portNum, int numOfChildren){
    std::thread threadA[numOfChildren];
    TCPStream* stream = NULL;
    TCPAcceptor* acceptor = new TCPAcceptor(portNum);
    if (acceptor->start() == 0) {
            for (int i = 0; i < numOfChildren; ++i){
                    LOG(INFO) << "Listening for input on port: " << portNum;
                    stream = acceptor->accept(); //Blocking; wait till it gets the connection
                    if (stream != NULL) {
                            LOG(INFO) << "Connection Successful";
                            threadA[i] = std::thread(multiple, stream);
                    }
            }
    }

    for(int i = 0; i < numOfChildren; i++)
    {
            threadA[i].join();
            LOG(INFO) << "Joined "<< i+1 << "threads successfully!!";
    }

}

这是由新线程

调用的connection_handler
void multiple(TCPStream* stream){
    std::string received;
    ssize_t len;
    char line[HUNDMB];
    LOG(INFO) << "Thread created with ID: " << std::this_thread::get_id();
    if ((len = stream->receive(line, sizeof(line))) > 0) {
            line[len] = 0;
            received = string(line);
            LOG(INFO) << "Results for Query " << received.substr(0,36) << " received; size of the result is "<< received.size();
    }

}

0 个答案:

没有答案