setsockopt()¶
Purpose¶
setsockopt() configures socket options.
It is commonly used to enable SO_REUSEADDR for local server testing.
Header¶
Prototype¶
Minimal Example¶
int enable = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
&enable, sizeof(enable)) < 0) {
perror("setsockopt");
return -1;
}
Common Pitfalls¶
- Passing the wrong level, such as
SOL_SOCKETvsIPPROTO_TCP. - Passing the wrong option value size.
- Expecting
SO_REUSEADDRto solve all bind conflicts.