Role of setsockopt

Source: Internet
Author: User
Tags socket error

Int setsockopt (
Socket s,
Int level,
Int optname,
Const char * optval,
Int optlen
);

S (socket): point to an open set Interface Description
Level: (level): Specifies the option code type.
Sol_socket: basic set of interfaces
Ipproto_ip: IPv4 Interface
Ipproto_ipv6: ipv6 Interface
Ipproto_tcp: TCP interface set
Optname (option name): Option name
Optval (option value): It is a pointer type pointing to a variable: integer, set interface structure, other structure types: Linger {}, timeval {}
Optlen (Option Length): optval size

Function Description:

Obtain or set the options associated with a socket. Options may exist in multi-layer protocols, and they will always appear at the top socket layer. When you operate on socket options, the layer at which the options are located and the name of the options must be provided. To operate the socket layer options, specify the layer value as sol_socket. In order to operate on the options of other layers, the appropriate Protocol Number of the control options must be given. For example, to indicate that an option is parsed by the TCP protocol, the layer should be set to the Protocol Number TCP.




Usage:
# 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 );


Parameters:
Sock: the socket for which the options will be set or obtained.
Level: the protocol layer of the option.
Optname: name of the option to be accessed.
Optval: For getsockopt (), the buffer pointing to the returned option value. For setsockopt (), point to the buffer containing the new option value.
Optlen: the maximum length of the option value when getsockopt () is used as the entry parameter. The actual length of the option value when it is used as an exit parameter. For setsockopt (), the length of the current option.




Return description:




0 is returned when execution is successful. -1 is returned for failure, and errno is set to one of the following values
Ebadf: sock is not a valid file description.
Efault: the memory to which optval points is not a valid process space
Einval: optlen is invalid when setsockopt () is called.
Enoprotoopt: the specified protocol layer does not recognize the option.
Enotsock: Sock does not describe socket




Parameter Details:


Level specifies the level of the control socket. Three values can be obtained:
1) sol_socket: General socket options.
2) ipproto_ip: IP Option.
3) ipproto_tcp: TCP option.
Optname specifies the control method (option name), which is explained in detail below


Optval gets or sets socket options. Convert the Data Type of the Option name




Option Name Description Data Type
========================================================== ======================================
Sol_socket
------------------------------------------------------------------------
So_broadcast allows sending broadcast data int
So_debug allows int debugging
So_dontroute
So_error get socket error int
So_keepalive
So_linger delay disconnection struct linger
So_oobinline put out-of-band data into normal data stream int
So_rcvbuf receive buffer size int
So_sndbuf sending buffer size int
So_rcvlowat lower limit int of the receiving buffer
So_sndlowat lower limit int of the sending Buffer
So_rcvtimeo receiving timeout struct timeval
So_sndtimeo sending timeout struct timeval
So_reuseraddr allows reuse of the local address and port int
So_type: Obtain the socket type Int.
So_bsdcompat is compatible with the BSD system int
========================================================== ======================================
Ipproto_ip
------------------------------------------------------------------------
Ip_hdrincl contains the IP header int in the data package.
Ip_optinos IP header option int
Ip_tos service type
Ip_ttl time int
========================================================== ======================================
Ippro_tcp
------------------------------------------------------------------------
Tcp_maxseg maximum TCP Data Segment Size int
Tcp_nodelay does not use the Nagle algorithm int
========================================================== ======================================


Return description:
0 is returned when execution is successful. -1 is returned for failure, and errno is set to one of the following values
Ebadf: sock is not a valid file description.
Efault: the memory to which optval points is not a valid process space
Einval: optlen is invalid when setsockopt () is called.
Enoprotoopt: the specified protocol layer does not recognize the option.
Enotsock: Sock does not describe socket


Each interface set of so_rcvbuf and so_sndbuf has a sending buffer and a receiving buffer. The default buffer size can be changed using these two Interface Options.


// Receiving buffer
Int nrecvbuf = 32*1024; // set it to 32 K
Setsockopt (S, sol_socket, so_rcvbuf, (const char *) & nrecvbuf, sizeof (INT ));

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.