现在的位置: 首页 > 综合 > 正文

套接字选项和I/O控制命令

2013年06月05日 ⁄ 综合 ⁄ 共 2870字 ⁄ 字号 评论关闭

套接字选项和I/O控制命令

套接字一旦建立,通过套接字选项和I/O控制命令对各种属性进行操作,便可对套接字的行为产生影响。有的选项只用于信息的返回,而有的选项则可在应用程序中影响套接字的行为。I/O控制命令肯定会对套接字的行为产生影响。

1         套接字选项

getsocketopt(获得套接字选项)函数来说,它的常见用法是获得与指定套接字相关的信息。其原型如下:

int getsockopt (

  SOCKET s,        

  int level,       

  int optname,     

  char FAR* optval,

  int FAR*  optlen 

);

第一个参数s指定的是一个套接字,我们打算在这个套接字上执行指定的选项。对你打算使用的具体协议来说,这个套接字必须是有效的。大多数选项都是一种特定的协议和套接字类型专有的,而其他选项适用于所有类型的套接字(特别是第二个参数levelSOLSOCKET的一个选项级别表明它是一个通用选项,并不一定要与一种给定的协议有关。但之所以说不一定,是由于并非所有协议都实现了级别SOLSOCKET定义的每一个套接字选项。例如,SOBROADCAST可将一个套接字置入广播模式,但并非所有支持的协议都支持广播套接字的概念。optname参数是我们在此真正感兴趣的选项。这些选项名均是在Winsock头文件内定义的常数值。最常见的与协议无关选项(比如和SOLSOCKET级别关联在一起的选项)是在Winsock.hWinsock2.h这两个头文件中定义的。对于每种特定的协议来说,它们都有自己的头文件,定义了与之对应的特定选项。最后,optvaloptlen参数是两个变量,用于返回目标选项的值。大多数情况下,选项值都是一个整数(但也不是绝对的)。

setsocketopt函数用于在一个套接字级别或由协议决定的级别上设置套接字选项。它的定义如下:

int setsockopt (

  SOCKET s,                

  int level,               

  int optname,             

  const char FAR * optval, 

  int optlen               

);

它的参数和setsocketopt函数的参数相同,例外的是我们以optvaloptlen参数的形式,将值传递进去。这些值是为指定的选项设定的。和getsocketopt函数一样, optval大多数时候都是一个整数,但也并非总是如此。正式编程的时候,应查询对每个选项的说明,了解到底该将什么作为选项值传递进去。

调用getsocketoptsetsocketopt时,最常见的错误是试图获得一个套接字的信息,但那个套接字的基层协议却不具备某种指定的特征(或选项)。例如,类型为SOCK _ STREAM的一个套接字本身是不能对数据进行广播操作的;因此,若试图设置或获取SOBROADCAS T选项,便会造成WSAENOPROTOOPT错误。

关于SOL_SOCKETIPPROTO_TCP的详细解释,可以参考MSDN中的getsocketopt函数的解释方法。如下是从MSDN中摘抄一步分列表;

level = SOL_SOCKET

Value

Type

Meaning

SO_BROADCAST

BOOL

Allow transmission of broadcast messages on the socket.

SO_DEBUG

BOOL

Record debugging information.

SO_DONTLINGER

BOOL

Do 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

Do not route: send directly to interface.

SO_GROUP_PRIORITY

int

Reserved for future use with socket groups. Specify the relative priority to be established for sockets that are part of a socket group.

SO_KEEPALIVE

BOOL

Send keepalives

SO_LINGER

struct LINGER

Linger on close if unsent data is present.

SO_OOBINLINE

BOOL

Receive out-of-band data in the normal data stream. (See section DECnet Out-Of-band data for a discussion of this topic.)

SO_RCVBUF

int

Specify 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

Allow the socket to be bound to an address that is already in use. (See bind.)

SO_SNDBUF

int

Specify 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

TCP_NODELAY

BOOL

Disables the Nagle algorithm for send coalescing.

1    included for backward compatibility with Windows Sockets 1.1

BSD options not supported for setsockopt are:

Value

Type

Meaning

SO_ACCEPTCONN

BOOL

Socket is listening

SO_RCVLOWAT

int

Receive low water mark

SO_RCVTIMEO

int

Receive time-out (available in Microsoft implementation of Windows Sockets 2)

SO_SNDLOWAT

int

Send low water mark

SO_SNDTIMEO

int

抱歉!评论已关闭.