Description of functional descriptions:
Gets or sets the options associated with a socket. Options may exist in multi-tier protocols, and they always come out of the top sockets Layer today. When you manipulate socket options, the options are located at the layer and the name of the option must be given. To manipulate the sockets Layer options, you should specify the value of the layer as sol_socket. In order to manipulate the options of other layers, the appropriate protocol number for the control option must be given. For example, to indicate that an option is resolved by the TCP protocol, the layer should be set to protocol number TCP.
How to use:
#include <sys/types.h>
#include <sys/socket.h>
int getsockopt (int sock, int level, int optname, void *optval, socklen_t *optlen);
int setsockopt (int sock, int level, int optname, const void *optval, socklen_t optlen);
Number of references:
Sock: The socket that will be set or get the option.
Level: The protocol layer where the option is located.
Optname: The name of the option that needs to be interviewed.
Optval: For GetSockOpt (), point to the buffer that returns the value of the option. For setsockopt (), point to the buffer that includes the new option value.
Optlen: For GetSockOpt (), the maximum length of the option value as the entry parameter. The actual length of the option value as the export parameter. For setsockopt (), the length of the current option.
Return Description:
When run successfully, returns 0. The failed return -1,errno is set to one of the following values
Ebadf:sock is not a valid document descriptive narrative word
The memory that Efault:optval points to is not a valid process space
EINVAL: Optlen Invalid when calling setsockopt ()
ENOPROTOOPT: The specified protocol layer does not recognize the option
Enotsock:sock description is not a socket
The specific description of the parameters:
level specifies the hierarchy of the control sockets. Can take three kinds of values:
1) Sol_socket: Universal socket option.
2) ipproto_ip:ip option.
3) ipproto_tcp:tcp option.
Optname Specify the mode of control (the name of the option), we explain below
Optval Gets or sets the socket option. Conversion based on the data type of the option name
Option Name Description Data type
========================================================================
Sol_socket
------------------------------------------------------------------------
So_broadcast agree to send broadcast data int
So_debug agree to debug int
So_dontroute does not find route int
So_error Getting socket error int
So_keepalive remain connected int
So_linger delay closing connection struct LINGER
So_oobinline out-of-band data into normal data stream int
SO_RCVBUF Receive buffer size int
SO_SNDBUF Send buffer size int
So_rcvlowat receive buffer lower bound int
So_sndlowat send buffer lower bound int
So_rcvtimeo Receive timeout struct timeval
So_sndtimeo Send timeout struct timeval
So_reuseraddr agree to reuse local address and Port int
So_type Get socket type int
So_bsdcompat compatible with BSD systems int
========================================================================
Ipproto_ip
------------------------------------------------------------------------
Ip_hdrincl include IP header int in packet
Ip_optinos IP Header option int
Ip_tos Service Type
Ip_ttl Time to live int
========================================================================
Ippro_tcp
------------------------------------------------------------------------
Tcp_maxseg the size of the TCP maximum data segment int
Tcp_nodelay does not use the Nagle algorithm int
========================================================================
Return Description:
When run successfully, returns 0. The failed return -1,errno is set to one of the following values
Ebadf:sock is not a valid document descriptive narrative word
The memory that Efault:optval points to is not a valid process space
EINVAL: Optlen Invalid when calling setsockopt ()
ENOPROTOOPT: The specified protocol layer does not recognize the option
Enotsock:sock description is not a socket
So_rcvbuf and So_sndbuf each of the socket interfaces has a send buffer and a receive buffer, using these two sets of interface options to change the default buffer size.
Receive buffers
int nrecvbuf=32*1024; Set to 32K
SetSockOpt (S,sol_socket,so_rcvbuf, (const char*) &nrecvbuf,sizeof (int));
Send buffer
int nsendbuf=32*1024;//set to 32K
SetSockOpt (S,sol_socket,so_sndbuf, (const char*) &nsendbuf,sizeof (int));
Attention:
When setting the TCP socket receive buffer size, the function call order is very important, because the TCP form size option is used to interchange with SYN when establishing the connection. For customers, the O_RCVBUF option must be set before connect, and the SERVER,SO_RCVBUF option must be set before listen.
Combined with the principle description:
1. Each set of interfaces has a send buffer and a receive buffer. The receive buffer is used by TCP and UDP to persist the received data until it is read by the application process. The TCP:TCP notice also has a form size at one end. The TCP socket receive buffer cannot overflow because the other party does not agree to emit data that exceeds the size of the advertised form. This is the traffic control of TCP, assuming that the other party ignores the size of the form and emits more than the size of the form, the receiver TCP discards it. UDP: This datagram is discarded when the received datagram is not loaded into the socket receive buffer. UDP is no traffic control, the fast sender can be very easy to drown the slow receiver, causing the receiver's UDP drop datagram.
2. We often hear about the three handshake of the TCP protocol, but what exactly is the three handshake and what are the details and why do we do it?
First time: Client sends connection request to server,server receive;
Second time: Server returns a confirmation code to the client, with a connection request from server to client, received by the client, confirming the client-to-server connection.
Third time: The client returns the confirmation code of the server's last send request, the server receives it, and confirms the server-to-client connection.
We can see:
1. Each connection to TCP needs to be confirmed.
2. Client-to-server and server-to-client connections are independent.
Let's think about the characteristics of the TCP protocol: connected, reliable, full-duplex, and actually TCP's three-time handshake is just to ensure that these features are implemented.
How to use 3.setsockopt
1.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*) &breuseaddr,sizeof (BOOL));
2. Assume that a soket that is already in the connected state is forced to close after calling Closesocket and does not experience the TIME_WAIT process:
BOOL Bdontlinger = FALSE;
SetSockOpt (S,sol_socket,so_dontlinger, (const char*) &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 time and delivery period:
int NNETTIMEOUT=1000;//1 sec
Delivery time limit
SetSockOpt (Socket,sol_s0cket,so_sndtimeo, (char *) &nnettimeout,sizeof (int));
Receiving time limit
SetSockOpt (Socket,sol_s0cket,so_rcvtimeo, (char *) &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, the ability to 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*) &nrecvbuf,sizeof (int));
Send buffer
int nsendbuf=32*1024;//set to 32K
SetSockOpt (S,sol_socket,so_sndbuf, (const char*) &nsendbuf,sizeof (int));
5. Assume that when sending data, you want to not experience a copy of the system buffer to the socket buffer
Performance of the program:
int nzero=0;
SetSockOpt (Socket,sol_s0cket,so_sndbuf, (char *) &nzero,sizeof (Nzero));
6. Above in recv () complete the above function (by default, the contents of the socket buffer are copied to the system buffer):
int nzero=0;
SetSockOpt (Socket,sol_s0cket,so_rcvbuf, (char *) &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*) &bbroadcast,sizeof (BOOL));
8. In the Client connection server process, it is assumed that the socket in non-clogging mode can set the Connect () delay in the process of connect () until Accpet () is called (This function setting only has a significant effect in the non-clogging process, Not very useful in blocked function calls)
BOOL bconditionalaccept=true;
SetSockOpt (s,sol_socket,so_conditional_accept, (const char*) &bconditionalaccept,sizeof (BOOL));
9. Assume that in the process of sending the data (send () is not complete, and the data is not sent) and call the Closesocket (), once we generally take the measure is "calmly close" shutdown (S,sd_both), but the data is definitely lost, How do I set a program to meet the requirements of a detailed application (that is, to send out the data after it has not been 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 to complete the time allowed to stay)
assume m_slinger.l_onoff=0; then function and 2.) function likewise;
m_slinger.l_linger=5;//(allow 5 seconds to stay)
SetSockOpt (S,sol_socket,so_linger, (const char*) &m_slinger,sizeof (LINGER));
The role of setsockopt