views:

291

answers:

2

I'm developing a dedicated game server on a linux machine, in C/C++ (mixed). I have the following snippet of code:

int sockfd=socket(AI_INET, SOCK_DGRAM, 0);
if(sockfd==-1)
{
    int err=errno;
    fprintf(stderr,"%s",strerror(err));
    exit(1);
}

My problem here, is that socket is returning -1 (implying a failure) and the error string is being printed, but it is "Success" (ERROR_SUCCESS).

Other notes:

  • I am requesting a socket on a port >1024 (out of context, but thought I'd mention)
  • I'm executing the app as the super user
A: 

Do you have multiple threads running? They could be overwriting the errno value.

Are there any lines of code between socket() and if() that you left out? Another function call could overwrite the errno.

Robert
`errno` is thread-local by C standard
qrdl
The C standard has no concept of threads. Though posix requires it to be thread local.
nos
+6  A: 

I feel incredibly stupid. Carefully looking over my code, on my dev-computer shows:

if(sockfd==-1);
...
Aviral Dasgupta
you are not the first one to do this, nor will you be the last one :-)
moritz
This is exactly why ERROR_SUCCESS exists. :-)
T.E.D.
Aviral Dasgupta
aviraldg, you might want to accept your own answer so the question appears as solved.
Georg Fritzsche
Let this be a lesson: Always copy and paste *real* code that demonstrates the problem you're asking about.
Rob Kennedy
@rob yes, of course, but not if you're programming on a different computer...
Aviral Dasgupta