Socket programming basics-socket options

Source: Internet
Author: User
Tags set socket

In some cases, we need to further control the socket behavior and attributes, such as modifying the buffer size and viewing the socket status. Therefore, we need to set/obtain the socket options.

1. Obtain socket options
int getsockopt(SOCKET s,int level,int optname,void *optval,int *optlen)
s: Socket Descriptor
level: Option level, includingSOL_SOCKETAndIPPROTO_TCP
optname: Socket option name
optval: Buffer Used to receive socket values
optlen: Buffer size

For example:

int optVal;int optLen=sizeof(optVal);// getsocketoptgetsockopt(ListenSocket,SOL_SOCKET,SO_ACCEPTCONN,(char*)&optVal,&optLen);cout<<"SO_ACCEPTCONN:"<<optVal<<endl;

2. Set socket options
int setsockopt(SOCKET s,int level,int optname,void *optval,int optlen)
s: Socket Descriptor
level: Option level, includingSOL_SOCKETAndIPPROTO_TCP
optname: Socket option name
optval: Buffer used to set the socket Value
optlen: Buffer size

For example:

Bool bopt = true; int boptlen = sizeof (bopt); // getsockopt (listensocket, sol_socket, so_keepalive, (char *) & optval, & optlen) before modification ); cout <"so_keepalive:" <optval <Endl; // setsockoptsetsockopt (listensocket, sol_socket, so_keepalive, (char *) & bopt, boptlen ); // getsockopt (listensocket, sol_socket, so_keepalive, (char *) & optval, & optlen); cout <"so_keepalive:" <optval <Endl;

Socket programming basics-socket options

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.