Strange SIGPIPE in loop <p>After dealing with a very strange error in a C++ program I was writing, I decided to write the following test code, confirming my suspicion. In the original program, calling <code>send()</code> and <code>this_thread::sleep_for()</code> (with any amount of time) in a loop 16 times caused send to fail with a <code>SIGPIPE</code> signal. In this example however, it fails after 4 times. </p> <p>I have a server running on port 25565 bound to localhost. The original program was designed to communicate with this server. I'm using the same one in this test code because it doesn't terminate connections early. </p> <pre><code>int main() { struct sockaddr_in sa; memset(sa.sin_zero, 0, 8); sa.sin_family = AF_INET; inet_pton(AF_INET, "127.0.0.1", &amp;(sa.sin_addr)); sa.sin_port = htons(25565); cout &lt;&lt; "mark 1" &lt;&lt; endl; int sock = socket(AF_INET, SOCK_STREAM, 0); connect(sock, (struct sockaddr *) &amp;sa, sizeof(sa)); cout &lt;&lt; "mark 2" &lt;&lt; endl; for (int i = 0; i &lt; 16; i++) { cout &lt;&lt; "mark 3" &lt;&lt; endl; cout &lt;&lt; "sent " &lt;&lt; send(sock, &amp;i, 1, 0) &lt;&lt; " byte" &lt;&lt; endl; cout &lt;&lt; "errno == " &lt;&lt; errno &lt;&lt; endl; cout &lt;&lt; "i == " &lt;&lt; i &lt;&lt; endl; this_thread::sleep_for(chrono::milliseconds(2)); } return 0; } </code></pre> <p>Running it in GDB is how I discovered it was emitting SIGPIPE. Here is the output of that: <a href="http://pastebin.com/gXg2Y6g1" rel="nofollow">http://pastebin.com/gXg2Y6g1</a> </p> <p>In another test, I called <code>this_thread::sleep_for()</code> 16 times in a loop, THEN called <code>send()</code> once. This did NOT produce the same error. It ran without issue. </p> <p>In yet another test, I commented out the thread sleeping line, and it ran all the way through just fine. I did this in both the original program and the above test code. </p> <p>These results make me believe it's not a case of the server closing the connection, even though that's usually what SIGPIPE means (why did it run fine when there was no call to <code>this_thread::sleep_for()</code>?). </p> <p>Any ideas as to what could be causing this? I've been messing around with it for a week and have gotten no further.</p>