Linux network programming-8. socket options

Source: Internet
Author: User
Tags socket error
Article title: Linux network programming-8. socket options. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Sometimes we need to control the socket behavior (such as modifying the buffer size). at this time, we need to control the socket options.
  
8.1 getsockopt and setsockopt
  
Int getsockopt (int sockfd, int level, int optname, void * optval, socklen_t * optlen)
Int setsockopt (int sockfd, int level, int optname, const void * optval, socklen_t * optlen)
  
Level specifies the level of the control socket. three values can be obtained: 1) SOL_SOCKET: General socket options. 2) IPPROTO_IP: IP options. 3) IPPROTO_TCP: TCP options.
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
========================================================== ======================================
  
For more information about these options, see Linux Programmer's Manual
  
8.2 ioctl
Ioctl can control all file descriptors. here we will introduce the options for controlling sockets.
  
Int ioctl (int fd, int req ,...)
  
========================================================== ========================================
Ioctl control options
--------------------------------------------------------------------------
Whether the SIOCATMARK has reached the out-of-band mark int
FIOASYNC asynchronous input/output flag int
Number of bytes readable in the FIONREAD buffer int
========================================================== ========================================
  
Use man ioctl_list to view detailed options.
  
  
  
  
  
  
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.