【Unix网络编程】socket函数与描述符

Socket

Socket 用于创建一个会话的端点,返回一个描述符. 失败将返回-1,并设置errno.

#include <sys/socket.h>
int socket(int domain, int type, int protocol);

参数

domain

用于指定会话的区域.这个选择决定用于会话的协议族.这些协议族位于sys/socket.h
参考手册.

Name                Purpose                          Man page
AF_UNIX, AF_LOCAL   Local communication              unix(7)
AF_INET             IPv4 Internet protocols          ip(7)
AF_INET6            IPv6 Internet protocols          ipv6(7)
AF_IPX              IPX - Novell protocols
AF_NETLINK          Kernel user interface device     netlink(7)
AF_X25              ITU-T X.25 / ISO-8208 protocol   x25(7)
AF_AX25             Amateur radio AX.25 protocol
AF_ATMPVC           Access to raw ATM PVCs
AF_APPLETALK        AppleTalk                        ddp(7)
AF_PACKET           Low level packet interface       packet(7)

常用的应该时AF_LOCALAF_INET.

type

这是定义数据语义(semantics)的.
SOCK_STREAM Provides sequenced, reliable, two-way, connection-based byte streams. An out-of-band data transmission mechanism may be supported.

   SOCK_DGRAM      Supports datagrams (connectionless, unreliable messages of a fixed maximum length).

   SOCK_SEQPACKET  Provides a sequenced, reliable, two-way connection-based data transmission path for datagrams of fixed maximum length;  a  consumer
                   is required to read an entire packet with each input system call.

   SOCK_RAW        Provides raw network protocol access.

   SOCK_RDM        Provides a reliable datagram layer that does not guarantee ordering.

   SOCK_PACKET     Obsolete and should not be used in new programs; see packet(7).

   Some socket types may not be implemented by all protocol families.

   Since Linux 2.6.27, the type argument serves a second purpose: in addition to specifying a socket type, it may include the bitwise OR of any of the following values, to modify the behavior of socket():

   SOCK_NONBLOCK    Set the O_NONBLOCK file status flag on the new open file description.
        Using this flag saves extra calls to fcntl(2) to achieve the same result.

   SOCK_CLOEXEC Set  the close-on-exec (FD_CLOEXEC) flag on the new file descriptor.
        See the description of the O_CLOEXEC flag in open(2) for reasons why this may be useful.

基于网络的知识,Stream基于TCP,DGram基于UDP.
我在面试中遇到过一个问怎么才能让accept()变成非阻塞,这时候就type要位或(|)上SOCK_NONBLOCK.

protocol

制定所使用的协议,通常一种socket的协议族只支持一种协议,这种情况可以设为0.但是也有可能有特殊的1对多的情况,这时候我们可以通过getprotoent来获取.

其他

套接字选项

#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>

int getsockopt(int sockfd, int level, int optname,
               void *optval, socklen_t *optlen);
int setsockopt(int sockfd, int level, int optname,
               const void *optval, socklen_t optlen);

我们可以使用getsockopt和setsockopt来获取或修改套接字的选项.
其中level是指操作的级别,这里面的级别分为两种.第一种时socketAPI级,第二种时协议级.
第一级level=SOL_SOCKET,第二种,level=Protocol_numer(Protocol_number,就是所要操作的协议编号,通过getprotoent来查看).
optname是选项的名字,包含在<sys/socket.h>里了.
optval是值,optlen是值的长度.

其中:

>
SO_KEEPALIVE 保持连接检测对方主机是否崩溃,避免(服务器)永远阻塞于TCP连接的输入。

设置该选项后,如果2小时内在此套接口的任一方向都没有数据交换,TCP就自动给对方 发一个保持存活探测分节(keepalive probe)。这是一个对方必须响应的TCP分节.它会导致以下三种情况:

1、对方接收一切正常:以期望的ACK响应,2小时后,TCP将发出另一个探测分节。

2、对方已崩溃且已重新启动:以RST响应。套接口的待处理错误被置为ECONNRESET,套接 口本身则被关闭。

3、对方无任何响应:源自berkeley的TCP发送另外8个探测分节,相隔75秒一个,试图得到一个响应。在发出第一个探测分节11分钟15秒后若仍无响应就放弃。套接口的待处理错误被置为ETIMEOUT,套接口本身则被关闭。如ICMP错误是“host unreachable(主机不可达)”,说明对方主机并没有崩溃,但是不可达,这种情况下待处理错误被置为 EHOSTUNREACH。

有关SO_KEEPALIVE的三个参数详细解释如下:

(16)tcp_keepalive_intvl,保活探测消息的发送频率。默认值为75s。

发送频率tcp_keepalive_intvl乘以发送次数tcp_keepalive_probes,就得到了从开始探测直到放弃探测确定连接断开的时间,大约为11min。

(17)tcp_keepalive_probes,TCP发送保活探测消息以确定连接是否已断开的次数。默认值为9(次)。

注意:只有设置了SO_KEEPALIVE套接口选项后才会发送保活探测消息。

(18)tcp_keepalive_time,在TCP保活打开的情况下,最后一次数据交换到TCP发送第一个保活探测消息的时间,即允许的持续空闲时间。默认值为7200s(2h)。

这里有更详细的解释.

但其实像FTP服务器这种有应用层分钟量级的超时,这是应用层实现的,这可以使得应用层对这种机制有完全控制权.
在SCTP中有心跳机制与此类似.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值