ccplusplus.com
Learn C, C++ Concepts
Tuesday, September 6, 2011
getsockopt so_type example c code
/* gettype.c: * Retrieving the Socket Type (SO_TYPE) using getsockopt * Get SO_TYPE Option: */ #include
#include
#include
#include
#include
#include
#include
#include
/* * This function reports the error and * exits back to the shell: */ static void displayError(const char *on_what) { if ( errno != 0 ) { fputs(strerror(errno),stderr); fputs(": ",stderr); } fputs(on_what,stderr); fputc('\n',stderr); exit(1); } int main(int argc,char **argv) { int z; int s = -1; /* Socket */ int so_type = -1; /* Socket type */ socklen_t optlen; /* Option length */ /* * Create a TCP/IP socket to use: */ s = socket(PF_INET,SOCK_STREAM,0); if ( s == -1 ) { displayError("socket(2)"); } /* * Get socket option SO_SNDBUF: */ optlen = sizeof so_type; z = getsockopt(s, SOL_SOCKET, SO_TYPE, &so_type, &optlen); if ( z ) { displayError("getsockopt(s,SOL_SOCKET," "SO_TYPE)"); } assert(optlen == sizeof so_type); /* * Report the buffer sizes: */ printf("Socket s : %d\n",s); printf(" SO_TYPE : %d\n",so_type); printf(" SOCK_STREAM = %d\n", SOCK_STREAM); close(s); return 0; } /* * OUTPUT * [sgupta@rhel54x64 socket]$ gcc getty.c -o getty [sgupta@rhel54x64 socket]$ ./getty Socket s : 3 SO_TYPE : 1 SOCK_STREAM = 1 [sgupta@rhel54x64 socket]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment