TCP/UDP protocol operation process and interface introduction

Transport layer protocol: TCP/UDP protocol

TCP : Transmission Control Protocol, connection-oriented, reliable transmission, byte stream-oriented;

——Applied to scenarios that require more security than real-time performance (file transfer, etc.);

UDP : User Datagram Protocol, connectionless, unreliable, datagram-oriented;

——Applied to scenarios that require real-time performance more than security (video, audio, etc.);

UDP operation process:

Server:

① Create a socket:

Create a socket structure in the kernel to associate the process with the network card;

② Bind address information for the socket:

Describe the source address information for the created socket structure;

③ Receive data:

Take out data from the socket receiving buffer;

④ Send data to the server:

Put the data to be sent into the send buffer;

⑤ Close the socket;

client:

① Create a socket;

② Bind address information for the socket:

The client does not recommend actively binding addresses . After binding, the program can only start one and the client does not need to use a fixed address;

③Send data to the server:

If the socket is not bound to the specified address before sending data, the system will automatically select the appropriate address information for binding;

④ Receive data;

⑤ Close the socket;

TCP operation process

Listening socket (only for handling new connection requests) & communication socket (only for communication);

Need to connect first and then communicate, so there is no broadcast operation;

Server

① Create a socket:

Create a socket structure in the kernel to associate the process with the network card;

② Bind address information for the socket:

Describe the source address information for the created socket structure;

③Start monitoring:

Tell the server that the current socket can handle the connection request (the listening socket is converted to the listen state);

If a client sends a connection request, the service will create a new socket for it to communicate with the specified client (the socket is a communication socket, and the state is established—ready state);

④ Obtain a new connection:

Take out a new socket descriptor from the completed connection queue, which corresponds to the communication between the specified socket and the specified client;

⑤ send and receive data;

⑥Close the socket;

client

① Create a socket;

②Bind address information for the socket (not recommended);

③Initiate a connection to the server:

After the connection, the client tcp will also save the complete quintuple;

④ send and receive data;

⑤ Close the socket;

Interface introduction

#include<sys/socket.h>
#include<netinet/in.h>

create socket

int socket(int domain, int type, int protocol);
//domain:地址域类型——AF_INET & AF_INET6;
//type:套接字类型——SOCK_STREAM & SOCK_DGRAM;
//protocol:协议类型——IPPROTO_TCP & IPPROTO_UDP (可以默认给0);
//返回值:成功返回一个套接字描述符,失败返沪-1;

Bind address information

int bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen);

//sockfd:创建套接字返回的描述符;
//addr:要绑定的地址信息(这是一个通用描述,根据前两个字节的内容使用统一的接口绑定不同的地址结构,还有端口信息、IP地址,IPV4为struct sockaddr_in, IPV6为struct sockaddr_in6);
//addrlen:地址信息长度,防止访问越界;

eg:
struct sockaddr_in addr;
addr.sin_family = AF_INET;//前两个字节代表IPV4地址结构,IPV6为AF_INET6
addr.sin_port = htons(9000);//端口信息,需要转化为网络字节序
addr.sin_addr.s_addr = inet_addr("192.168.2.2");//本机有的IP地址,inet_addr可将点分十进制转化为长整型数字
bind(sockfd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in));

close socket

int close(int sockfd);

//实际情况下在TCP通信中,监听套接字基本不关闭,一般只关闭通信套接字

UDP send data

ssize_t sendto(int sockfd,char* data,int data_len,int flag,struct socket* dest_addr,socket_t addr_len);

//sockfd:套接字描述符
//data:要发送的数据首地址
//data_len:要发送的数据长度
//flag:选项标志,默认为0----表示当前操作是阻塞操作
//dest_addr:对端地址信息
//addr_len:地址信息结构长度
//返回值:成功返回实际发送数据长度,失败返回-1

UDP receiving data

ssize_t recvfrom(int sockfd,void* buf,size_t len,int flag,struct socket* src_addr,socket_t *addrlen);

//sockfd:套接字描述符
//buf:一块空间首地址,用于存放从内核获取到的数据
//len:要获取的数据长度
//flag:选项标志,默认为0----表示当前操作是阻塞操作
//src_addr:源端地址信息
//*addrlen:这是输入输出型参数,指定想要多长地址,返回实际长度地址

The TCP client initiates a connection to the server

int connect(int socket, struct sockaddr* srvaddr, socklen_t addrlen);
//srvaddr:服务器地址信息
//返回值:成功返回0,失败返回-1;

The TCP server listens to the connection sent by the client (parameters will be asked)

int listen(int socket, int backlog);
//backlog:同一时间最大并发连接数,当socket连接队列被放满之后,如果有新的连接请求到来则丢弃,以此限制同一时间所处理的新建连接请求数量;

//该操作将服务器端状态置为listen;

TCP get new connection

Take out a socket t that has been connected from the socket queue of the kernel that has completed the connection , and return the descriptor;

int accept(int listen_socket, struct sockaddr* addr, socklen_t *addrlen);
//listen_socket:监听套接字,决定了获取的是哪个套接字的新建连接;
//addr:一个地址结构的空间首地址,用于接收新连接的客户端地址信息;
//*addr:用于指定想要获取的地址长度以及返回的实际长度;
//返回值:成功返回新建连接的描述符,失败返回-1;

TCP sends data

ssize_t send(int sockfd, char *buf, int len, int flag);
//sockfd:新建套接字描述符;
//data:要发送的数据的空间首地址;
//len:要发送的数据长度;
//flag:0,默认阻塞接收;
//返回值:成功返回实际发送的数据长度,出错返回-1,

TCP receives data

The TCP communication socket saves the complete five-tuple ;

ssize_t recv(int sockfd, char *buf, int len, int flag);
//sockfd:新建套接字描述符;
//buf:空间首地址用于存放接收的数据;
//len:要获取的数据长度;
//flag:0,默认阻塞接收;
//返回值:成功返回实际获取到的数据长度,出错返回-1,连接断开返回0;

Guess you like

Origin blog.csdn.net/SFDWU3QVG/article/details/126819085