connect() failed: Connection refused Error for receiving 1MB data from two nodes simultaneously
<p>I have a tree structure in which parent sends data to two nodes and children send back the same data to parent. Sending to both children is done using two threads and both the children are getting the message. Parent waits for children to send the message back and spawns a thread for each connection. If my data is less than 1MB, both my children get the data and they send it back to parent, parent gets data from both the children. But when data is 1MB or more, I get <code>connect() failed: Connection refused</code> error on one of the child, parent receives data from one child and keeps waiting for other child to send data.</p>

<p>This is the method which spawns new thread on each connection. </p>

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

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

<p>}</p>

<p>This is connection_handler which is called by new thread</p>

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

<p>}</p>