This is a small mistake, but takes me two hours to find out, take a look:
if (nbytes = recv(i, buf, sizeof buf, 0) <= 0) {
...
}
I find my program always has error and can’t find the reason. This line will make nbytes to be zero and of course this will make error.
The right version:
if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
...
}