I am calling bind() on a socket, and the call is failing owing to "Address in use". I am fairly confident that's the failure, anyway - the socket has been left open by a previous instance of the app that crashed. Specifically, bind() returns a negative value. However, errno reads zero on the function's return. Bizarrely, perror(), however, generates "Address in use".
Under what conditions can errno and perror() disagree on the value of the error code? I understand that the errno system is thread-safe (there are other threads running in the meantime). I am on Linux 3.14.43.
if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
printf("\nerrno %i %s\n", errno, strerror(errno));
perror("PERROR");
// raise error
__ERROR("failed bind socket");
}
The above code generates this output:
errno 0 Success
PERROR: Address already in use
**** ERROR **** failed bind socket
Perhaps I have misunderstood the relationship between errno, strerror(), and perror()?