当前位置:网站首页>Client server communication project 1
Client server communication project 1
2022-04-22 08:36:00 【Sunspot ball】
1、 functional requirement :
1、 Realize the basic communication between client and server
2、 The client can read the file list of the server , And print on the client
3、 After the client enters the corresponding file name , The server can transfer the corresponding file to the client
4、 The server is transferring files , The client can open 、 Pause and terminate file transfers
5、 The client can obtain the configuration parameters of the server , Include the server name and the maximum number of client connections supported
6、 The client can modify the configuration parameters of the server , And save , Still effective after restart
7、 Implement multithreading processing mechanism , Parallel processing of multi client access
notes : Use static link libraries and Makefile Script Compilation project
2、socket Communication process
Socket That is what we call socket , It is one or a way of communication between computers . Through this Agreement , A computer can receive data from other computers , You can also send data to other computers .
The server is initialized first socket() function , And then bind to the port bind() function , Listen to the port listen() function , call accept() function , Wait for the client to connect . At this time, if a client initializes a socket() function , And then connect to the server connect() function , If the connection is successful , At this time, the connection between the client and the server is established . The client sends the request , The server receives the request and processes it , Then send the response data to the client , Client reads data , Finally, close the connection , An interaction ends .
3、socket Related functions
3.1、socket() function
int socket(int domain, int type, int protocol);
socket() Function to create a socket The descriptor , Take it as a parameter , You can perform some read and write operations , The parameters are defined as follows :
1、domain: Protocol family (AF_INET(IPv4)、AF_INET6(IPv6))
2、type:sockset type ,SOCK_STREAM(TCP)、SOCK_DGRAM(UDP)
3、protocol: Designated agreement , Specified as 0
3.2、bind() function
int bind(int sockfd, const struct sockaddr* addr, socklen_t addrlen);
bind() The function assigns a specific address in an address family to socket, The parameters are as follows :
1、sockfd:socket The descriptor , adopt socket() establish
2、addr: Point to bind to sockfd Agreement address for
3、addrlen: The size of the corresponding address structure
3.3、listen() function
int listen(int sockfd, int backlog);
socket() Function created socket The default is an active type ,listen() Function will socket Become passive , Waiting for the client to connect , The parameters are as follows :
1、sockfd:socket The descriptor , adopt socket() establish
2、backlog:socket The maximum number of connections that can be monitored at the same time
3.4、connect() function
int connect(int sockfd, const struct sockaddr* addr, socklen_t addrlen);
The client calls connect() Function to establish TCP Server connection , The parameters are as follows :
1、sockfd:socket The descriptor , adopt socket() establish
2、addr: Server's socket Pointer address structure
3、addrlen: Corresponding socket Length of address structure
3.5、accept() function
int accept(int sockfd, const struct sockaddr* addr, socklen_t addrlen);
The server calls... In turn socket()、bind()、listen() after , Will monitor the designated socket Address
The client calls... In turn socket()、connect() after , Sent a connection request to the server
After the server receives the request , Will call accept() Receiving request , And establish a connection , Return connected socket Description words
1、sockfd:socket The descriptor , adopt socket() establish
2、addr: Client's socket Pointer address structure
3、addrlen: Corresponding socket Length of address structure
notes : A server usually creates only one socket Description words , It exists throughout the life of the server . The kernel creates a connected connection for each client connection received by the server process socket Description words , When the server completes the service to a client , Corresponding connected socket Description word is closed .
3.6、write()/read() function
ssize_t write(int fd, const void *buf, size_t count);
ssize_t read(int fd, const void *buf, size_t count);
explain :
write Function will buf Data write in socket Data flow . Returns the number of bytes written on success , Return... On failure -1, And at the same time set error Variable
read The function can be derived from fd Read content in , Returns the number of bytes actually read when successful , return 0 Read to the end of the file , Less than 0 Indicates that there is a mistake
These two functions do not actually transmit data directly to the network , Instead, write the data to the cache first , Read again
1、fd:socket The descriptor , adopt socket() establish
2、buf: Address of the data buffer to be written or read
3、count: Number of data bytes written or read
3.7、close() function
int close(int fd);
After the read / write operation is completed, you need to close the socket, At this time, it can no longer be used as a read-write descriptor
版权声明
本文为[Sunspot ball]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220742498135.html
边栏推荐
猜你喜欢

leaflet、cesium加载百度地图,加载自定义样式百度地图

Teach you how to realize the pull-down refresh and pull-up load of recyclerview

第3关:节点配额及其他命令

【大话云原生】微服务篇-五星级酒店的服务方式

手把手教你实现RecyclerView的下拉刷新和上拉加载更多

机器学习笔记 - 主成分分析中的数学

Flutter GridView Demo

sonic云真机入门教程

1315. 祖父节点值为偶数的节点和(Medium)

记录node中使用sequelize,自动生成表模型,链接操作数据库(以postgreSQL为例)
随机推荐
617. 合并二叉树(Easy)
华为机试题——HJ53 杨辉三角的变形
Mapbox设置官方地图语言为中文
235. 二叉搜索树的最近公共祖先(Easy)
leaflet、cesium加载百度地图,加载自定义样式百度地图
函数指针和指针函数
Viewpager comprehensive summary
空心字母金字塔
ospf四类,五类和七类LSA详解
94. 二叉树的中序遍历(Easy)
二进制的前导的零
oracle 数据库常识以及使用
ViewPager 全面总结
shell脚本中ps -ef查询进程PID一直返回异常
猴子吃桃子问题(循环、递归)
Make the airtest ide into a script and make the script run
指针和字符串
第3关:节点配额及其他命令
shell脚本学习笔记——shell对文件的操作sed
Single page application(单页面应用)