setsockopt () Usage

Source: Internet
Author: User
Tags socket error

Header files: #include <sys/types.h> #include <sys/socket.h>

Define functions: int setsockopt (int s, int level, int optname, const void * optval, Socklen_toptlen);

Function Description:

The s:setsockopt () is used to set the socket state specified by the parameter S.

Level: The parameter level represents the network layer to be set, which is typically set to Sol_socket to access the SOCKET layer. The protocol layer parameters indicate the stack of protocols that we want to access an option. Usually we need to use one of the following:
Sol_socket to access the Socket interface layer option
SOL_TCP to access TCP layer options

Optname: Represents the options you want to set, with the following values:

The parameter optname represents the options you want to set, with the following values:
So_debug turn debug mode on or off
SO_REUSEADDR allows local addresses to be reused during bind ()
So_type returns the socket pattern.
So_error returns the cause of the error that has occurred with the socket
So_dontroute sent packets do not use the routing device to transmit.
So_broadcast transmission using broadcast mode
So_sndbuf Setting the staging area size sent out
So_rcvbuf setting the received staging area size
So_keepalive periodically determines whether the connection has been terminated.
So_oobinline when the OOB data is received, it is sent to the standard input device immediately
So_linger ensures that data is delivered securely and reliably.
Option Name Description Data type
========================================================================
Sol_socket
------------------------------------------------------------------------
So_broadcast allow sending broadcast data int
So_debug Allow 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 allow reuse of local addresses and port int
So_type Get socket type int
So_bsdcompat compatible with BSD systems int
========================================================================
Ipproto_ip
------------------------------------------------------------------------
IP_HDRINCL contains the IP header int in the 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 executed successfully, returns 0. The failed return -1,errno is set to one of the following values
Ebadf:sock is not a valid file description 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 set of interfaces has a send buffer and a receive buffer, using these two sets of interface options to change the default buffer size.


The parameter optval represents the value to set, and the parameter optlen is the length of optval.

Return value: Success returns 0, if there is an error return-1, the reason for the error is stored in errno.

Additional Instructions:
1, EBADF parameter s is not a valid socket processing code
2. Enotsock parameter S is a file description word, non-socket
3. enoprotoopt parameter optname The specified option is incorrect.
4. Efault parameter optval pointer points to memory space that cannot be accessed.



In a TCP connection, a function such as recv defaults to blocking mode (block), that is, until the data arrives, the function does not return, and we sometimes need a timeout mechanism to return it after a certain amount of time, regardless of whether there is data coming, we will use the setsockopt () function:
int setsockopt (int s, int level, int optname, void* optval, socklen_t* Optlen);
Here we relate to a structure:
struct Timeval
{
time_t tv_sec;
time_t tv_usec;
};
Here the first field is in seconds, and the second field is in microseconds.
struct Timeval tv_out;
Tv_out.tv_sec = 1;
tv_out.tv_usec = 0;
Once this structure is populated, we can call this function in the following way:
SetSockOpt (FD, Sol_socket, So_rcvtimeo, &tv_out, sizeof (tv_out));(specific parameters you can have a man.
This allows us to set the timeout mechanism for the recv () function, and recv () returns a value of 0 when the time of the tv_out set is exceeded and no data arrives.

setsockopt () Usage

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.