当前位置:网站首页>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;
}
边栏推荐
- Typescript array out of order output
- [fluent] dart data type boolean type (boolean type definition | logical operation)
- According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
- 618深度複盤:海爾智家的制勝方法論
- JS learning notes - variables
- Which software is good for machine vision?
- LeetCode 3. 无重复字符的最长子串
- 自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
- Ranger (I) preliminary perception
- Source code look me
猜你喜欢

⌈ 2022 ⌋ how to use webp gracefully in projects

Register as a harmonios developer and install deveco studio 3.0 beta2 for harmonios

The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines

The light of ideal never dies

Mysql database mysqldump why there is no statement to create a database

sim2real环境配置教程

2022 the latest and most detailed will successfully set the background image in vscade and solve unsupported problems at the same time

JS learning notes - data types

Data security industry series Salon (III) | data security industry standard system construction theme Salon

潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
随机推荐
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
Written by unity Jason
[fluent] dart data type string type (string definition | string splicing | string API call)
Source code look me
Unity Json 编写
去除router-link中的下划线
虚假的暑假
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
数学分析_笔记_第6章:一元函数的Riemann积分
电脑管理员权限在哪里可以打开
ROW_NUMBER()、RANK()、DENSE_RANK区别
外企高管、连续创业者、瑜伽和滑雪高手,持续迭代重构的程序人生
Add user-defined formula (time sharing t+0) to mobile app access as an example
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
Cloud native cicd framework: Tekton
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
Write your own CPU Chapter 11 - learning notes
PyC file decompile