getsockopt.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* Unit test for issue #92.
  2. * Example for use of getsockopt with SO_TYPE
  3. * taken from here: http://alas.matf.bg.ac.rs/manuals/lspe/snode=103.html
  4. */
  5. #include <assert.h>
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <sys/socket.h>
  10. int main(int argc, char** argv) {
  11. int z;
  12. int s = -1; /* Socket */
  13. int so_type = -1; /* Socket type */
  14. socklen_t optlen; /* Option length */
  15. int rv = 0;
  16. /*
  17. * Create a TCP/IP socket to use:
  18. */
  19. s = socket(PF_INET, SOCK_STREAM, 0);
  20. if (s == -1) {
  21. printf("socket(2) error %d", errno);
  22. exit(-1);
  23. }
  24. /*
  25. * Get socket option SO_SNDBUF:
  26. */
  27. optlen = sizeof so_type;
  28. z = getsockopt(s, SOL_SOCKET, SO_TYPE, &so_type, &optlen);
  29. if (z) {
  30. printf("getsockopt(s,SOL_SOCKET,SO_TYPE) %d", errno);
  31. exit(-1);
  32. }
  33. assert(optlen == sizeof so_type);
  34. if (so_type == SOCK_STREAM) {
  35. printf("getsockopt: Got socket type OK\n");
  36. } else {
  37. printf("getsockopt: Got socket type failed\n");
  38. rv = -1;
  39. }
  40. return rv;
  41. }