13.07.2015 Views

Beej's Guide to Network Programming Using Internet Sockets

Beej's Guide to Network Programming Using Internet Sockets

Beej's Guide to Network Programming Using Internet Sockets

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Beej’s <strong>Guide</strong> <strong>to</strong> <strong>Network</strong> <strong>Programming</strong> <strong>Using</strong> <strong>Internet</strong> <strong>Sockets</strong> 508.8. errnoHolds the error code for the last system callPro<strong>to</strong>types#include int errno;DescriptionThis is the variable that holds error information for a lot of system calls. If you’ll recall, thingslike socket() and listen() return -1 on error, and they set the exact value of errno <strong>to</strong> let youknow specifically which error occurred.The header file errno.h lists a bunch of constant symbolic names for errors, such as EADDRI-NUSE, EPIPE, ECONNREFUSED, etc. Your local man pages will tell you what codes can be returnedas an error, and you can use these at run time <strong>to</strong> handle different errors in different ways.Or, more commonly, you can call perror() or strerror() <strong>to</strong> get a human-readable versionof the error.Return ValueThe value of the variable is the latest error <strong>to</strong> have transpired, which might be the code for“success” if the last action succeeded.Examples = socket(PF_INET, SOCK_STREAM, 0);if (s == -1) {perror("socket"); // or use strerror()}tryagain:if (select(n, &readfds, NULL, NULL) == -1) {// an error has occurred!!}// if we were only interrupted, just restart the select() call:if (errno == EINTR) go<strong>to</strong> tryagain; // AAAA! go<strong>to</strong>!!!// otherwise it’s a more serious error:perror("select");exit(1);See Alsoperror(), strerror()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!