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...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Beej’s <strong>Guide</strong> <strong>to</strong> <strong>Network</strong> <strong>Programming</strong> <strong>Using</strong> <strong>Internet</strong> <strong>Sockets</strong> 458.4. close()Close a socket descrip<strong>to</strong>rPro<strong>to</strong>types#include int close(int s);DescriptionAfter you’ve finished using the socket for whatever demented scheme you have concocted andyou don’t want <strong>to</strong> send() or recv() or, indeed, do anything else at all with the socket, you canclose() it, and it’ll be freed up, never <strong>to</strong> be used again.The remote side can tell if this happens one of two ways. One: if the remote side calls recv(),it will return 0. Two: if the remote side calls send(), it’ll recieve a signal SIGPIPE and send()will return -1 and errno will be set <strong>to</strong> EPIPE.Windows users: the function you need <strong>to</strong> use is called closesocket(), not close(). Ifyou try <strong>to</strong> use close() on a socket descrip<strong>to</strong>r, it’s possible Windows will get angry... And youwouldn’t like it when it’s angry.Return ValueReturns zero on success, or -1 on error (and errno will be set accordingly.)Examples = socket(PF_INET, SOCK_DGRAM, 0);.// a whole lotta stuff...*BRRRONNNN!*.close(s); // not much <strong>to</strong> it, really.See Alsosocket(), shutdown()

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

Saved successfully!

Ooh no, something went wrong!