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> 688.19. shutdown()S<strong>to</strong>p further sends and recieves on a socketPro<strong>to</strong>types#include int shutdown(int s, int how);DescriptionThat’s it! I’ve had it! No more send()s are allowed on this socket, but I still want <strong>to</strong> recv()data on it! Or vice-versa! How can I do this?When you close() a socket descrip<strong>to</strong>r, it closes both sides of the socket for reading andwriting, and frees the socket descrip<strong>to</strong>r. If you just want <strong>to</strong> close one side or the other, you can usethis shutdown() call.As for parameters, s is obviously the socket you want <strong>to</strong> perform this action on, and what actionthat is can be specified with the how paramter. How can be SHUT_RD <strong>to</strong> prevent further recv()s,SHUT_WR <strong>to</strong> prohibit further send()s, or SHUT_RDWR <strong>to</strong> do both.Note that shutdown() doesn’t free up the socket descrip<strong>to</strong>r, so you still have <strong>to</strong> eventuallyclose() the socket even if it has been fully shut down.This is a rarely used system call.Return ValueReturns zero on success, or -1 on error (and errno will be set accordingly.)Exampleint s = socket(PF_INET, SOCK_STREAM, 0);// ...do some send()s and stuff in here...// and now that we’re done, don’t allow any more sends()s:shutdown(s, SHUT_RD);See Alsoclose()

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

Saved successfully!

Ooh no, something went wrong!