这两个函数仅用于套接字。

函数原型:

 #include <sys/socket.h>
#include <sys/types.h> int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen); int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);

sockfd:必须指向一个打开的套接字描述符

level:指定系统中解释选项的代码或为通用套接字代码,或为某个特定于协议的代码(例如IPv4、IPv6、TCP或SCTP)。

optval:指向某个变量(*optval)的指针,setsockopt从*optval中取得选项设置的新值,getsockopt则把以获取的选项当前值存入*optval。

optlen:指定*optval的大小

套接字选项的汇总可以查看unix网络编程(卷1)151页 图7-1和图7-2.

LINUX下getsockopt和setsockopt函数-LMLPHP

LINUX下getsockopt和setsockopt函数-LMLPHP

LINUX下getsockopt和setsockopt函数-LMLPHP

LINUX下getsockopt和setsockopt函数-LMLPHP

并非所有实现都支持所有的套接字选项,下面的程序可以简单测试系统是否支持选项:

 /* include checkopts1 */
/* *INDENT-OFF* */
#include "unp.h"
#include <netinet/tcp.h> /* for TCP_xxx defines */ union val {
int i_val;
long l_val;
struct linger linger_val;
struct timeval timeval_val;
} val; static char *sock_str_flag(union val *, int);
static char *sock_str_int(union val *, int);
static char *sock_str_linger(union val *, int);
static char *sock_str_timeval(union val *, int); struct sock_opts {
const char *opt_str;
int opt_level;
int opt_name;
char *(*opt_val_str)(union val *, int);
} sock_opts[] = {
#ifdef SO_BROADCAST
{ "SO_BROADCAST", SOL_SOCKET, SO_BROADCAST, sock_str_flag },
#else
{ "SO_BROADCAST", , , NULL},
#endif
#ifdef SO_DEBUG
{ "SO_DEBUG", SOL_SOCKET, SO_DEBUG, sock_str_flag },
#else
{ "SO_DEBUG", , , NULL},
#endif
#ifdef SO_DONTROUTE
{ "SO_DONTROUTE", SOL_SOCKET, SO_DONTROUTE, sock_str_flag },
#else
{ "SO_DONTROUTE", , , NULL},
#endif
#ifdef SO_ERROR
{ "SO_ERROR", SOL_SOCKET, SO_ERROR, sock_str_int },
#else
{ "SO_ERROR", , , NULL},
#endif
#ifdef SO_KEEPALIVE
{ "SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE, sock_str_flag },
#else
{ "SO_KEEPALIVE", , , NULL},
#endif
#ifdef SO_LINGER
{ "SO_LINGER", SOL_SOCKET, SO_LINGER, sock_str_linger },
#else
{ "SO_LINGER", , , NULL},
#endif
#ifdef SO_OOBINLINE
{ "SO_OOBINLINE", SOL_SOCKET, SO_OOBINLINE, sock_str_flag },
#else
: "SO_OOBINLINE", , , NULL},
#endif
#ifdef SO_RCVBUF
{ "SO_RCVBUF", SOL_SOCKET, SO_RCVBUF, sock_str_int },
#else
{ "SO_RCVBUF", , , NULL},
#endif
#ifdef SO_SNDBUF
{ "SO_SNDBUF", SOL_SOCKET, SO_SNDBUF, sock_str_int },
#else
{ "SO_SNDBUF", , , NULL},
#endif
#ifdef SO_RCVLOWAT
{ "SO_RCVLOWAT", SOL_SOCKET, SO_RCVLOWAT, sock_str_int },
#else
{ "SO_RCVLOWAT", , , NULL},
#endif
#ifdef SO_SNDLOWAT
{ "SO_SNDLOWAT", SOL_SOCKET, SO_SNDLOWAT, sock_str_int },
#else
{ "SO_SNDLOWAT", , , NULL},
#endif
#ifdef SO_RCVTIMEO
{ "SO_RCVTIMEO", SOL_SOCKET, SO_RCVTIMEO, sock_str_timeval },
#else
{ "SO_RCVTIMEO", , , NULL},
#endif
#ifdef SO_SNDTIMEO
{ "SO_SNDTIMEO", SOL_SOCKET, SO_SNDTIMEO, sock_str_timeval },
#else
{ "SO_SNDTIMEO", , , NULL},
#endif
#ifdef SO_REUSEADDR
{ "SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR, sock_str_flag },
#else
{ "SO_REUSEADDR", , , NULL},
#endif
#ifdef SO_REUSEPORT
{ "SO_REUSEPORT", SOL_SOCKET, SO_REUSEPORT, sock_str_flag },
#else
{ "SO_REUSEPORT", , , NULL },
#endif
#ifdef SO_TYPE
{ "SO_TYPE", SOL_SOCKET, SO_TYPE, sock_str_int },
#else
{ "SO_TYPE", , , NULL},
#endif
#ifdef SO_USELOOPBACK
{ "SO_USELOOPBACK", SOL_SOCKET, SO_USELOOPBACK, sock_str_flag },
#else
{ "SO_USELOOPBACK", , , NULL},
#endif
#ifdef IP_TOS
{ "IP_TOS", IPPROTO_IP, IP_TOS, sock_str_int },
#else
{ "IP_TOS", , , NULL},
#endif
#ifdef IP_TTL
{ "IP_TTL", IPPROTO_IP, IP_TTL, sock_str_int },
#else
{ "IP_TTL", , , NULL},
#endif
#ifdef IPV6_DONTFRAG
{ "IPV6_DONTFRAG", IPPROTO_IPV6,IPV6_DONTFRAG, sock_str_flag },
#else
{ "IPV6_DONTFRAG", , , NULL },
#endif
#ifdef IPV6_UNICAST_HOPS
{ "IPV6_UNICAST_HOPS", IPPROTO_IPV6,IPV6_UNICAST_HOPS,sock_str_int },
#else
{ "IPV6_UNICAST_HOPS", , , NULL },
#endif
#ifdef IPV6_V6ONLY
{ "IPV6_V6ONLY", IPPROTO_IPV6,IPV6_V6ONLY, sock_str_flag },
#else
{ "IPV6_V6ONLY", , , NULL },
#endif
#ifdef TCP_MAXSEG
{ "TCP_MAXSEG", IPPROTO_TCP,TCP_MAXSEG, sock_str_int },
#else
{ "TCP_MAXSEG", , , NULL},
#endif
#ifdef TCP_NODELAY
{ "TCP_NODELAY", IPPROTO_TCP,TCP_NODELAY, sock_str_flag },
#else
{ "TCP_NODELAY", , , NULL},
#endif
#ifdef SCTP_AUTOCLOSE
{ "SCTP_AUTOCLOSE", IPPROTO_SCTP,SCTP_AUTOCLOSE,sock_str_int },
#else
{ "SCTP_AUTOCLOSE", , , NULL },
#endif
#ifdef SCTP_MAXBURST
{ "SCTP_MAXBURST", IPPROTO_SCTP,SCTP_MAXBURST, sock_str_int },
#else
{ "SCTP_MAXBURST", , , NULL },
#endif
#ifdef SCTP_MAXSEG
{ "SCTP_MAXSEG", IPPROTO_SCTP,SCTP_MAXSEG, sock_str_int },
#else
{ "SCTP_MAXSEG", , , NULL },
#endif
#ifdef SCTP_NODELAY
{ "SCTP_NODELAY", IPPROTO_SCTP,SCTP_NODELAY, sock_str_flag },
#else
{ "SCTP_NODELAY", , , NULL },
#endif
{ NULL, , , NULL }
};
/* *INDENT-ON* */
/* end checkopts1 */ /* include checkopts2 */
int
main(int argc, char **argv)
{
int fd;
socklen_t len;
struct sock_opts *ptr; for (ptr = sock_opts; ptr->opt_str != NULL; ptr++) {
printf("%s: ", ptr->opt_str);
if (ptr->opt_val_str == NULL)
printf("(undefined)\n");
else {
switch(ptr->opt_level) {
case SOL_SOCKET:
case IPPROTO_IP:
case IPPROTO_TCP:
fd = Socket(AF_INET, SOCK_STREAM, );
break;
#ifdef IPV6
case IPPROTO_IPV6:
fd = Socket(AF_INET6, SOCK_STREAM, );
break;
#endif
#ifdef IPPROTO_SCTP
case IPPROTO_SCTP:
fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
break;
#endif
default:
err_quit("Can't create fd for level %d\n", ptr->opt_level);
} len = sizeof(val);
if (getsockopt(fd, ptr->opt_level, ptr->opt_name,
&val, &len) == -) {
err_ret("getsockopt error");
} else {
printf("default = %s\n", (*ptr->opt_val_str)(&val, len));
}
close(fd);
}
}
exit();
}
/* end checkopts2 */ /* include checkopts3 */
static char strres[]; static char *
sock_str_flag(union val *ptr, int len)
{
/* *INDENT-OFF* */
if (len != sizeof(int))
snprintf(strres, sizeof(strres), "size (%d) not sizeof(int)", len);
else
snprintf(strres, sizeof(strres),
"%s", (ptr->i_val == ) ? "off" : "on");
return(strres);
/* *INDENT-ON* */
}
/* end checkopts3 */ static char *
sock_str_int(union val *ptr, int len)
{
if (len != sizeof(int))
snprintf(strres, sizeof(strres), "size (%d) not sizeof(int)", len);
else
snprintf(strres, sizeof(strres), "%d", ptr->i_val);
return(strres);
} static char *
sock_str_linger(union val *ptr, int len)
{
struct linger *lptr = &ptr->linger_val; if (len != sizeof(struct linger))
snprintf(strres, sizeof(strres),
"size (%d) not sizeof(struct linger)", len);
else
snprintf(strres, sizeof(strres), "l_onoff = %d, l_linger = %d",
lptr->l_onoff, lptr->l_linger);
return(strres);
} static char *
sock_str_timeval(union val *ptr, int len)
{
struct timeval *tvptr = &ptr->timeval_val; if (len != sizeof(struct timeval))
snprintf(strres, sizeof(strres),
"size (%d) not sizeof(struct timeval)", len);
else
snprintf(strres, sizeof(strres), "%ld sec, %ld usec",
tvptr->tv_sec, tvptr->tv_usec);
return(strres);
}

我用的系统是Ubuntu 13.04,结果截图:

LINUX下getsockopt和setsockopt函数-LMLPHP

05-06 08:50