Linux Network programming Function--address multiplexing setsockopt ()

Source: Internet
Author: User

1, setsockopt () Role and introduction

The socket is not withdrawn immediately after it is closed, but it is going through a time_wait phase. The port is now re-bound to an error. To bind the port immediately, you need to set the SO_REUSEADDR first.

Or, when closesocket, use setsockopt to set the So_dontlinger. The time_wait time is eliminated and can be achieved with the setsockopt () function.

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

/* SOCKFD: Identifies a description word for a set of interfaces.
Level: The hierarchy of option definitions; Sol_socket, ipproto_tcp, Ipproto_ip, and Ipproto_ipv6 are supported.
Optname: Options to be set.
Optval: Pointer to the buffer that holds the option value.
Optlen:optval the buffer length.
Return value: Successfully returned 0, failure returned-1. */

2. Specific application

1 //address re-use2 voidSET_REUSEADDR (intSOCKFD,intoptval)3 {4     intOn = (Optval! =0) ?1:0;5     //int setsockopt (int sockfd, int level, int optname,const void *optval, socklen_t optlen);6     /*SOCKFD: A descriptive word that identifies a set of interfaces. 7 level: The hierarchy of option definitions; Sol_socket, ipproto_tcp, Ipproto_ip, and Ipproto_ipv6 are supported. 8 optname: Options to be set. 9 optval: Pointer to the buffer that holds the option value. Ten optlen:optval the buffer length.  One return value: Successfully returned 0, failure returned-1. */ A  -      if(SetSockOpt (SOCKFD, Sol_socket, SO_REUSEADDR, &on,sizeof(ON)) <0) -Err_exit ("setsockopt so_reuseaddr"); the } -  - //Port Multiplexing - voidSet_reuseport (intSOCKFD,intoptval) + { - #ifdef So_reuseport +     intOn = (Optval! =0) ?1:0; A     if(SetSockOpt (SOCKFD, Sol_socket, So_reuseport, &on,sizeof(ON)) <0) atErr_exit ("setsockopt So_reuseport"); - #else -fprintf (stderr,"So_reuseport is not supported.\n"); - #endif //So_reuseport -}

3. Supplement

1. If a socket that is already in the established state (typically distinguished by a port number and a marker) is called
Closesocket (typically does not close immediately and undergoes the time_wait process) to continue to reuse the socket:
BOOL breuseaddr=true;
SetSockOpt (S,sol_socket, SO_REUSEADDR, (const
char*) &amp;breuseaddr,sizeof (BOOL));

2. If you want a soket that is already in the connected state to be forced to close after calling Closesocket, do not experience
The process of time_wait:
BOOL Bdontlinger = FALSE;
SetSockOpt (S,sol_socket,so_dontlinger, (const
char*) &amp;bdontlinger,sizeof (BOOL));

      3. In the Send (), recv () process sometimes due to network conditions and other reasons, the collection can not be expected to proceed, and set the transceiver time:
       int NNETTIMEOUT=1000;//1 seconds
     //Send time limit
      SetSockOpt (Socket,sol_s0cket,so_sndtimeo, (Char&NBSP;
       *) &amp;nnettimeout,sizeof (int));
     //Receive time limit
      setsockopt (socket,sol_s0cket,so_ Rcvtimeo, (Char&NBSP;
      *) &amp; nnettimeout,sizeof (int));

4. In Send (), the actual bytes sent (synchronous) or bytes sent to the socket buffer are returned.
(asynchronous); The system default state send and Receive is 8688 bytes (approximately 8.5K), and data is sent in the actual process.
and receive a large amount of data, you can set the socket buffer, and avoid the Send (), recv () Continuous loop transceiver:
Receive buffers
int nrecvbuf=32*1024;//set to 32K
SetSockOpt (S,sol_socket,so_rcvbuf, (const char*) &amp;nrecvbuf,sizeof (int));
Send buffer
int nsendbuf=32*1024;//set to 32K
SetSockOpt (S,sol_socket,so_sndbuf, (const char*) &amp;nsendbuf,sizeof (int));

5. If you do not want to experience a copy of the system buffer to the socket buffer when sending the data
Performance of the program:
int nzero=0;
SetSockOpt (Socket,sol_s0cket,so_sndbuf, (char *) &amp;nzero,sizeof (Nzero));

6. Complete the above function (by default, copy the contents of the socket buffer to the system buffer) in recv ():
int nzero=0;
SetSockOpt (Socket,sol_s0cket,so_rcvbuf, (char *) &amp;nzero,sizeof (int));

7. In general, when sending a UDP datagram, you want the data sent by the socket to have broadcast characteristics:
BOOL bbroadcast=true;
SetSockOpt (S,sol_socket,so_broadcast, (const
char*) &amp;bbroadcast,sizeof (BOOL));

8. In the Client connection server process, if the socket in non-blocking mode is in the process of connect (), you can
To set the Connect () delay until Accpet () is called (this function is set only in the non-blocking process with significant
function, which has little effect in blocking functions)
BOOL bconditionalaccept=true;
SetSockOpt (s,sol_socket,so_conditional_accept, (const
char*) &amp;bconditionalaccept,sizeof (BOOL));

9. If in the process of sending the data (send () is not completed, and the data is not sent), Closesocket () was called, before we
The general measures taken are "calmly shut down" shutdown (s,sd_both), but the data is definitely lost and how to set the program to meet specific
Application requirements (that is, to let the data not sent out after the socket is closed)?
struct Linger {
U_short L_onoff;
U_short L_linger;
};
Linger M_slinger;
m_slinger.l_onoff=1;//(in the closesocket () call, but there is no data sent when the time allowed to stay)
If m_slinger.l_onoff=0, then function and 2.) function the same;
m_slinger.l_linger=5;//(allow 5 seconds to stay)
SetSockOpt (S,sol_socket,so_linger, (const
char*) &amp;m_slinger,sizeof (linger));
Note:1. In setting a stay delay, for a non-blocking socket is not very useful, preferably not; 2. If you want the program not to experience So_linger need to set So_dontlinger, or set l_onoff=0;

10. Another less used is in the SDI or dialog program, you can record the socket debug information:
(The test of this function has been done before, the mode information can be saved, including the parameters of the socket when it was established, the use of
The specific protocol, and the code of the error can be recorded)
BOOL bdebug=true;
SetSockOpt (S,sol_socket,so_debug, (const char*) &amp;bdebug,sizeof (BOOL));

Linux Network programming Function--address multiplexing setsockopt ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.