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);
}