[Linux网络通信]socket函数

socket函数用于Linux网络通信,传入的参数为对网络协议的描述,会返回一个文件描述符,该文件用于网络通信,

如果C/C++中0,1,2默认被使用,如果没有打开其他文件的话一般就是3

在Linux环境下可以通过man 2 socket  指令查看Linux官方介绍socket的文档

头文件

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

接口

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

参数

第一个参数domain

直译为域,这个参数与网络通信协议相对应,通过宏定义的方式传参,

常用的有 AF_INET       IPV4网络协议

               AF_INET6     IPv6协议

Linux官方文档显示支持的参数

         Name         Purpose                                    Man page
       AF_UNIX      Local communication                        unix(7)
       AF_LOCAL     Synonym for AF_UNIX
       AF_INET      IPv4 Internet protocols                    ip(7)
       AF_AX25      Amateur radio AX.25 protocol               ax25(4)
       AF_IPX       IPX - Novell protocols
       AF_APPLETALK AppleTalk                                  ddp(7)
       AF_X25       ITU-T X.25 / ISO-8208 protocol             x25(7)
       AF_INET6     IPv6 Internet protocols                    ipv6(7)
       AF_DECnet    DECet protocol sockets
       AF_KEY       Key  management protocol, originally de‐
                    veloped for usage with IPsec
       AF_NETLINK   Kernel user interface device               netlink(7)
       AF_PACKET    Low-level packet interface                 packet(7)
       AF_RDS       Reliable Datagram Sockets (RDS) protocol   rds(7)
                                                               rds-rdma(7)
       AF_PPPOX     Generic PPP transport layer, for setting
                    up L2 tunnels (L2TP and PPPoE)
       AF_LLC       Logical  link  control  (IEEE 802.2 LLC)
                    protocol
       AF_IB        InfiniBand native addressing
       AF_MPLS      Multiprotocol Label Switching
       AF_CAN       Controller Area Network  automotive  bus
                    protocol
       AF_TIPC      TIPC, "cluster domain sockets" protocol
       AF_BLUETOOTH Bluetooth low-level socket protocol
       AF_ALG       Interface to kernel crypto API
       AF_VSOCK     VSOCK   (originally  "VMWare  VSockets")   vsock(7)
                    protocol for hypervisor-guest communica‐
                    tion
       AF_KCM       KCM  (kernel connection multiplexer) in‐
                    terface
       AF_XDP       XDP (express data path) interface

第二个参数type

这个参数是指定网络嵌套字socket的类型

常用的有 SOCK_STREAM、SOCK_DGRAM、SOCK_RAW、SOCK_PACKET、SOCK_SEQPACKET

Linux官方文档显示支持的参数


       SOCK_STREAM     Provides sequenced, reliable, two-way, connection-based byte streams.  An out-of-band data  transmission  mechanism  may  be  sup‐
                       ported.

       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 open file description (see open(2)) referred to by the new file descriptor.  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 rea‐
                       sons why this may be useful.
 

第三个参数protocal

直译为协议,如果传的0话,会根据前两个参数进行推导,如果显示的传递的话,必须要跟前两个参数对应上,比较麻烦,一般来说传0就行

返回值

返回一个文件描述符,该文件用于网络通信(bind的参数)

使用socket接口创建一个使用IPV4模型通信的文件

int   fd  = ::socket(AF_INET,SOCK_DGRAM,0);

        if(fd < 0)

        {

            LOG(FATAL,"socket error\n");            

            exit(SOCKET_ERROR);

        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值