当前位置:网站首页>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;
}
边栏推荐
- 学生选课系统(山东农业大学课程设计)
- Vscode设置标签页多行显示
- Maui学习之路(三)--Winui3深入探讨
- 自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
- Add user-defined formula (time sharing t+0) to mobile app access as an example
- La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
- Practice of constructing ten billion relationship knowledge map based on Nebula graph
- sim2real环境配置教程
- Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
- Recalling the college entrance examination and becoming a programmer, do you regret it?
猜你喜欢
Headline | Asian control technology products are selected in the textile and clothing industry digital transformation solution key promotion directory of Textile Federation
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
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
Effectively use keywords to increase Amazon sales
Seal Library - installation and introduction
mysql数据库mysqldump为啥没有创建数据库的语句
Recommended practice sharing of Zhilian recruitment based on Nebula graph
What is Amazon keyword index? The consequences of not indexing are serious
Practice of traffic recording and playback in vivo
渗透工具-内网权限维持-Cobalt strike
随机推荐
Unity Json 编写
Mobile web development learning notes - Layout
绝对真理和相对真理思考
dried food! Understand the structural vulnerability of graph convolution networks
day4
Bone conduction non ear Bluetooth headset brand, bone conduction Bluetooth headset brand recommendation
自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
PCL least median square method fitting plane
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
LeetCode 1. 两数之和
Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
mysql min() 求某条件下最小的值出现多个结果
PCL 最小中值平方法拟合平面
结构体的内存对齐
Routing mode: hash and history mode
学生选课系统(山东农业大学课程设计)
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
SSM integration exception handler and project exception handling scheme