A, socket ()
header: #include <sys / socket.h>
Function Prototype: int socket (int domain, int type, int protocol);
action: creating the socket.
Parameters domain: Specifies to address what type of use. Default AF_INET.
EG:
AF_INET the IPv4 network communication
AF_INET6 IPv6 network communication
AF_PACKET link layer communications
AF_UNIX, AF_LOCAL local communication
Parameters type: the socket type. Default SOCK_STREAM.
EG:
. 1, SOCK_STREAM provide continuous and reliable two-way data flow, i.e., TCP supports OOB mechanism, must be used before all the data transfer connect () to establish the connection status.
2, SOCK_DGRAM discontinuous untrusted connection packet, UDP-based
3, SOCK_SEQPACKET providing continuous reliable connection packet
4, SOCK_RAW original network protocol provides access
5, SOCK_RDM provide reliable data packet connection
6, SOCK_PACKET provide direct communication and network drivers. protocol is used to specify the socket transport protocol number, usually do not ignore this reference, can be set to 0
Parameters protocol: protocol number. It can be set to 0, indicating the selection of the current protocol family and the system defaults combination type
EG:
IPPROTO_TCP go IPPTOTO_UDP IPPROTO_SCTP IPPROTO_TIPCTCP
the TCP transport protocol UDP transport protocol STCP transport protocol transport protocol TIPC
Two, bind ()
header: #include <sys / socket.h>
Function Prototype: int bind (int sockfd, const struct sockaddr * addr, socklen_t addrlen);
action: the address assigned to the address family specific socket.
Parameters sockfd: socket descriptor that is, it is through the socket () function creates uniquely identify a socket. bind () function is to give the word to describe the binding of a name.
Parameters addr: a const struct sockaddr * pointer to bind to the protocol address sockfd. The address structure depending on the protocol suite addresses when addresses are created socket is different.
IPV4:
struct in_addr
{
uint32_t s_addr; / * 32 bit IPv4 address * /
};
struct the sockaddr_in
{
sa_family_t the sin_family; / * // address family * /
in_port_t sin_port; / * 16 bit TCP / UDP port number * /
struct in_addr sin_addr; / * 32-bit IP address * /
};
Parameters addrlen: the length of the address structure.
三,listen()
头文件:#include<sys/socket.h>
函数原型:int listen(int sockfd, int backlog);
作用:如果作为一个服务器,在调用socket()、bind()之后就会调用listen()来监听这个socket,如果客户端这时调用connect()发出连接请求,服务器端就会接收到这个请求。
参数sockfd:即为要监听的socket描述字
参数backlog:相应socket可以排队的最大连接个数。
四,connect()
头文件:#include<sys/socket.h>
函数原型:int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
作用:将套接字连接到目的地址,客户端通过调用connect函数来建立与TCP服务器的连接。
参数sockfd:为客户端的socket描述字
参数addr:为服务器的socket地址
参数addrlen:为socket地址的长度
五,accept()
头文件:#include<sys/socket.h>
函数原型:int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen); //返回连接connect_fd
作用:TCP服务器端依次调用socket()、bind()、listen()之后,就会监听指定的socket地址了。TCP客户端依次调用socket()、connect()之后就向TCP服务器发送了一个连接请求。TCP服务器监听到这个请求之后,就会调用accept()函数取接收请求,这样连接就建立好了。之后就可以开始网络I/O操作了,即类同于普通文件的读写I/O操作。
关键点
*accept()会创建一个新的socket,并且正是这个新的socket会与执行connect()的对象socket进行连接。
返回值:accept()的返回结果是已经连接的socket文件描述符
六,close()
头文件:#include <unistd.h>
函数原型:int close(int fd);
作用:在服务器与客户端建立连接之后,会进行一些读写操作,完成了读写操作就要关闭相应的socket描述字