setsockopt function uses

Function prototype

#include <sys/types.h >
#include <sys/socket.h>
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
  • sockfd: a socket descriptor identifier
  • level: Options defined hierarchy; support SOL_SOCKET, IPPROTO_TCP, IPPROTO_IP and IPPROTO_IPV6
  • optname: Options to be set, but there are some options are valid to be called before setting the listen / connect, this part of the options are as follows: SO_DEBUG, SO_DONTROUTE, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_RCVLOWAT, SO_SNDBUF, SO_SNDLOWAT, TCP_MAXSEG, TCP_NODELAY
  • optval: pointer to the buffer storage option value
  • optlen: optval buffer length

scenes to be used

1. Reuse socket

If already in the socket in the ESTABLISHED state (usually by the port number and identifier to distinguish) call to close (socket) (generally not immediately shut down and go through the process of TIME_WAIT) after want to continue to reuse the socket:

bool reuse=1;
setsockopt(s,SOL_SOCKET ,SO_REUSEADDR,(const char*)& reuse,sizeof(bool));

Note: You must set SO_REUSEADDR options before calling bind function.

2. No experience TIME_WAIT

soket If you want to have in a connected state forcibly closed after calling close (socket), not through the process of TIME_WAIT:

int reuse=0;
setsockopt(s,SOL_SOCKET ,SO_REUSEADDR,(const char*)& reuse,sizeof(int));

3. Set the transceiver minute

In the send (), recv () process is sometimes due to network conditions, etc., can not be expected for the transceiver, the transceiver can set time limit:

int nNetTimeout=1000; // 1秒
// 发送时限
setsockopt(socket,SOL_S0CKET, SO_SNDTIMEO,(char *)&nNetTimeout,sizeof(int));
// 接收时限
setsockopt(socket,SOL_S0CKET, SO_RCVTIMEO,(char *)&nNetTimeout,sizeof(int));

4. The transmission and receiving buffers

When send () returns the byte (sync) or to the socket buffer in bytes (asynchronous) actually transmitted; the transmission and receiving system default state once for 8688 bytes (about 8.5K) ; If the actual process of sending or receiving a large amount of data, socket buffer may be provided, to avoid send (), recv () constant cycle transceiver:

// 接收缓冲区
int nRecvBuf=32*1024; // 设置为32K
setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char*)&nRecvBuf,sizeof(int));
// 发送缓冲区
int nSendBuf=32*1024; // 设置为32K
setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(const char*)&nSendBuf,sizeof(int));

Note: not to say how much you set, the system will set how much the system will generally we set the buffer size is doubled, and not less than tcp receive buffer and transmit buffer minimum default settings.

5. When transmitting data, the system does not perform the copy buffer into the socket buffer to improve performance of the procedure:

int nZero=0;
setsockopt(fd,SOL_SOCKET,SO_SNDBUF,(char *)&nZero,sizeof(int));

6. When receiving data, the content is not executed socket buffer is copied to the buffer system:

int nZero=0;
setsockopt(fd,SOL_SOCKET,SO_RCVBUF,(char *)&nZero,sizeof(int));

7. socket so that the data transmitted with the broadcast characteristics

Usually when sending UDP data packets, it is desirable that the data transmitted has a socket broadcast characteristics:

bool bBroadcast = 1;
setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (const char*)&bBroadcast, sizeof(bool));

8. Test of survival

int opt = 1;
if (setsockopt (fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(int)) == -1)
{
    return 0;
}

9. delayed received

In fact, after receiving the first data, will create a connection. For non-interactive such as http server, this makes sense, the defense can attack air connection.

int val = 5;
setsockopt(fd, SOL_TCP, TCP_DEFER_ACCEPT, &val, sizeof(val));

After opening this function, the kernel has not received the kind of data val time, the process will not continue to wake up, but directly drops the connection.
Speaking after the three-way handshake is set in this state, even if the completion of the three-way handshake, not a server socket state ESTABLISHED, while still SYN_RCVD, not to receive data.

10. Close the delay socket

linger: hover delay.
By default, when you call close close of use socke, close returns immediately, however, if the send buffer still contains data, the system will try to put the data in the send buffer is sent out, and then close it to return.
SO_LINGER option is to delay socket is closed.

#include <sys/socket.h>
struct linger {
      int l_onoff  //0=off, nonzero=on(开关)
      int l_linger //linger time(延迟时间)
}
struct linger Linger;
Linger.l_onoff = 1; //在调用close(socket)时还有数据未发送完,允许等待
// 若Linger.l_onoff=0;则调用closesocket()后强制关闭
Linger.l_linger = 5; //设置等待时间为5秒
setsockopt(fd, SOL_SOCKET, SO_LINGER, (const char*)&Linger, sizeof(struct linger));

Guess you like

Origin blog.csdn.net/Vickers_xiaowei/article/details/93402601