getsockopt.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 <stdio.h>
  6. #include <stdlib.h>
  7. #include <errno.h>
  8. #include <sys/socket.h>
  9. #include <assert.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;
  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,
  29. &so_type,&optlen);
  30. if ( z ) {
  31. printf("getsockopt(s,SOL_SOCKET,"
  32. "SO_TYPE) %d", errno);
  33. exit(-1);
  34. }
  35. assert(optlen == sizeof so_type);
  36. if (so_type == SOCK_STREAM) {
  37. printf("getsockopt: Got socket type OK\n");
  38. } else {
  39. printf("getsockopt: Got socket type failed\n");
  40. rv = -1;
  41. }
  42. return rv;
  43. }