ccplusplus.com
Learn C, C++ Concepts
Monday, September 5, 2011
getsockopt example c code
/* getsndrcv.c: * getsockopt sample c code * using getsockopt * Get SO_SNDBUF & SO_RCVBUF Options: */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <assert.h> /* * 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 sndbuf=0; /* Send buffer size */ int rcvbuf=0; /* Receive buffer size */ socklen_t optlen; /* Option length */ /* * Create a TDP/IP socket to use: */ s = socket(PF_INET,SOCK_STREAM,0); if ( s == -1 ) { displayError("socket(2)"); } /* * Get socket option SO_SNDBUF: */ optlen = sizeof sndbuf; z = getsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf,&optlen); if ( z ) { displayError("getsockopt(s,SOL_SOCKET," "SO_SNDBUF)"); } assert(optlen == sizeof sndbuf); /* * Get socket option SO_SNDBUF: */ optlen = sizeof rcvbuf; z = getsockopt(s, SOL_SOCKET, SO_RCVBUF, &rcvbuf,&optlen); if ( z ) { displayError("getsockopt(s,SOL_SOCKET," "SO_RCVBUF)"); } assert(optlen == sizeof rcvbuf); /* * Report the buffer sizes: */ printf( "Socket s : %d\n",s); printf( "Send buf: %d bytes\n", sndbuf); printf( "Recv buf: %d bytes\n", rcvbuf); close(s); return 0; } /* * OUTPUT * [sgupta@rhel54x64 socket]$ gcc getsndrcv.c -o getsndrcv [sgupta@rhel54x64 socket]$ ./getsndrcv Socket s : 3 Send buf: 16384 bytes Recv buf: 87380 bytes [sgupta@rhel54x64 socket]$ */
See Also
Other
Socket Options
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment