The recv() function returns a value; when this value is less than zero, it denotes that an error has occurred. By examining errno, you can get a better idea as to the exact error.
For example:
Code:
#include <errno.h> // for errno global value
#include <string.h> // for strerror()
...
int bytesReceived = recv(sd, ...);
if (bytesReceived > 0)
{
// received data
}
else if (bytesReceived == 0)
{
// was the socket closed?
}
else
{
fprintf(stderr, "Error receiving from socket; reason is %s [%d]\n", strerror(errno), errno);
}