以下内容摘自于<msdn>
The setsockopt function sets a socket option.
int setsockopt( SOCKET s, int level, int optname, const char* optval, int optlen );
Remarks
The setsockopt function sets the current value for a socket option associated with a socket of any type, in any state. Although options can exist at multiple protocol levels, they are always present at the uppermost socket level. Options affect socket operations, such as whether expedited data (OOB data for example) is received in the normal data stream, and whether broadcast messages can be sent on the socket.
Note If the setsockopt function is called before the bind function, TCP/IP options will not be checked with TCP/IP until the bind occurs. In this case, the setsockopt function call will always succeed, but the bind function call may fail because of an early setsockopt failing.
Note If a socket is opened, a setsockopt call is made, and then a sendto call is made, Windows Sockets performs an implicit bind function call.
level = SOL_SOCKET
Value | Type | Meaning |
---|---|---|
SO_BROADCAST | BOOL | Enables transmission and receipt of broadcast messages on the socket. |
SO_CONDITIONAL_ACCEPT | BOOL | Enables sockets to delay the acknowledgment of a connection until after the WSAAccept condition function is called. |
SO_DEBUG | BOOL | Records debugging information. |
SO_DONTLINGER | BOOL | Does not block close waiting for unsent data to be sent. Setting this option is equivalent to setting SO_LINGER with l_onoff set to zero. |
SO_DONTROUTE | BOOL | Does not route: sends directly to interface. Succeeds but is ignored on AF_INET sockets; fails on AF_INET6 sockets with WSAENOPROTOOPT. Not supported on ATM sockets (results in an error). |
SO_GROUP_PRIORITY | int | Reserved. |
SO_KEEPALIVE | BOOL | Sends keep-alives. Not supported on ATM sockets (results in an error). |
SO_LINGER | LINGER | Lingers on close if unsent data is present. |
SO_OOBINLINE | BOOL | Receives OOB data in the normal data stream. (See section Protocol Independent Out-Of-band Data for a discussion of this topic.) |
SO_RCVBUF | int | Specifies the total per-socket buffer space reserved for receives. This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. |
SO_REUSEADDR | BOOL | Allows the socket to be bound to an address that is already in use. (See bind.) Not applicable on ATM sockets. |
SO_EXCLUSIVEADDRUSE | BOOL | Enables a socket to be bound for exclusive access. Does not require administrative privilege. |
SO_SNDBUF | int | Specifies the total per-socket buffer space reserved for sends. This is unrelated to SO_MAX_MSG_SIZE or the size of a TCP window. |
PVD_CONFIG | Service Provider Dependent | This object stores the configuration information for the service provider associated with socket s. The exact format of this data structure is service provider specific. |
level = IPPROTO_TCP1
Value | Type | Meaning |
---|---|---|
TCP_NODELAY | BOOL | Disables the Nagle algorithm for send coalescing. |
1 included for backward compatibility with Windows Sockets 1.1 |
level = NSPROTO_IPX
Note Windows NT supports all IPX options. Windows Me/98/95 support only the following options:
Note IPX_PTYPE
IPX_FILTERPTYPE
IPX_DSTYPE
IPX_RECVHDR
IPX_MAXSIZE (used with the getsockopt function)
IPX_ADDRESS (used with the getsockopt function)
Value | Type | Meaning |
---|---|---|
IPX_PTYPE | int | Sets the IPX packet type. |
IPX_FILTERPTYPE | int | Sets the receive filter packet type |
IPX_STOPFILTERPTYPE | int | Stops filtering the filter type set with IPX_FILTERTYPE |
IPX_DSTYPE | int | Sets the value of the data stream field in the SPX header on every packet sent. |
IPX_EXTENDED_ADDRESS | BOOL | Sets whether extended addressing is enabled. |
IPX_RECVHDR | BOOL | Sets whether the protocol header is sent up on all receive headers. |
IPX_RECEIVE_BROADCAST | BOOL | Indicates broadcast packets are likely on the socket. Set to TRUE by default. Applications that do not use broadcasts should set this to FALSE for better system performance. |
IPX_IMMEDIATESPXACK | BOOL | Directs SPX connections not to delay before sending an ACK. Applications without back-and-forth traffic should set this to TRUE to increase performance. |
BSD options not supported for setsockopt are shown in the following table.
Value | Type | Meaning |
---|---|---|
SO_ACCEPTCONN | BOOL | Socket is listening. |
SO_RCVLOWAT | int | Receives low watermark. |
SO_RCVTIMEO | int | Receives time-out in milliseconds (available in the Microsoft implementation of Windows Sockets 2). |
SO_SNDLOWAT | int | Sends low watermark. |
SO_SNDTIMEO | int | Sends time-out in milliseconds (available in the Microsoft implementation of Windows Sockets 2). |
SO_TYPE | int | Type of the socket. |