Several configuration functions of socket

int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);

sockfd : a descriptor that identifies a socket

level : level of option definitionSOL_SOCKET,IPPROTO_TCP,IPPROTO_IPandIPPROTO_IPV6

optname : the option to set

optval : pointer to the buffer where the option value is stored

optlenoptvalbuffer length

Returns 0 on success, -1 on failure


1.  If the socket that is already in the  ESTABLISHED state ( usually distinguished by the port number and identifier) ​​calls closesocket (generally will not be closed immediately but go through the process of TIME_WAIT ) and want to continue to reuse the socket : ( Do not write close description in the program In fact, it doesn’t matter if you write or not, here it is set to be reused )   The previous programming did not set this, and it cannot be reused by default, so only one server can run, not multiple at the same time; this interface can achieve load balancing at the system level.

int reuse=1;   the default value is 0

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


int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
        \\ Get the local socket information
and return 0 if it succeeds, and -1 if it fails.


int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
        \\ Get the address of the accepted client and
return 0 for success, -1 for failure.


int shutdown(int sockfd, int how);
          \\ 关闭读端、写端或者读写端
成功返回0,失败返回-1。

sockfd:  accept 返回的描述符
how:    
      SHUT_RD    关闭读端
      SHUT_WR    关闭写端
      SHUT_RDWR  关闭读写端


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324624307&siteId=291194637