📄 36.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>CTerm非常精华下载</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%" cellspacing="0" cellpadding="0" height="577">
<tr><td width="32%" rowspan="3" height="123"><img src="DDl_back.jpg" width="300" height="129" alt="DDl_back.jpg"></td><td width="30%" background="DDl_back2.jpg" height="35"><p align="center"><a href="http://apue.dhs.org"><font face="黑体"><big><big>123</big></big></font></a></td></tr>
<tr>
<td width="68%" background="DDl_back2.jpg" height="44"><big><big><font face="黑体"><p align="center"> ● UNIX网络编程 (BM: clown) </font></big></big></td></tr>
<tr>
<td width="68%" height="44" bgcolor="#000000"><font face="黑体"><big><big><p align="center"></big></big><a href="http://cterm.163.net"><img src="banner.gif" width="400" height="60" alt="banner.gif"border="0"></a></font></td>
</tr>
<tr><td width="100%" colspan="2" height="100" align="center" valign="top"><br><p align="center">[<a href="index.htm">回到开始</a>][<a href="17.htm">上一层</a>][<a href="37.htm">下一篇</a>]
<hr><p align="left"><small>发信人: cloudsky (晓舟·轩辕明月), 信区: Linux <br>
标 题: 网络socket编程(5) <br>
发信站: 武汉白云黄鹤站 (Mon Feb 8 20:21:54 1999) , 站内信件 <br>
10. man setsockopt <br>
int getsockopt(int s, int level, int optname, char *optval, int *optlen); <br>
int setsockopt(int s, int level, int optname, const char *optval, int optlen <br>
); <br>
当操作socket选项的时候,选项所在层以及选项名必须被指定。 <br>
为了操作socket层选项,level参数应该是SOL_SOCKET。 <br>
其它层的level参数是协议号。例如,为了指明一个选项由 <br>
TCP协议解释,可以用getprotobyname()函数得到TCP协议号。 <br>
getsockopt调用若失败,*optval == 0 <br>
optname不经解释地传递给适当的协议模块去解释。<sys/socket.h>中定义了socket-le <br>
vel <br>
上的选项。绝大多数socket-level选项的optval是个整数。对于setsockopt(),为了en <br>
able <br>
一个布尔型选项,参数optval应该是一个非零值,反之零值将disable这个选项。 <br>
SO_LINGER uses a struct linger parameter that specifies the <br>
desired state of the option and the linger interval (see <br>
below). struct linger is defined in <sys/socket.h>. struct <br>
linger contains the following members: <br>
l_onoff on = 1/off = 0 <br>
l_linger linger time, in seconds <br>
下列选项是socket-level上的 <br>
SO_DEBUG enable/disable recording of debugging information <br>
SO_REUSEADDR enable/disable local address reuse <br>
SO_KEEPALIVE enable/disable keep connections alive <br>
SO_DONTROUTE enable/disable routing bypass for outgoing messages <br>
SO_LINGER linger on close if data is present <br>
SO_BROADCAST enable/disable permission to transmit broadcast messages <br>
SO_OOBINLINE enable/disable reception of out-of-band data in band <br>
SO_SNDBUF set buffer size for output <br>
SO_RCVBUF set buffer size for input <br>
SO_TYPE get the type of the socket(get only) <br>
SO_ERROR get and clear error on the socket(get only) <br>
SO_KEEPALIVE enables the periodic transmission of messages on a connected so <br>
cket <br>
. <br>
If the connected party fails to respond to these messages, the connection is <br>
<br>
considered broken and processes using the socket are notified using a SIGPIP <br>
E si <br>
gnal. <br>
SO_DONTROUTE indicates that outgoing messages should bypass the standard rou <br>
ting <br>
ting <br>
facilities. Instead, messages are directed to the appropriate network interf <br>
ace <br>
according to the network portion of the destination address. <br>
SO_LINGER controls the action taken when unsent messages are <br>
queued on a socket and a close(2) is performed. If the <br>
socket promises reliable delivery of data and SO_LINGER is <br>
set, the system will block the process on the close() <br>
attempt until it is able to transmit the data or until it <br>
decides it is unable to deliver the information (a timeout <br>
period, termed the linger interval, is specified in the set- <br>
sockopt() call when SO_LINGER is requested). If SO_LINGER <br>
is disabled and a close() is issued, the system will process <br>
the close() in a manner that allows the process to continue <br>
as quickly as possible. <br>
The option SO_BROADCAST requests permission to send broad- <br>
cast datagrams on the socket. <br>
对于那些支持带外数据的协议,选项SO_OOBINLINE要求把带外数据放在 <br>
普通数据输入队列里,此时可用不带MSG_OOB标志的recv读取这些带外数据。 <br>
SO_SNDBUF and SO_RCVBUF are options that adjust the normal <br>
buffer sizes allocated for output and input buffers, respec- <br>
tively. The buffer size may be increased for high-volume <br>
connections or may be decreased to limit the possible back- <br>
log of incoming data. The Internet protocols place an abso- <br>
lute limit of 64 Kbytes on these values for UDP and TCP <br>
sockets. <br>
Finally, SO_TYPE and SO_ERROR are options used only with <br>
getsockopt(). SO_TYPE returns the type of the socket (for <br>
example, SOCK_STREAM). It is useful for servers that <br>
inherit sockets on startup. SO_ERROR returns any pending <br>
error on the socket and clears the error status. It may be <br>
used to check for asynchronous errors on connected datagram <br>
sockets or for other asynchronous errors. <br>
getsockopt成功则返回0,否则返回-1,errno包含了更详细的信息。 <br>
/usr/include/netinet/in.h中有如下定义 <br>
/* <br>
* Options for use with [gs]etsockopt at the IP level. <br>
*/ <br>
#define IP_OPTIONS 1 /* set/get IP per-packet options */ <br>
#define IP_HDRINCL 2 /* int; header is included with data (raw) * <br>
/ <br>
#define IP_TOS 3 /* int; IP type of service and precedence */ <br>
<br>
#define IP_TTL 4 /* int; IP time to live */ <br>
#define IP_RECVOPTS 5 /* bool; receive all IP options w/datagram * <br>
/ <br>
#define IP_RECVRETOPTS 6 /* bool; receive IP options for response */ <br>
#define IP_RECVDSTADDR 7 /* bool; receive IP dst addr w/datagram */ <br>
#define IP_RETOPTS 8 /* ip_opts; set/get IP per-packet options */ <br>
<br>
#define IP_MULTICAST_IF 0x10 /* set/get IP multicast interface * <br>
/ <br>
#define IP_MULTICAST_TTL 0x11 /* set/get IP multicast timetolive * <br>
/ <br>
#define IP_MULTICAST_LOOP 0x12 /* set/get IP multicast loopback * <br>
/ <br>
#define IP_ADD_MEMBERSHIP 0x13 /* add an IP group membership * <br>
/ <br>
#define IP_DROP_MEMBERSHIP 0x14 /* drop an IP group membership * <br>
/ <br>
#define IP_DEFAULT_MULTICAST_TTL 1 /* normally limit m'casts to 1 hop * <br>
/ <br>
#define IP_DEFAULT_MULTICAST_LOOP 1 /* normally hear sends if a member * <br>
/ <br>
-- <br>
我问飘逝的风:来迟了? <br>
风感慨:是的,他们已经宣战。 <br>
我问苏醒的大地:还有希望么? <br>
大地揉了揉眼睛:还有,还有无数代的少年。 <br>
我问长空中的英魂:你们相信? <br>
英魂带着笑意离去:相信,希望还在。 <br>
</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="17.htm">上一层</a>][<a href="37.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -