Socket options for Linux socket programming

Source: Internet
Author: User
Tags set socket socket socket error

The socket mechanism provides two socket option interfaces to control the behavior of sockets. An interface is used to set options, and another interface is used to allow us to request the status of the option. We can get and set three kinds of options.

1. General options that can work on all socket types.

2. Options for managing at the socket level, but depending on the support of the bottom protocol.

3. Protocol options related to each protocol.

Single UNIX specification only defines the options for the sockets Layer (the preceding two items mentioned above)

We can set socket options by using the SetSockOpt function.

#include <sys/socket.h>

int setsockopt (int sockfd, int level, int option, const void *val, socklen_t len);

Return: If 0 is returned successfully, if the error returns 1.

The parameter level is used to distinguish the protocol that option applies to. If option is the universal socket hierarchy, then level is set to Sol_socket. Otherwise level is set to the protocol number that controls option. For example, IPPROTO_TCP is used for TCP options, and IPPROTO_IP for IP options. The following table lists the common socket-level options defined by single UNIX specification.

Socket options

+-------------------------------------------------------------------------------------------------------------+

| Option |                             Type of Val Argument | Description |

|---------------+----------------------+----------------------------------------------------------------------|

| So_acceptconn | int | Returns whether the socket is active for listening (for getsockopt only). |

|---------------+----------------------+----------------------------------------------------------------------|

| So_broadcast | int | If *val is not 0 then broadcast datagram. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_debug | int | If *val is not 0, then the network-driven debugging is activated. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_dontroute | int | If *val is not 0, then the usual route is ignored. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_error | int | Returns and clears the committed socket error (for getsockopt only). |

|---------------+----------------------+----------------------------------------------------------------------|

| so_keepalive | int | If *val is not 0, the message that activates the periodic activity is activated. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_linger | struct Linger | If there are messages that are not sent and the socket is closed, delay. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_oobinline | int | If the *val is not 0, then the Out-of-band data is embedded in the normal data. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_rcvbuf | int | The byte size to receive the cache. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_rcvlowat | int | The minimum data byte returned by the receive call. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_rcvtimeo | struct Timeval | Timeout value for socket receive invocation. |

|---------------+----------------------+----------------------------------------------------------------------|

| so_reuseaddr | int | If *val is not 0, then use the address of bind again. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_sndbuf | int | The byte size in the Send cache. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_sndlowat | int | The minimum amount of data bytes transmitted by the send call at a time. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_sndtimeo | struct Timeval | Timeout value for a socket send call. |

|---------------+----------------------+----------------------------------------------------------------------|

| So_type | int | Identifies the socket type (only in getsockopt). |

+-------------------------------------------------------------------------------------------------------------+

Related Article

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.