关于setsockopt()和getsockopt()函数

use*_*401 5 c sockets networking network-programming

在套接字编程中,到底使用了哪些套接字选项,例如setsockopt()和getsockopt()?

San*_*huk 4

例如您想设置或知道接收缓冲区大小

1)

int skt, int sndsize;
err = setsockopt(skt, SOL_SOCKET, SO_RCVBUF, (char *)&sndsize,
                                 (int)sizeof(sndsize));

err = getsockopt(skt, SOL_SOCKET, SO_RCVBUF, (char *)&sockbufsize, &size);
Run Code Online (Sandbox Code Playgroud)

2) 复用地址

 int on = 1;
 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
Run Code Online (Sandbox Code Playgroud)