• socket()


    #include <sys/types.h>          
    #include <sys/socket.h>
     

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

     
    返回值:
           On success, a file descriptor for the new socket is returned.  On error, -1 is returned, and errno is set  appropriately.
     
    描述:
    socket() creates an endpoint for communication and returns a descriptor.
     
    The domain argument specifies a communication domain; this selects the protocol family which will be used for communication.  These families are defined in <sys/socket.h>.  The currently understood formats include:
    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)
     
     
    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; for example, SOCK_SEQPACKET  is  not  implemented
    for AF_INET.
     
     
     
    domain,即协议域,又称为协议族(family)。常用的协议族有,AF_INET、AF_INET6、AF_LOCAL(或称AF_UNIX,Unix域socket)、AF_ROUTE等等。协议族决定了socket的地址类型,在通信中必须采用对应的地址,如AF_INET决定了要用ipv4地址(32位的)与端口号(16位的)的组合、AF_UNIX决定了要用一个绝对路径名作为地址。http://blog.csdn.net/sandware/article/details/40923491
     
    type,指定socket类型。常用的socket类型有,SOCK_STREAM、SOCK_DGRAM、SOCK_RAW、SOCK_PACKET、SOCK_SEQPACKET等等
     
    protocol,故名思意,就是指定协议。常用的协议有,IPPROTO_TCP、IPPTOTO_UDP、IPPROTO_SCTP、IPPROTO_TIPC等,它们分别对应TCP传输协议、UDP传输协议、STCP传输协议、TIPC传输协议。
     
    注意:并不是上面的type和protocol可以随意组合的,如SOCK_STREAM不可以跟IPPROTO_UDP组合。当protocol为0时,会自动选择type类型对应的默认协议
     
     
  • 相关阅读:
    数据库权限分配操作
    1130-host ... is not allowed to connect to this MySql server
    ubuntu 14.04安装mysql-python
    Ubuntu中的MySQL修改root密码的多种方法
    ubuntu下安装mysql及卸载mysql方法
    XShell本地上传文件到Ubuntu上及从Ubuntu下载文件到本地
    深度学习-1
    html-新闻滚动条
    Django部署uwsgi 与 nginx配置
    二叉树层次遍历
  • 原文地址:https://www.cnblogs.com/black-mamba/p/5678419.html
Copyright © 2020-2023  润新知