Last active
August 29, 2015 14:07
-
-
Save zwetan/0fa2df2bae32c83ac867 to your computer and use it in GitHub Desktop.
strangely getsockopt() return 4 instead of 1 for some options
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
struct addrinfo hints, *res; | |
int sockfd; | |
memset(&hints, 0, sizeof hints); | |
hints.ai_family = AF_INET; | |
hints.ai_socktype = SOCK_STREAM; | |
hints.ai_flags = AI_PASSIVE; | |
int result = getaddrinfo(NULL, "3490", &hints, &res); | |
sockfd = socket( res->ai_family, res->ai_socktype, res->ai_protocol ); | |
int optval1; | |
socklen_t optlen1 = sizeof(optval1); | |
int resultO1 = getsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval1, &optlen1 ); | |
if( resultO1 < 0 ) | |
{ | |
printf( "## fail getsockopt()\n" ); | |
printf( "err %i: %s\n", errno, strerror(errno) ); | |
exit(1); | |
} | |
printf( "SO_REUSEADDR = %i\n", optval1 ); | |
int optval2 = 1; | |
int optlen2 = sizeof(optval2); | |
int resultO2 = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval2, optlen2 ); | |
if( resultO2 < 0 ) | |
{ | |
printf( "## fail setsockopt()\n" ); | |
printf( "err %i: %s\n", errno, strerror(errno) ); | |
exit(1); | |
} | |
printf( "SO_REUSEADDR changed = %i\n", resultO2 ); | |
int optval3; | |
socklen_t optlen3 = sizeof(optval3); | |
int resultO3 = getsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval3, &optlen3 ); | |
if( resultO3 < 0 ) | |
{ | |
printf( "## fail getsockopt()\n" ); | |
printf( "err %i: %s\n", errno, strerror(errno) ); | |
exit(1); | |
} | |
printf( "SO_REUSEADDR = %i\n", optval3 ); | |
//... | |
//output | |
/* | |
SO_REUSEADDR = 0 | |
SO_REUSEADDR changed = 0 | |
SO_REUSEADDR = 4 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment