当前位置:网站首页>TCP server communication process (important)
TCP server communication process (important)
2022-07-02 16:37:00 【Soy sauce;】
1.TCP Introduce
Transmission control protocol
characteristic :
Error re threading , Each time you send data, the other party will reply ACK
Safe and reliable
2. Server client TCP Communication model

client : Call model ( Establishing a connection , Use connections , Close the connection )
Create socket ( Customer phone )– Connect to server ( Connect , dial )– Sending and receiving data ( Reading and writing is right socket File operations )( Talking and communicating )– close ( Hang up )
Correlation function description
1. socket function – Semantics : Create socket
#include <sys/types.h> / See NOTES /
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
Specific parameters
domain:(socket Choice of agreement )
AF_INET This is most used to produce socket The agreement , Use TCP or UDP To transmit , use IPv4 The address of
AF_INET6 Similar to the above , It's just for use IPv6 The address of
AF_UNIX Local agreement , Use in Unix and Linux On the system , Generally, it is used when the client and server are on the same platform and on it
type:( Socket type )
SOCK_STREAM The agreement is in order 、 reliable 、 Data complete connection based on byte stream . This is one of the most used socket type , This socket It's using TCP To transmit .
SOCK_DGRAM This protocol is connectionless 、 Fixed length transfer calls . It's not a reliable agreement , Use UDP To connect it .
SOCK_SEQPACKET The agreement is two-way 、 Reliable connection , Sending packets of fixed length for transmission . The package must be fully accepted before it can be read .
SOCK_RAW socket Type provides a single network access , This socket Type used ICMP Public agreement .(ping、traceroute Use this Agreement )
SOCK_RDM This type is rarely used , It's not implemented on most operating systems , It is provided to the data link layer for use , The order of packets is not guaranteed
protocol:
Pass on 0 Indicates that the default protocol is used .
Return value :
success : Returns to the newly created socket File descriptor for sockdf, For later use ,
Failure : return -1, Set up errno
describe
socket() Open a network communication port , If it works , It's like open() Also returns a file descriptor , Applications can be used just like reading and writing files read/write Sending and receiving data on the Internet , If socket() Call error, return -1. about IPv4,domain Parameter specified as AF_INET. about TCP agreement ,type Parameter specified as SOCK_STREAM, Represents a stream oriented transport protocol . If it is UDP agreement , be type Parameter specified as SOCK_DGRAM, Represents a datagram oriented transport protocol .protocol The introduction of parameters is omitted , Designated as 0 that will do .
2.connect function – Semantics : Connect to server
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
sockdf:
socket File descriptor , The descriptor just created
addr:
Pass in the parameter ,ipv4/ipv6 General socket structure address ( Specify server-side address information , contain IP Address and port number ) If the type is wrong, force it , Turn to the general structure address
addrlen:
Pass in the parameter , Pass in sizeof(addr) size , The length of the incoming socket structure
Return value :
Successfully returns 0, Failure to return -1, Set up errno
The client needs to call connect() Connect to server ,connect and bind The form of the parameter is consistent , The difference lies in bind The parameter of is its own address , and connect Is the address of the other party .connect() Successfully returns 0, Error return -1
Example of procedure code ( a key )
#include <unistd.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int main(int argc, char *argv[])
{
// Create socket
int sock_fd;
sock_fd = socket(AF_INET,SOCK_STREAM,0);
// establish ipv4 Socket structure -- Assign the three elements of the server to the client
struct sockaddr_in addr;
addr.sin_family = AF_INET;// Set up ipv4 Address format
addr.sin_port = htons(8080);// Host byte order to network byte order
inet_pton(AF_INET,"192.168.21.29",&addr.sin_addr.s_addr);// Convert dotted decimal string into network big end
// Connect to server
connect(sock_fd,(struct sockaddr *)&addr,sizeof(addr));//connect To convert to a generic socket structure type
char buf[1024]="";
while(1)
{
int n = read(STDIN_FILENO,buf,sizeof(buf));
write(sock_fd,buf,n);// Writing data
n = read(sock_fd,buf,sizeof(buf));// Reading data
write(STDOUT_FILENO,buf,n);
printf("\n");
}1
// Close socket
close(sock_fd);
return 0;
}
边栏推荐
- 渗透工具-内网权限维持-Cobalt strike
- Multi task prompt learning: how to train a large language model?
- Routing mode: hash and history mode
- Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
- sim2real环境配置教程
- Seal Library - installation and introduction
- Practice of traffic recording and playback in vivo
- Effectively use keywords to increase Amazon sales
- 月报总结|Moonbeam6月份大事一览
- ⌈ 2022 ⌋ how to use webp gracefully in projects
猜你喜欢

Routing mode: hash and history mode

Write your own CPU Chapter 11 - learning notes

Route service grid traffic through two-level gateway design

⌈ 2022 ⌋ how to use webp gracefully in projects

数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙

Set the background picture in the idea (ultra detailed)

MySQL calculates the data within the longitude and latitude range

JS learning notes - process control

原神2.6服务端下载以及搭建安装教程

关于mysql安装的一些问题
随机推荐
LeetCode 6. Z 字形变换 (N字形变换)
Where can I open computer administrator permissions
Recalling the college entrance examination and becoming a programmer, do you regret it?
Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
sim2real环境配置教程
mysql min() 求某条件下最小的值出现多个结果
路由模式:hash和history模式
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
LeetCode 3. 无重复字符的最长子串
ROW_ NUMBER()、RANK()、DENSE_ Rank difference
Typescript array out of order output
Leetcode -- number of palindromes
Cloud native cicd framework: Tekton
图书管理系统(山东农业大学课程设计)
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
Maui学习之路(三)--Winui3深入探讨
一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
Route service grid traffic through two-level gateway design