赞
踩
setsockopt( ) - set socket options
STATUS setsockopt ( int s, /* target socket */ int level, /* protocol level of option */ int optname, /* option name */ char *optval, /* pointer to option value */ int optlen /* option length */ )
This routine sets the options associated with a socket. To manipulate options at the "socket" level, level should be SOL_SOCKET. Any other levels should use the appropriate protocol number.
The following sections discuss the socket options available for stream sockets.
Although SO_LINGER is a socket-level option, it presently affects only the TCP and SCTP protocols. The SO_LINGER option is used to specify whether they should perform a "graceful" close. The first part of this section deals primarily with TCP while the SCTP specifics are at the end of the section.
setsockopt (sock, SOL_SOCKET, SO_LINGER, &optval, sizeof (optval));For a "graceful" close in response to the shutdown of a connection, TCP tries to make sure that all the unacknowledged data in the transmission channel are acknowledged, and the peer is shut down properly, by going through an elaborate set of state transitions.
The value at optval indicates the amount of time to linger if there is unacknowledged data, using struct linger, defined in the header sys/socket.h. The lingerstructure has two members: l_onoff and l_linger. l_onoff can be set to 1 to turn on the SO_LINGER option, or set to 0 to turn off the SO_LINGER option. l_lingerspecifies a time-out value in seconds.
The default behavior (SO_LINGER disabled, l_onoff = 0) is for a close( ) operation on a socket to return immediately without waiting for outstanding data to be acknowledged by the peer. However, although close( ) returns immediately, TCP does perform a graceful close on the endpoint, retransmitting unacknowledged data and the final FIN as necessary until they have been acknowledged, or until the connection times out. Since the socket has been closed, the application cannot know in this case whether all data were eventually successfully delivered to the peer.
When SO_LINGER is enabled (l_onoff = 1), the behavior upon a close( ) depends upon the value of l_linger. If l_linger is zero, then instead of performing a graceful close, TCP aborts the connection, sending a RST segment to the peer. On the other hand, if l_linger is non-zero, TCP performs a graceful close, and theclose( ) call blocks until either the peer acknowledges any outstanding data and the final FIN segment from the local side, or until the connection times out, or until the timeout specified in l_linger expires, whichever occurs first. Negative or excessively large values of l_linger are treated as infinite time-outs. (In prior VxWorks releases, l_linger was significant only in whether it was zero or non-zero; if non-zero, it was effectively treated as an infinite time-out. This traditional behavior may be restored by defining the configuration parameter SO_LINGER_INIFINITE_CFG of the INCLUDE_BSD_SOCKET component to be TRUE.)
If SO_LINGER is enabled and the time-out specified by l_linger expires before the local side's FIN is acknowledged, the close( ) call will return ERROR with errno set to ETIMEDOUT; the socket has nevertheless been closed. In this case, if the SO_LINGERRESET socket option is enabled, then the local side will abort the connection by sending a TCP RST to the peer. Otherwise, if SO_LINGERRESET is not enabled, the local TCP continues with the graceful shutdown attempt.
A special case applies when the SO_LINGER option is applied to a listening endpoint. In this case, the SO_LINGER option is inherited by the accepted child sockets of the listening parent; as is the parent's l_linger value, if it is nonzero. However, if the parent's l_linger value is zero, the child sockets are given the default value TCP_LINGERTIME (specified in netinet/tcp_timer.h) rather than zero. A child endpoint must explicitly use setsockopt( ) to set its linger time to zero if it wants a close( ) call to abort its connection.
For SCTP the processing is similar to TCP. The option is enabled by setting l_onoff to 1. If the l_linger value is then set to 0 calling close will result in an ABORT instead of a graceful shutdown. If l_linger is set to a positive value a graceful close will be attempted. If it does not complete within the period specified by l_linger the close( ) call will return but the graceful shutdown will continue.
The following sections discuss the socket options available for TCP (stream) sockets.
Specify the SO_KEEPALIVE option to make the transport protocol (TCP) initiate a timer to detect a dead connection:
setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof (optval));This prevents an application from hanging on an invalid connection. The value at optval for this option is an integer (type int), either 1 (on) or 0 (off).
The integrity of a connection is verified by transmitting zero-length TCP segments triggered by a timer, to force a response from a peer node. If the peer does not respond after repeated transmissions of the KEEPALIVE segments, the connection is dropped, all protocol data structures are reclaimed, and processes sleeping on the connection are awakened with an ETIMEDOUT error.
The ETIMEDOUT timeout can happen in two ways. If the connection is not yet established, the KEEPALIVE timer expires after idling for TCPTV_KEEP_INIT. If the connection is established, the KEEPALIVE timer starts up when there is no traffic for TCPTV_KEEP_IDLE. If no response is received from the peer after sending the KEEPALIVE segment TCPTV_KEEPCNT times with interval TCPTV_KEEPINTVL, TCP assumes that the connection is invalid. The parametersTCPTV_KEEP_INIT, TCPTV_KEEP_IDLE, TCPTV_KEEPCNT, and TCPTV_KEEPINTVL are defined in the file target/h/net/tcp_timer.h.
Although SO_LINGERRESET is a socket-level option, it presently affects only the TCP protocol. This socket option has effect only if the SO_LINGER option is enabled with a non-zero linger time, close( ) is called, and the linger time expires before all outstanding data and the final FIN are acknowledged. In this case, ifSO_LINGERRESET has been enabled (integer optval nonzero) the local side will abort the connection, sending a reset segment to the peer. Otherwise, TCP continues with the graceful close attempt after close( ) returns. By default, SO_LINGERRESET is disabled. To enable it:
int optval = 1; setsockopt (sock, SOL_SOCKET, SO_LINGERRESET, &optval, sizeof (optval));See the discussion of SO_LINGER.
Specify the TCP_NODELAY option for real-time protocols, such as the X Window System Protocol, that require immediate delivery of many small messages:
setsockopt (sock, IPPROTO_TCP, TCP_NODELAY, &optval, sizeof (optval));The value at optval is an integer (type int) set to either 1 (on) or 0 (off).
By default, the VxWorks TCP implementation employs an algorithm that attempts to avoid the congestion that can be produced by a large number of small TCP segments. This typically arises with virtual terminal applications (such as telnet or rlogin) across networks that have low bandwidth and long delays. The algorithm attempts to have no more than one outstanding unacknowledged segment in the transmission channel while queueing up the rest of the smaller segments for later transmission. Another segment is sent only if enough new data is available to make up a maximum sized segment, or if the outstanding data is acknowledged.
This congestion-avoidance algorithm works well for virtual terminal protocols and bulk data transfer protocols such as FTP without any noticeable side effects. However, real-time protocols that require immediate delivery of many small messages, such as the X Window System Protocol, need to defeat this facility to guarantee proper responsiveness in their operation.
TCP_NODELAY is a mechanism to turn off the use of this algorithm. If this option is turned on and there is data to be sent out, TCP bypasses the congestion-avoidance algorithm: any available data segments are sent out if there is enough space in the send window.
Specify the TCP_MAXSEG option to decrease the maximum allowable size of an outgoing TCP segment. This option cannot be used to increase the MSS.
setsockopt (sock, IPPROTO_TCP, TCP_MAXSEG, &optval, sizeof (optval));The value at optval is an integer set to the desired MSS (eg. 1024).
When a TCP socket is created, the MSS is initialized to the default MSS value which is determined by the configuration parameter TCP_MSS_DFLT (512 by default). When a connection request is received from the other end with an MSS option, the MSS is modified depending on the value of the received MSS and on the results of Path MTU Discovery (which is enabled by default). The MSS may be set as high as the outgoing interface MTU (1460 for an Ethernet). Therefore, after a call to socket but before a connection is established, an application can only decrease the MSS from its default of 512. After a connection is established, the application can decrease the MSS from whatever value was selected.
Specify the SO_DEBUG option to let the underlying protocol module record debug information.
setsockopt (sock, SOL_SOCKET, SO_DEBUG, &optval, sizeof (optval));The value at optval for this option is an integer (type int), either 1 (on) or 0 (off).
The following section discusses an option for datagram (UDP) sockets.
Specify the SO_BROADCAST option when an application needs to send data to a broadcast address.
setsockopt (sock, SOL_SOCKET, SO_BROADCAST, &optval, sizeof (optval));The value at optval is an integer (type int), either 1 (on) or 0 (off).
The following section discusses options for RAW sockets.
Enable/disable checksum computation on IPv6 packets.
setsockopt (sock, IPPROTO_IPV6, IPV6_CHECKSUM, &optval, sizeof (optval));The value optval is an integer (type int). A value of -1 (default) causes checksum computation to be skipped (except for ICMPv6 packets for which the checksum is always computed). Any other value will result in the checksum being computed and inserted in outgoing packets and also verified for incoming packets. optvalspecifies an offset into the user data where the checksum is located.
Allows filtering ICMPv6 packets received on a raw socket based on the ICMPv6 type field.
setsockopt (sock, IPPROTO_ICMPV6, ICMP6_FILTER, (char *) &filter, sizeof (filter));filter is of type struct icmp6_filter and it defines the filter for the socket. Various macros such as ICMP6_FILTER_SETPASS and ICMP6_FILTER_SETBLOCK are available to configure the filter to accept or reject specific ICMPv6 packets. When a raw socket is created, it will pass all ICMPv6 message types to the application by default.
The following section discusses options for multicasting on UDP and RAW sockets.
Specify the IP_ADD_MEMBERSHIP option when a process needs to join multicast group:
setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&ipMreq, sizeof (ipMreq));The value of ipMreq is an ip_mreq structure. ipMreq.imr_multiaddr.s_addr is the internet multicast address ipMreq.imr_interface.s_addr is the internet unicast address of the interface through which the multicast packet needs to pass.
Specify the IP_DROP_MEMBERSHIP option when a process needs to leave a previously joined multicast group:
setsockopt (sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&ipMreq, sizeof (ipMreq));The value of ipMreq is an ip_mreq structure. ipMreq.imr_multiaddr.s_addr is the internet multicast address. ipMreq.imr_interface.s_addr is the internet unicast address of the interface to which the multicast address was bound.
Specify the IPV6_JOIN_GROUP option when an application needs to join an IPv6 multicast group:
setsockopt (sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *) &ip6Mreq, sizeof (ip6Mreq));ip6Mreq is an ipv6_mreq structure. ip6Mreq.ipv6mr_multiaddr.s6_addr is the IPv6 multicast address. ip6Mreq.ipv6mr_interface is the interface index of the interface through which the multicast packet needs to pass. A value of 0 specifies the default multicast interface.
Specify the IPV6_LEAVE_GROUP option when an application needs to join an IPv6 multicast group:
setsockopt (sock, IPPROTO_IPV6, IPV6_LEAVE_GROUP, (char *) &ip6Mreq, sizeof (ip6Mreq));ip6Mreq is an ipv6_mreq structure. ip6Mreq.ipv6mr_multiaddr.s6_addr is the IPv6 multicast address. ip6Mreq.ipv6mr_interface is the interface index of the interface to which the multicast address was bound.
Specify the IP_MULTICAST_IF option when an application needs to specify an outgoing network interface through which all multicast packets are sent:
setsockopt (sock, IPPROTO_IP, IP_MULTICAST_IF, (char *)&ifAddr, sizeof (mCastAddr));The value of ifAddr is an in_addr structure. ifAddr.s_addr is the internet network interface address.
Specify the IPV6_MULTICAST_IF option when an application needs to specify an outgoing network interface through which all IPv6 multicast packets are sent:
setsockopt (sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, (char *) &outIfIdx, sizeof (outIfIdx));outIfIdx is the interface index of the desired interface and is an unsigned integer (type unsigned int).
Specify the IP_MULTICAST_TTL option when an application needs to select a default TTL (time to live) for outgoing multicast packets:
setsockopt (sock, IPPROTO_IP, IP_MULTICAST_TTL, &optval, sizeof(optval));The value at optval is an integer (type int), time to live value.
optval(TTL) Application Scope
0 same interface 1 same subnet 31 local event video 32 same site 63 local event audio 64 same region 95 IETF channel 2 video 127 IETF channel 1 video 128 same continent 159 IETF channel 2 audio 191 IETF channel 1 audio 223 IETF channel 2 low-rate audio 255 IETF channel 1 low-rate audio unrestricted in scope
Specify the IPV6_MULTICAST_HOPS option when an application needs to select a default hoplimit for outgoing IPv6 multicast packets:
setsockopt (sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &optval, sizeof (optval));The value at optval is an integer (type int), which specifies the default hoplimit.
Enable or disable loopback of outgoing multicasts.
setsockopt (sock, IPPROTO_IP, IP_MULTICAST_LOOP, &optval, sizeof(optval));The value at optval is an integer (type int), either 1(on) or 0 (off).
Enable or disable loopback of outgoing IPv6 multicasts.
setsockopt (sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &optval, sizeof (optval));The value at optval is an unsigned integer (type unsigned int), either 1 (on) or 0 (off).
Allows the application to receive interface information about incoming packets.
setsockopt (sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &optval, sizeof (optval));Here optval is a boolean (type int). When it is set to 1, the application can receive the destination IPv6 address and arriving interface index using recvmsg( ). This information is available in a struct in6_pktinfo which is part of a control structure with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_PKTINFO.
Allows the application to receive packet hop-limit information.
setsockopt (sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval, sizeof (optval));Here optval is a boolean (type int). Setting it to 1 will make the packet hoplimit value available to the user in the form of socket control information usingrecvmsg( ). The hoplimit will be returned as an integer in a control mbuf with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_HOPLIMIT.
Allows the application to receive hop-by-hop options for an incoming IPv6 packet.
setsockopt (sock, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &optval, sizeof (optval));Here optval is a boolean (type int). Setting it to 1 will make the packet hop-by-hop options available to the user in the form of socket control information usingrecvmsg( ). This information will be returned in a control mbuf with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_HOPOPTS.
Allows the application to receive destination options for an incoming IPv6 packet.
setsockopt (sock, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &optval, sizeof (optval));Here optval is a boolean (type int). Setting it to 1 will make the packet destination options available to the user in the form of socket control information usingrecvmsg( ). This information will be returned in a control mbuf with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_DSTOPTS.
Allows the application to receive the traffic class for an incoming IPv6 packet.
setsockopt (sock, IPPROTO_IPV6, IPV6_RECVTCLASS, &optval, sizeof (optval));Here optval is a boolean (type int). Setting it to 1 will make the traffic class available to the application in the form of socket control information using recvmsg( ). This information will be returned in a control mbuf with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_TCLASS.
Allows the application to receive the routing header for an incoming IPv6 packet.
setsockopt (sock, IPPROTO_IPV6, IPV6_RECVRTHDR, &optval, sizeof (optval));Here optval is a boolean (type int). Setting it to 1 will make the packet routing header available to the user in the form of socket control information usingrecvmsg( ). This information will be returned in a control mbuf with cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_RTHDR.
Determine the path MTU for PMTU discovery.
setsockopt (sock, IPPROTO_IPV6, IPV6_RECVPATHMTU, &optval, sizeof (optval));Here optval is an integer (type int). Setting it to 1 will enable this option. The MTU information is received using the ancillary data item IPV6_PATHMTU which is delivered to recvmsg( ) without any actual data.
When the application is sending packets too big for the path MTU recvmsg( ) will return zero (indicating no data) but there will be a cmsghdr with cmsg_type set to IPV6_PATHMTU, and cmsg_len will indicate that cmsg_data is sizeof(struct ip6_mtuinfo) bytes long. This can happen when the sending node receives a corresponding ICMPv6 packet too big error, or when the packet is sent from a socket with the IPV6_DONTFRAG option being on and the packet size is larger than the MTU of the outgoing interface.
Prevents automatic insertion of fragment header while sending.
setsockopt (sock, IPPROTO_IPV6, IPV6_DONTFRAG, &optval, sizeof (optval));Here optval is an integer (type int). Setting it to 1 will turn off the automatic insertion of a fragment header for UDP and raw sockets if the packet is too large for the path MTU. This can be useful for applications like traceroute which want to avoid fragmentation to discover the actual path MTU. When the data size is larger than the MTU of the outgoing interface, the packet will be discarded. Applications can know the result by enabling the IPV6_RECVPATHMTU option and receiving the corresponding ancillary data items.
This option can also be sent as ancillary data. In the cmsghdr structure containing this ancillary data, the cmsg_level member will be IPPROTO_IPV6, the cmsg_type member will be IPV6_DONTFRAG, and the first byte of cmsg_data[] will be the first byte of the integer.
Allows the application to specify the next hop for the datagram.
setsockopt (sock, IPPROTO_IPV6, IPV6_NEXTHOP, &addr, sizeof (addr));Here addr is a structure of type sockaddr. It contains the address of the next hop for the outgoing IPv6 packet.
An alternative to using this option is to specify this information as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal toIPPROTO_IPV6 and cmsg_type equal to IPV6_NEXTHOP.
Allows the application to specify the traffic class for the outgoing IPv6 packet.
setsockopt (sock, IPPROTO_IPV6, IPV6_TCLASS, &tclass, sizeof (tclass));Here tclass is an unsigned integer (type u_int).
An alternative to using this option is to specify this information as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal toIPPROTO_IPV6 and cmsg_type equal to IPV6_TCLASS.
Allows the application to specify a routing header for the outgoing IPv6 packet.
setsockopt (sock, IPPROTO_IPV6, IPV6_RTHDR, &ip6rth, sizeof (ip6rth));Here ip6rth is a structure of type ip6_rthdr. It contains the routing header that should be sent as part of every IPv6 packet. Specifying an option length of 0 will turn off this option.
An alternative to using this option is to send the destination options as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_RTHDR.
Allows the application to send hop-by-hop options.
setsockopt (sock, IPPROTO_IPV6, IPV6_HOPOPTS, &ip6hbh, sizeof (ip6hbh));Here ip6hbh is a structure of type ip6_hbh. It contains the hop-by-hop options that should be sent as part of every IPv6 packet. Specifying an option length of 0 will turn off this option.
An alternative to using this option is to send the destination options as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_HOPOPTS.
Allows the application to send destination options that precede a routing header.
setsockopt (sock, IPPROTO_IPV6, IPV6_RTHDRDSTOPTS, &ip6dst, sizeof (ip6dst));Here ip6dst is a structure of type ip6_dest. It contains the destination options that should be sent as part of every IPv6 packet. Specifying an option length of 0 will turn off this option.
An alternative to using this option is to send the destination options as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_RTHDRDSTOPTS.
Allows the application to send destination options that follow a routing header.
setsockopt (sock, IPPROTO_IPV6, IPV6_DSTOPTS, &ip6dst, sizeof (ip6dst));Here ip6dst is a structure of type ip6_dest. It contains the destination options that should be sent as part of every IPv6 packet. Specifying an option length of 0 will turn off this option. This option is also used when there is no routing header.
An alternative to using this option is to send the destination options as ancillary data using the sendmsg( ) call. The control mbuf should have cmsg_level equal to IPPROTO_IPV6 and cmsg_type equal to IPV6_DSTOPTS.
The following section discusses options for RAW, DGRAM or STREAM sockets.
Sets the IP options sent from this socket with every packet.
setsockopt (sock, IPPROTO_IP, IP_OPTIONS, optbuf, optbuflen);Here optbuf is a buffer containing the options.
Sets the Type-Of-Service field for each packet sent from this socket.
setsockopt (sock, IPPROTO_IP, IP_TOS, &optval, sizeof(optval));Here optval is an integer (type int). This integer can be set to IPTOS_LOWDELAY, IPTOS_THROUGHPUT, IPTOS_RELIABILITY, or IPTOS_MINCOST, to indicate how the packets sent on this socket should be prioritized.
Sets the Time-To-Live field for each packet sent from this socket.
setsockopt (sock, IPPROTO_IP, IP_TTL, &optval, sizeof(optval));Here optval is an integer (type int), indicating the number of hops a packet can take before it is discarded.
Sets the hop-limit field for each packet sent from this socket.
setsockopt (sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &optval, sizeof (optval));Here optval is an integer (type int), indicating the number of hops a packet can take before it is discarded.
This option is only available for AF_INET6 sockets. It is similar to the IP_TTL option for AF_INET sockets. Unless the IPV6_V6ONLY option has been set on this socket, this operation also sets the outgoing TTL for the socket since the socket also supports IPv4 and IPv4-mapped addresses.
Sets whether or not reversed source route queueing will be enabled for incoming datagrams. (Not implemented)
setsockopt (sock, IPPROTO_IP, IP_RECVRETOPTS, &optval, sizeof(optval));Here optval is a boolean (type int). However, this option is currently not implemented, so setting it will not change the behavior of the system.
Sets whether or not the socket will receive the IP address of the destination of an incoming datagram in control data.
setsockopt (sock, IPPROTO_IP, IP_RECVDSTADDR, &optval, sizeof(optval));Here optval is a boolean (type int).
Allows the application to control how ephemeral ports are allocated for AF_INET sockets.
setsockopt (sock, IPPROTO_IP, IP_PORTRANGE, &optval, sizeof (optval));Here optval is an integer (type int) which determines which range of ports will be used. Acceptable values are IP_PORTRANGE_DEFAULT (default range of 1024-5000), IP_PORTRANGE_HIGH (49152-65535) and IP_PORTRANGE_LOW (600-1023). All of these range values can be modified using Sysctl.
Allows the application to control how ephemeral ports are allocated for AF_INET6 sockets.
setsockopt (sock, IPPROTO_IPV6, IPV6_PORTRANGE, &optval, sizeof (optval));Here optval is an integer (type int) which determines which range of ports will be used. Acceptable values are IPV6_PORTRANGE_DEFAULT (default range of 1024-5000), IPV6_PORTRANGE_HIGH (49152-65535) and IPV6_PORTRANGE_LOW (600-1023). All of these range values can be modified using Sysctl.
Enables/disables support for receiving IPv4 traffic on an AF_INET6 socket.
setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &optval, sizeof (optval));Here optval is a boolean (type int). Setting it to 1 causes the socket to receive IPv6 traffic only. Setting it to 0 (default) restores support for IPv4 packets (using IPv4-mapped addresses). This call must be made before a call to bind.
Allows an application to avoid Path MTU Discovery on the socket.
setsockopt (sock, IPPROTO_IPV6, IPV6_USE_MIN_MTU, &optval, sizeof (optval));The value at optval is an integer (type int). If the value is -1, PMTU discovery is performed for unicast destinations but not for multicast. If the value is 0, PMTU discovery is always done. A value of 1 indicates that PMTU discovery should always be performed.
PMTU discovery is avoided by sending at the minimum IPv6 MTU. If the packet is larger than the minimum MTU and this feature has been enabled the IP layer will fragment to the minimum MTU.
This option can also be sent as ancillary data. In the cmsghdr structure containing this ancillary data, the cmsg_level member will be IPPROTO_IPV6, the cmsg_type member will be IPV6_USE_MIN_MTU, and the first byte of cmsg_data[] will be the first byte of the integer.
The following sections describe options that can be used with either stream or datagram sockets.
Specify the SO_REUSEADDR option to bind a stream socket to a local port that may be still bound to another stream socket:
setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (optval));The value at optval is an integer (type int), either 1 (on) or 0 (off).
When the SO_REUSEADDR option is turned on, applications may bind a stream socket to a local port even if it is still bound to another stream socket, if that other socket is associated with a "zombie" protocol control block context not yet freed from previous sessions. The uniqueness of port number combinations for each connection is still preserved through sanity checks performed at actual connection setup time. If this option is not turned on and an application attempts to bind to a port which is being used by a zombie protocol control block, the bind( ) call fails.
This option is similar to the SO_REUSEADDR option but it allows binding to the same local address and port combination.
setsockopt (sock, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof (optval));The value at optval is an integer (type int), either 1 (on) or 0 (off).
The SO_REUSEPORT option is mainly required by multicast applications where a number of applications need to bind to the same multicast address and port to receive multicast data. Unlike SO_REUSEADDR where only the later applications need to set this option, with SO_REUSEPORT all applications including the first to bind to the port are required to set this option. For multicast addresses SO_REUSEADDR and SO_REUSEPORT show the same behavior so SO_REUSEADDRcan be used instead.
Specify the SO_OOBINLINE option to place urgent data within the normal receive data stream:
setsockopt (sock, SOL_SOCKET, SO_OOBINLINE, &optval, sizeof (optval));TCP provides an expedited data service which does not conform to the normal constraints of sequencing and flow control of data streams. The expedited service delivers "out-of-band" (urgent) data ahead of other "normal" data to provide interrupt-like services (for example, when you hit a CTRL-C during telnet or rlogin session while data is being displayed on the screen.)
TCP does not actually maintain a separate stream to support the urgent data. Instead, urgent data delivery is implemented as a pointer (in the TCP header) which points to the sequence number of the octet following the urgent data. If more than one transmission of urgent data is received from the peer, they are all put into the normal stream. This is intended for applications that cannot afford to miss out on any urgent data but are usually too slow to respond to them promptly.
Specify the SO_SNDBUF option to adjust the maximum size of the socket-level send buffer:
setsockopt (sock, SOL_SOCKET, SO_SNDBUF, &optval, sizeof (optval));The value at optval is an integer (type int) that specifies the size of the socket-level send buffer to be allocated.
When stream, datagram or sequential packet sockets are created, each transport protocol reserves a set amount of space at the socket level for use when the sockets are attached to a protocol. For TCP, the default size of the send buffer is 8192 bytes. For UDP, the default size of the send buffer is 9216 bytes. For COMP, it is 64kbytes. Socket-level buffers are allocated dynamically from the mbuf pool.
The effect of setting the maximum size of buffers (for both SO_SNDBUF and SO_RCVBUF, described below) is not actually to allocate the mbufs from the mbuf pool, but to set the high-water mark in the protocol data structure which is used later to limit the amount of mbuf allocation. Thus, the maximum size specified for the socket level send and receive buffers can affect the performance of bulk data transfers. For example, the size of the TCP receive windows is limited by the remaining socket-level buffer space. These parameters must be adjusted to produce the optimal result for a given application.
Specify the SO_RCVBUF option to adjust the maximum size of the socket-level receive buffer:
setsockopt (sock, SOL_SOCKET, SO_RCVBUF, &optval, sizeof (optval));The value at optval is an integer (type int) that specifies the size of the socket-level receive buffer to be allocated.
When stream, datagram or sequential packet sockets are created, each transport protocol reserves a set amount of space at the socket level for use when the sockets are attached to a protocol. For TCP, the default size is 8192 bytes. UDP reserves 41600 bytes, enough space for up to forty incoming datagrams (1 Kbyte each). COMP reserves 64kbytes, which can be any arrangement of packets of any size (64k one-byte packets to one 64kbytes packet).
See the SO_SNDBUF discussion above for a discussion of the impact of buffer size on application performance.
Specify the SO_SNDTIMEO option to set a timeout value for output operations:
setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO, &optval, sizeof (optval));The value at optval is a timeval structure with the number of seconds and microseconds used to limit waits for output operations to complete. If a send operation has blocked for this much time, it returns with a partial count or with the error EWOULDBLOCK if no data was sent. The timer is restarted each time additional data is delivered to the protocol.
Specify the SO_RCVTIMEO option to set a timeout value for input operations:
setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO, &optval, sizeof (optval));The value at optval is a timeval structure with the number of seconds and microseconds used to limit waits for input operations to complete. If a receive operation has blocked for this much time, it returns with a short count or with the error EWOULDBLOCK if no data was received. The timer is restarted each time additional data is received from the protocol. Thus, the limit is, in effect, an inactivity timer.
The following sections discuss the socket options available for SCTP (stream) sockets.
Specify the SCTP_RTOINFO option to tune the retransmission parameters for SCTP.
setsockopt (sock, IPPROTO_SCTP, SCTP_RTOINFO, (char *)&sctpRTOInfo, sizeof(sctpRTOInfo));sctpRTOInfo is a sctp_rtoinfo structure. Parameters to initialize, and bound retransmission timeouts (RTO) can be set using this option.
Specify the SCTP_ASSOCINFO option to set association and endpoint parameters.
setsockopt (sock, IPPROTO_SCTP, SCTP_ASSOCINFO, (char *)&sctpAssocInfo, sizeof(sctpAssocInfo));sctpAssocInfo is a sctp_assocparams structure.
Specify the SCTP_INITMSG option to set default association initialization parameters.
setsockopt (sock, IPPROTO_SCTP, SCTP_INITMSG, (char *)&sctpInitMsg, sizeof(sctpInitMsg));sctpInitMsg is a sctp_initmsg structure. This option allows the values used when creating a new association to be set.
Specify the SCTP_NODELAY option to enable or disable any Nagle like algorithm.
setsockopt (sock, IPPROTO_SCTP, SCTP_NODELAY, (char *)&optval, sizeof(optval));optval is an integer (type int), either 1 (on) or 0 (off). If delays are disabled messages will be sent as soon as possible without adding any delays. If delays are allowed messages may be delayed slightly in order to consolidate them into a smaller number of packets.
Specify the SCTP_SET_PEER_PRIMARY_ADDRESS to change the primary address used by the peer for an association.
setsockopt (sock, IPPROTO_SCTP, SCTP_SET_PEER_PRIMARY_ADDR, (char *)&sctpPeerPrimary, sizeof(sctpPeerPrimary));sctpPeerPrimary is a sctp_setpeerprim structure. This option allows the local node to request that the peer mark the enclosed address as the primary for this association. The enclosed address must be one of the association's locally bound addresses.
Specify the SCTP_PRIMARY_ADDRESS to change the local primary address used for an association.
setsockopt (sock, IPPROTO_SCTP, SCTP_PRIMARY_ADDR, (char *)&sctpPrimary, sizeof(sctpPrimary));sctpPrimary is a sctp_setprim structure. This option allows the user to request that the enclosed address is used locally as the primary address for this association. The enclosed address must be one of the association's locally bound addresses.
Specify SCTP_ADAPTION_LAYER to set the value used in any outgoing adaption layer indication parameters.
setsockopt (sock, IPPROTO_SCTP, SCTP_ADAPTION_LAYER, (char *)&sctpAdaptInd, sizeof(sctpAdaptInd));sctpAdaptInd is a sctp_setadaption structure.
Specify SCTP_DISABLE_FRAGMENTS to enable or disable fragmentation of messages that exceed the MTU.
setsockopt (sock, IPPROTO_SCTP, SCTP_DISABLE_FRAGMENTS, (char *)&optval, sizeof(optval));optval is an integer (type int), either 1 (on) or 0 (off). If on no SCTP message fragmentation will be performed. If a message exceeds the current PMTU size it will not be sent and an error will be generated.
Specify SCTP_PEER_ADDR_PARAMS to modify the parameters associated with peer addresses.
setsockopt (sock, IPPROTO_SCTP, SCTP_PEER_ADDR_PARAMS, (char *)&sctpPeerAddrParams, sizeof(sctpPeerAddrParams));sctpPeerAddrParams is a sctp_paddrparams structure. This option allows the user to configure reachability parameters for a peer's address. The parameters include if heartbeats should be enabled or disabled, the heartbeat interval and the maximum number of retransmission required before an address is considered unreachable.
Specify SCTP_DEFAULT_SEND_PARAM to modify the default parameters used by the sendto function for an association.
setsockopt (sock, IPPROTO_SCTP, SCTP_DEFAULT_SEND_PARAM, (char *)&sctpSndRcvInfo, sizeof(sctpSndRcvInfo));sctpSndRcvInfo is a sctp_sndrcvinfo structure. This option allows the user to set the parameters that would normally be supplied through ancillary data.
Specify SCTP_EVENTS to choose which notification and ancillary data to receive.
setsockopt (sock, IPPROTO_SCTP, SCTP_EVENTS, (char *)&sctpEvents, sizeof(sctpEvents));sctpEvents is a sctp_event_subscribe structure. Setting a field in this structure to 1 causes the specified notifications or additional infomation to be returned by the recvmsg function.
Specify SCTP_MAXSEG in order to set the maximum size to put into a data chunk.
setsockopt (sock, IPPROTO_SCTP, SCTP_MAXSEG, (char *)&optval, sizeof(optval));optval is an integer (type int). A value of 0 indicates no specific limit, in which case the PMTU will be the limit on the size of a data chunk. Otherwise the size limit will be the lesser of optval and the PMTU.
OK, or ERROR if there is an invalid socket, an unknown option, an option length greater than MLEN, insufficient mbufs, or the call is unable to set the specified option.
Not Available
以上是官方给的使用说明,我在这里要讲的是在配置发送选项的时候,倒数第二个参数不给初值的话,发送就会出现阻塞!
例如:
struct sockaddr_in serverAddr;
int nRet = 0;
int nVal;/*/不给初值,发送就会出现阻塞*/
assert(defaultServer != NULL);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(defaultPort);
serverAddr.sin_addr.s_addr = inet_addr(defaultServer);
clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
setsockopt(m_clientsock, SOL_SOCKET, SO_SNDBUF, (char *)&nVal, sizeof(int));//设置发送BUFF
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。