socket通信基本流程
服务端
在服务端,从创建socket到连接成功共用到4个函数,下面一一说明
socket()
这个函数的作用在于给服务端在主机上一个名分,具有具体的IP地址和PORT端口,如此才能让客户端或者浏览器访问到它.
函数声明
/* Create a new socket of type TYPE in domain DOMAIN, using
protocol PROTOCOL. If PROTOCOL is zero, one is chosen automatically.
Returns a file descriptor for the new socket, or -1 for errors. */
extern int socket (int __domain, int __type, int __protocol) __THROW;
参数意义
domain
表示这个socket通信所用的网络层协议,是IPv4还是IPv6
type
表示这个socket所用的传输层协议,udp还是tcp
protocol
一般默认置为0,表示系统自动选择适合的协议
函数功能描述
这个意思就是创建了一个新的socket,这个scoket的domain是什么,type是什么,protocol是什么
Create a new socket of type TYPE in domain DOMAIN, using
protocol PROTOCOL.
函数返回值
正确创建socket会返回一个文件描述符(file descriptor),返回-1表示出现错误.
Returns a file descriptor for the new socket, or -1 for errors.
bind()
函数声明
extern int bind (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len)
__THROW;
参数意义
int __fd
就是socket返回的文件描述符,勇于指针服务端绑定的socket
__CONST_SOCKADDR_ARG __addr
这是一个socketaddr结构数据,其中存储了网络信息,此处是在给服务端指定ip地址和端口号
socklen_t __len
描述第二个参数的数据长度,这个参数决定了内存读取数据的长度,如果少于正确的值,会造成数据缺失,如果大于预期值,又会造成数据的溢出
函数功能描述
此函数就是为了给scoket文件描述符指定网络信息的
Give the socket FD the local address ADDR (which is LEN bytes long).
函数返回值
成功bind()返回0,错误返回-1,这种返回的函数一般都是要进行错误处理的
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is
set appropriately.
listen()
listen的作用在于可以设置与服务端三次握手的最大端点数,但是仍不能进行通信.
函数声明
int listen(int sockfd, int backlog);
参数意义
sockfd
就是之前socket函数返回的fd,用于设置是哪个服务端在进行listen
backlog
backlog的值决定了sockfd能同时最多完成三次握手的客户端数量,默认最大值为128。
函数功能描述
“backlog” 参数定义了套接字文件描述符 sockfd 的待处理连接队列可以增长的最大长度。
The backlog argument defines the maximum length to which the queue of
pending connections for sockfd may grow
listen() marks the socket referred to by sockfd as a passive socket, that
is, as a socket that will be used to accept incoming connection requests
using accept(2).
The sockfd argument is a file descriptor that refers to a socket of type
SOCK_STREAM or SOCK_SEQPACKET.
The backlog argument defines the maximum length to which the queue of
pending connections for sockfd may grow. If a connection request arrives
when the queue is full, the client may receive an error with an indication
of ECONNREFUSED or, if the underlying protocol supports retransmission,
the request may be ignored so that a later reattempt at connection suc‐
ceeds.
函数返回值
成功返回0,失败返回-1
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set
appropriately.
accept()
这是进行连接的最后一步,也是客户端开始与服务端交互的第一步,这一步返回的新的文件描述符fd将是后面通信用的fd,而之前的fd在这一步之后将被废除.
函数声明
extern int accept (int __fd, __SOCKADDR_ARG __addr,
socklen_t *__restrict __addr_len);
参数意义
int __fd
与之前一样,socket函数返回的fd,也是通过这个fd来与客户端建立连接
__SOCKADDR_ARG __addr
这是一个传入传出参数,这个结构体不仅要作为函数参数传入,还要返回client端传过来的sockaddr信息,这个信息保存在这个参数中.
*addrlen
这个参数的返回值是指向sockaddr的指针
函数功能描述
/* Await a connection on socket FD.
When a connection arrives, open a new socket to communicate with it,
set *ADDR (which is *ADDR_LEN bytes long) to the address of the connecting
peer and *ADDR_LEN to the address's actual length, and return the
new socket's descriptor, or -1 for errors.
This function is a cancellation point and therefore not marked with
__THROW. */
函数返回值
成功返回一个非负数(nonnegative)作为accepted socket的fd,错误返回-1
客户端
客户端的socket函数返回的fd是其唯一的fd,也是后续函数都要用到的,但是其参数以及各方面都与服务端一样,就不赘述了.
值得一提的是,客户端其实也可以使用bind(),只是一般来说你不用,系统会自动帮你绑定ip地址与端口号
connect()
这是客户端主动连接客户端的过程,需要为其生命连接的目的ip和port
函数声明
extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);
参数意义
_fd
客户端socket返回的fd
__CONST_SOCKADDR_ARG __addr
传入参数,const表明这个结构体不能被修改
socklen_t __len
传入int型参数,以便正确读取结构体中的网络信息
函数功能描述
创建连接与ADDR所描述的服务端
Open a connection on socket FD to peer at ADDR (which LEN bytes long).
For connectionless socket types, just set the default address to send to
and the only address from which to accept transmissions.
Return 0 on success, -1 for errors.
This function is a cancellation point and therefore not marked with
__THROW.
函数返回值
成功返回0,失败返回-1