socket

    技术2022-07-10  141

    1、

    SYNOPSIS #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> int socket(int domain, int type, int protocol);

    int socket(int domain, int type, int protocol);

    domain

    domain即协议域,又称为协议族(family)。常用的协议族有,AF_INET、AF_INET6、AF_LOCAL(或称AF_UNIX,Unix域socket)、AF_ROUTE等等。协议族决定了socket的地址类型,在通信中必须采用对应的地址,如AF_INET决定了要用ipv4地址(32位的)与端口号(16位的)的组合、AF_UNIX决定了要用一个绝对路径名作为地址。

    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_ALG Interface to kernel crypto API

     type:

    type:指定socket类型。常用的socket类型有,SOCK_STREAM、SOCK_DGRAM、SOCK_RAW、SOCK_PACKET、SOCK_SEQPACKET等等。

    套接字具有指定的类型,该类型指定通信语义。当前定义的类型是:

    SOCK_STREAM提供有序的、可靠的、双向的、基于连接的字节流。可能支持带外数据传输机制。

    SOCK_DGRAM支持数据报(固定最大长度的无连接、不可靠的消息)。

    SOCK_SEQPACKET为固定最大长度的数据报提供了有序、可靠、基于双向连接的数据传输路径;消费者需要在每次输入系统调用时读取整个数据包。

    SOCK_RAW提供原始网络协议访问。

    SOCK_RDM提供了一个不保证排序的可靠数据报层。

    数据包过时,不应该在新的程序中使用;看到包(7)。有些套接字类型可能不是由所有协议系列实现的。自从Linux 2.6.27以来,type参数有第二个用途:除了指定套接字类型之外,它还可以包括按位或以下任何val值,以修改socket()的行为:

    SOCK_NONBLOCK在新的open文件描述上设置O_NONBLOCK文件状态标志。使用这个标志可以节省对fcntl(2)的额外调用来实现相同的结果。在新的文件描述符上设置关闭-exec (FD_CLOEXEC)标志。请参阅open(2)中对O_CLOEXEC标志的描述,了解它可能有用的原因。

    The socket has the indicated type, which specifies the communication semantics. Currently defined types are: 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 val‐ ues, 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.

     protocol:

    protocol:故名思意,就是指定协议。常用的协议有,IPPROTO_TCP、IPPTOTO_UDP、IPPROTO_SCTP、IPPROTO_TIPC等,它们分别对应TCP传输协议、UDP传输协议、STCP传输协议、TIPC传输协议

    On  success,  a  file  descriptor  for  the new socket is returned.  On error, -1 is returned, and errno is set appropriately.

    如果成功,将返回新套接字的文件描述符。在出现错误时,返回-1,并相应地设置errno。

     

    注意:并不是上面的type和protocol可以随意组合的,如SOCK_STREAM不可以跟IPPROTO_UDP组合。当protocol为0时,会自动选择type类型对应的默认协议。

    当我们调用socket创建一个socket时,返回的socket描述字它存在于协议族(address family,AF_XXX)空间中,但没有一个具体的地址。如果想要给它赋值一个地址,就必须调用bind()函数,否则就当调用connect()、listen()时系统会自动随机分配一个端口。

    返回的socket描述字它存在于协议族空间中:虚拟地址空间?bind为其分配地址

     

     

     

    Processed: 0.008, SQL: 9