Setsockopt
1.Closesocket (usually does not close immediately and goes through the time_wait process) and wants to continue to reuse the socket:
Bool breuseaddr = true;
Setsockopt(S, sol_socket, so_reuseaddr, (const char *) & breuseaddr, sizeof (bool ));
2.If you want to force close a soket that is already in the connection status after you call closesocket
Time_wait process:
Bool bdontlinger = false;
Setsockopt(S, sol_socket, so_dontlinger, (const char *) & bdontlinger, sizeof (bool ));
3. In the send () and Recv () processes, the sending and receiving time limit cannot be set due to network conditions or other reasons:
int nnettimeout = 1000; // 1 second
// sending time limit
setsockopt (socket, sol_s0cket, so_sndtimeo, (char *) & nnettimeout, sizeof (INT ));
// receipt time limit
setsockopt (socket, sol_s0cket, so_rcvtimeo, (char *) & nnettimeout, sizeof (INT);
4.When sending (), the returned bytes are actually sent out bytes (synchronized) or bytes sent to the socket buffer.
(Asynchronous); by default, the system sends and receives data in 8688 bytes (about 8.5 KB) at a time.
When receiving a large amount of data, you can set a socket buffer to avoid the continuous cyclic sending and receiving of send () and Recv:
// Receiving buffer
Int nrecvbuf = 32*1024; // set it to 32 K
Setsockopt(S, sol_socket, so_rcvbuf, (const char *) & nrecvbuf, sizeof (INT ));
// Sending Buffer
Int nsendbuf = 32*1024; // set it to 32 K
Setsockopt(S, sol_socket, so_sndbuf, (const char *) & nsendbuf, sizeof (INT ));
5.If you want to avoid the impact of System Buffer copying to socket buffer when sending data
ProgramPerformance:
Int nzero = 0;
Setsockopt(Socket, sol_s0cket, so_sndbuf, (char *) & nzero, sizeof (nzero ));
6.Same as above in Recv () (by default, the socket buffer content is copied to the System Buffer ):
Int nzero = 0;
Setsockopt(Socket, sol_s0cket, so_rcvbuf, (char *) & nzero, sizeof (INT ));
7.Generally, when sending a UDP datagram, you want the data sent by the socket to have the broadcast feature:
Bool bbroadcast = true;
Setsockopt(S, sol_socket, so_broadcast, (const char *) & bbroadcast, sizeof (bool ));
8. during client connection to the server, if a socket in non-blocking mode is in the connect () process, you can
set the connect () latency, until accpet () is called (this function setting only plays a significant role in the non-blocking Process
and does not play a major role in blocked function calls)
bool bconditionalaccept = true;
setsockopt (S, sol_socket, so_conditional_accept, (const char *) & bconditionalaccept, sizeof (bool);
9.If closesocket () is called while sending data (sending () is not completed, and data is not sent ),
The general measure is to "calmly close" Shutdown (S, sd_both), but the data is definitely lost. How to set the program to meet specific requirements?
Application requirements (that is, disable the socket after sending the unsent data )?
Struct linger {
U_short l_onoff;
U_short l_linger;
};
Linger m_slinger;
M_slinger.l_onoff = 1; // (allowed to stay when closesocket () is called, but there is still data not sent)
// If m_slinger.l_onoff = 0, the function is the same as 2;
M_slinger.l_linger = 5; // (the allowable stay time is 5 seconds)
Setsockopt(S, sol_socket, so_linger, (const char *) & m_slinger, sizeof (linger ));
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// /////////////////////////////////////////
Set Interface Options.
# Include <Winsock. h>
Int Pascal farSetsockopt(Socket S, int level, int optname,
Const char far * optval, int optlen );
S: Specifies the description of a set of interfaces.
Level: The level defined by the option. Currently, only sol_socket and ipproto_tcp layers are supported.
Optname: the option to be set.
Optval: pointer, pointing to the buffer that stores option values.
Optlen: the length of the optval buffer.
Note:
Setsockopt () Function is used to set the option value for any type and any status set interface. Although there are options on different protocol layers, this function only defines the options at the highest "set interface" level. This option affects operations on the set interface, such as whether the expedited data is received in a common data stream, and whether the broadcast data can be sent from the set interface.
There are two sets of Interface Options: one is a Boolean option that allows or disables a feature, and the other is an integer or structure option. If a Boolean option is allowed, optval is directed to a non-zero integer. optval is not allowed to point to an integer equal to zero. For Boolean options, optlen should be equal to sizeof (INT); for other options, optval points to the integer number or structure containing the required options, and optlen is the length of the integer number or structure. The so_linger option is used to control actions in the following situations: the interface has queued data to be sent, and the closesocket () call has been executed. See the impact of the so_linger option on closesocket () semantics in the closesocket () function. The application creates a linger structure to set the corresponding operation features:
Struct linger {
Int l_onoff;
Int l_linger;
};
In order to allow so_linger, the application should set l_onoff to a non-zero value, set l_linger to zero or the required timeout value (in seconds), and then call Setsockopt (). To allow so_dontlinger (that is, to disable so_linger), l_onoff should be set to zero and then call Setsockopt ().
By default, a set of interfaces cannot be bound with a local address in use (see BIND ()). But sometimes it is necessary to "reuse" the address. Because each connection is uniquely determined by the combination of the local address and the remote address, as long as the remote address is different, binding two sets of interfaces with one address is not a major problem. To notify the implementation of Windows interface sets, do not bind an address to another interface because it has been used by one interface set. The application can set the so_reuseaddr option before BIND () is called. Note that this option is interpreted only when BIND () is called. Therefore, you do not need (but are harmless) to set this option for a set of APIs that do not share addresses, or in BIND () set or clear this option without affecting this or other sets of interfaces.
An application can enable the so_keepalive option to enable the "keep active" package for Windows interfaces over TCP connections. The implementation of a Windows interface does not need to support "persistence activity", but if it is supported, the specific semantics will be related to the implementation, rfc1122 "Internet host requirements-communication layer" section 4.2.3.6 shall be observed. If the connection fails due to "active", any call to this interface in progress will be returned with the wsaenetreset error. Any subsequent call will be returned with the wsaenotconn error.
Disable Nagle with the tcp_nodelay Option Algorithm . The Nagle algorithm saves unconfirmed data into the buffer until a packet is sent together to reduce the number of small packets sent by the host. However, for some applications, this algorithm will reduce system performance. Therefore, tcp_nodelay can be used to disable this algorithm. The tcp_nodelay option is set only when the application writer has a definite understanding of its effect and is required, because it has a significant negative impact on network performance. Tcp_nodelay is the only option to use the ipproto_tcp layer. All other options use the sol_socket layer.
If the so_debug option is set, the Windows interface provider is encouraged (but not required) to provide the corresponding debugging information. However, the mechanism for generating debugging information and the form of debugging information are beyond the scope of this specification.
Setsockopt () The following options are supported. "Type" indicates the data type specified by optval.
Option type meaning
So_broadcast bool allows an interface to transmit broadcast information.
So_debug bool records debugging information.
So_dontliner bool should not block or close the operation because the data is not sent. Setting this option is equivalent to setting the l_onoff element of so_linger to zero.
So_dontroute bool disallows path selection; direct transfer.
So_keepalive bool sends the "keep active" package.
So_linger struct linger far * stays if no data is sent when it is disabled.
So_oobinline bool receives out-of-band data in a conventional data stream.
So_rcvbuf int determines the buffer size for receiving.
So_reuseaddr bool allows you to bind an interface with an existing address (see BIND ()).
So_sndbuf int specifies the size of the sending buffer.
Tcp_nodelay bool prohibits the combined Nagle algorithm from being sent.
Setsockopt() Unsupported BSD options include:
Option name type meaning
The so_acceptconn bool interface is monitored.
So_error INT: Get the error status and clear it.
So_rcvlowat int receives low-level watermarks.
So_rcvtimeo int timeout.
So_sndlowat int sends a low-level watermark.
So_sndtimeo int timeout.
So_type int set interface type.
Set ip_options in the IP header.
Return Value:
If no error occurs,Setsockopt() Returns 0. Otherwise, the socket_error is returned. The application can obtain the corresponding error through wsagetlasterror ().Code.
Error code:
Wsanotinitialised: before using this API, you must successfully call wsastartup ().
Wsaenetdown: A Windows interface is used to detect the failure of the network subsystem.
Wsaefault: optval is not a valid part of the process address space.
Wsaeinss SS: A blocked Windows interface call is running.
Wsaeinval: The level value is invalid, or the information in optval is invalid.
Wsaenetreset: the connection times out when so_keepalive is set.
Wsaenoprotoopt: unknown or not supported. Sock_stream APIs do not support the so_broadcast option. sock_dgram APIs do not support the so_dontlinger, so_keepalive, so_linger, and so_oobinline options.
Wsaenotconn: When so_keepalive is set, the connection is reset.
Wsaenotsock: The description is not a set of interfaces.
See:
BIND (), getsockopt (), ioctlsocket (), socket (), wsaasyncselect ().
Copy From: http://blog.csdn.net/qinmi/archive/2007/03/07/1523081.aspx