当前位置:网站首页>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;
}
边栏推荐
- JS learning notes - data types
- Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
- Summary of multithreading and thread synchronization knowledge
- 中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
- ⌈ 2022 ⌋ how to use webp gracefully in projects
- Multi task prompt learning: how to train a large language model?
- 触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
- 请问怎么在oracle视图中使用stustr函数
- How to choose the right kubernetes storage plug-in? (09)
- [fluent] dart language (DART language features | JIT instant compilation | AOT static compilation)
猜你喜欢
The login box of unity hub becomes too narrow to log in
MySQL min() finds the minimum value under certain conditions, and there are multiple results
SSM integration exception handler and project exception handling scheme
JS learning notes - data types
Memory alignment of structure
How to solve the failure of printer driver installation of computer equipment
Aujourd'hui dans l'histoire: Alipay lance le paiement par code à barres; La naissance du père du système de partage du temps; La première publicité télévisée au monde...
月报总结|Moonbeam6月份大事一览
Original God 2.6 server download and installation tutorial
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
随机推荐
Sim2real environment configuration tutorial
[fluent] dart language (DART language features | JIT instant compilation | AOT static compilation)
路由模式:hash和history模式
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
What if the win11 app store cannot load the page? Win11 store cannot load page
Yyds dry inventory KVM new inventory to expand space for home
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
Yyds dry inventory executor package (parameter processing function)
Typescript array out of order output
Cloud native cicd framework: Tekton
做机器视觉哪个软件好?
电脑设备打印机驱动安装失败如何解决
Take you ten days to easily complete the go micro service series (I)
Route service grid traffic through two-level gateway design
2022最新最详细必成功的在Vscode中设置背景图、同时解决不受支持的问题
(practice C language every day) the sum of the nearest three numbers
Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification
Yyds dry inventory method of deleting expired documents in batch