Unix Network Programming {socket 选项}

SO_BROADCAST 是否支持广播,只能用于UDP,ETHERNET等支持广播的协议。

SO_DEBUG 只支持TCP协议。开启时,内核会跟踪所有发送和接受的数据包,用trpt 

命令来分析。

SO_ERROR是一个用于读取socket错误的选项。

SO_KEEPALIVE 如果连接双方超过两个小时没有数据交换就发送探测包。这个时间的修改要看各种内核实现,而且这个数值是在内核设定的,不是每个socket设定的。最好还是逻辑层加心跳包。If there is no response at all to TCP's keep-alive probes, the socket's pending error is set to ETIMEDOUT and the socket is closed. The purpose of this option is to detect if the peer host crashes or becomes unreachable (e.g., dial-up modem connection drops, power fails, etc.). If the peer process crashes, its TCP will send a FIN across the connection, which we can easily detect with select. 通过select可以检测另一方是否断掉。

SO_LINGER 用于影响close  socket。Shutdown比close更安全。If l_onoff is nonzero and l_linger is nonzero, then the kernel will linger when the socket is closed (p. 472 of TCPv2). That is, if there is any data still remaining in the socket send buffer, the process is put to sleep until either: (i) all the data is sent and acknowledged by the peer TCP, or (ii) the linger time expires. If the socket has been set to nonblocking (Chapter 16), it will not wait for the close to complete, even if the linger time is nonzero. When using this feature of the SO_LINGER option, it is important for the application to check the return value from close, because if the linger time expires before the remaining data is sent and acknowledged, close returns EWOULDBLOCK and any remaining data in the send buffer is discarded.As we show in this scenario, the client's close can return before the server reads the remaining data in its socket receive buffer.

One way for the client to know that the server has read its data is to call shutdown (with a second argument of SHUT_WR) instead of close and wait for the peer to close its end of the connection. 

SO_RCVBUF and SO_SNDBUF 发送和接收缓冲区大小。影响TCP接收和发送窗口大小,流控有影响。SO_RCVBUF必须在listen和connect之前设置。 大小至少是MSS的四倍。

SO_RCVLOWAT and SO_SNDLOWAT  select时可读/写的数据量。

SO_RCVTIMEO and SO_SNDTIMEO 分别影响read, readv, recdv, recvfrom, and recvmsg.

和write, writev, send, sendto, and sendmsg

SO_REUSEADDR 必须在bind之前设置。TCP不能对相同地址相同端口重复bind。UDP是可以的。TCP可以不同地址相同端口重复bind,内核会按照数据包的目的地做分发。SO_REUSEADDR allows a single process to bind the same port to multiple sockets, as long as each bind specifies a different local IP address. 

SO_TYPE 获得socket类型。

SO_USELOOPBACK 

TCP_NODELAY 是否使用Nagle算法。

TCP_MAXSEG 设置TCP连接的MSS值。

SO_DEBUG, SO_DONTROUTE, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_RCVLOWAT, SO_SNDBUF, SO_SNDLOWAT, TCP_MAXSEG, and TCP_NODELAY是由连接socket从listen socket继承的,所以不需要重新设置。 To ensure that one of these socket options is set for the connected socket when the three-way handshake completes, we must set that option for the listening socket.

Signal Driven IO 是异步IO。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值