Example:
// socket created above this
int iSocketOption = 1;
setsockopt(CFSocketGetNative(socket), SOL_SOCKET, SO_REUSEADDR, (void *)&iSocketOption, sizeof(iSocketOption));
I understand that when you pass a wildcard pointer to the address of an int set to 1 for the option_value arg (4th arg), you are saying, yes, use the SO_REUSEADDR option.
But what is going on with option_len, the last arg? The idea seems to be to recycle the int used for the arg before it, making it into a buffer for reporting the size of an unrelated value. What is that unrelated value? Should I be doing something with it?
Here’s the excerpt from the BSD Systems Calls Manual that’s confusing me:
For getsockopt(), option_len is a value-result parameter, initially containing the size of the buffer pointed to by option_value, and modified on return to indicate the actual size of the value returned.
What is the value whose size is to be returned using the buffer passed in via the penultimate arg of setsockopt?
(Granted, the excerpt regards getsockopt, not setsockopt, but there’s no other explanation of that last arg.)
Why I asked this question:
Apress’s More iPhone 3 Development gives this example of a socket setup, inside a method that returns a BOOL:
- (BOOL) methodInvolvingSocket {
// socket creation
int ret = 1;
setsockopt(CFSocketGetNative(socket), SOL_SOCKET, SO_REUSEADDR, (void *)&ret,
sizeof(ret));
//...socket address set up, but no further use of “ret.”
return ret;
}
At the end of this method, the return is made using “ret” — which works, because ret is 1, the equivalent of YES, but it’s a pretty weird thing to do unless there were some expectation that the value of “ret” might change.
For
setsockoptthe last parameter does nothing more than tell the function how long the memory pointer to by the parameter before that is.