当前位置:网站首页>2022-7-17 FTP client project implementation - Summary
2022-7-17 FTP client project implementation - Summary
2022-07-22 01:43:00 【Another start】
2022-7-17 Curriculum FTP Client implementation - summary
- One . brief introduction
- Two . Project design and implementation
- 3、 ... and . General layout of the project (2022717)
- Four . System operation and testing
- 5、 ... and . Difficulties encountered in implementation and pit records
- Directly use string to pass instructions ( Define various states with macros ):
- use memset Initialize the output array :
- The end of the string appears inexplicably in the string transmission `/377/177` problem :
- write Write overflow and read Read overflow of :
- open Of mode Permission problems caused by bit :
- Debugging problems of multi process server :
- 6、 ... and . Personal experience
- 7、 ... and . appendix
One . brief introduction
FTP client (Linux C++, https://gitee.com/tracker647/linftp)
Project description : The project simulation FTP Communication process between client and server , have shell, The Internet , File system and other modules , It realizes the ls,cd,put and get function , Consider adding login verification in the future , Multithreading and mkdir and rmdir Wait for more instructions to support .
Personal gain : Through this project, for Linux Files and network related API More familiar with , Understand the basic implementation of network protocol , At the same time, it deepens the understanding of TCP Understanding of transmission characteristics and network transmission principle .
Two . Project design and implementation
Plan to realize the function
Instructions | function | Implementation |
---|---|---|
ls | Show ftp Server current directory file , Default to root | |
cd | Switch remote directory | |
help | List of instructions | |
open | Connection designation IP | |
mkdir | Create a remote directory | |
rmdir | Delete remote directory | |
get | Download the file | |
put | Upload files | |
quit | Exit the system | |
status | Show client status | |
close | disconnect |
Other functional features of the instruction have not been considered :
validate logon ( I didn't do it for the convenience of debugging )
Multi client support ( At present, I have tried too many processes -> To learn about vscode The method of debugging multiple processes , Try multithreading later )
The direction key pops up the historical command ( Once wrote a historical instruction queue , but Linux C There is no processing library for each key interrupt , except Ctrl+C, This part has a nurse Library implements , But I'm too lazy to introduce external libraries , To give up )
Function realization ideas
ls Function realization :
The client sends ls The signal waits for the server to respond
If the server responds normally , Then the client enters the data receiving state
The server passes the current path into the corresponding function of the file system , This function returns the string of all file names of the current directory according to the current path
The current directory information , The file name set is integrated into the server write buffer , Send to client
After the client reads, the information of the current directory of the server is displayed
cd Function realization :
receive ”./” , “…/”, Three outputs of relative path
about “./” Go straight back to , Let the client display the current directory
about “…/” If it is already in the root directory , Inform the client and return , Otherwise, get the path of the upper level directory and change the current directory of the server to the upper level directory , And tell the client
For relative paths , Combine with the current path and pass open Check its path validity , If the path is valid , Then copy the current path as the new path after the combination of the current path and the relative path , And tell the client
put Function realization :
- Before transmission starts , The client enters the ready state and informs the server
- The client opens the file corresponding to the path , If opening fails , Return abnormal information and inform the server to interrupt the transmission
, Otherwise, inform the server that the input stream is successfully opened , Make the server open the output stream
- If the server fails to open the output stream , Inform the client to interrupt the transmission , Otherwise, tell the client to open the input stream
- After both write streams and read streams are turned on , The client informs the server of the length of each field of data ( Include the file name , file size ), And start transmission according to the length of each field
- The server receives byte by byte , If there is an exception in the receiving process , Inform the client of the transmission failure and interrupt the transmission
- After the data length of the notification is transmitted, the transmission of the two segments will be completed normally
get Function realization :
- The client sends the path to the server , Ask the server about the path validity , If the path is valid , Then the server notifies the client that the input stream can be opened normally , Otherwise, the path is illegal , Interrupt transmission .
- If the client opens the input stream, it tells the server to start data transmission , Otherwise, inform that the opening fails , Interrupt transmission .
- The server informs the client of the length of each field of data ( Include the file name , file size ), And start transmission according to the length of each field
- Client receives byte by byte , If there is an exception in the receiving process , It is necessary to inform the server of the transmission failure and interrupt the transmission
- After the data length of the notification is transmitted, the transmission of the two segments will be completed normally
3、 ... and . General layout of the project (2022717)
Icon
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-wkRVXgKU-1658071604140)(https://s2.loli.net/2022/07/17/b4RT6mklJAVD75t.png)]
Directory structure
linftp/
├── ftp_client
├── ftp_server
├── makefile
├── obj/
│ ├── client_shell.o
│ ├── error.o
│ ├── filesys.o
│ ├── ftp_client.o
│ ├── ftp_server.o
│ ├── network.o
│ └── server_backend.o
├── serverRootDir/
└── src/
├── client_shell.c
├── client_shell.h
├── cmd_code.h
├── filesys.c
├── filesys.h
├── ftp_client.c
├── ftp_server.c
├── network.c
├── network.h
├── server_backend.c
└── server_backend.h
Introduction to main documents
serverRootDir/ ftp Server file root directory
ftp_client Client front end , The main command functions are client_shell Realization
cmd_code shell be used for shell Macro definition of the instruction
client_shell The specific implementation of instructions on the client , Including local instructions and instructions that can only be used after connecting to the server
network Be responsible for the packaging and implementation of networking functions , Including client connection and server socket initialization
filesys The file system of the server , Macro definition and function implementation
ftp_server Server front end , The main functions are server_backend Realization
server_backend Server back end , It is used to carry the main functions of the server , Receive client remote instructions
Four . System operation and testing
General interface of server and client
Client calls ls, cd function
Client calls put function
Client calls get function
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-8eICzBV7-1658071604152)(https://s2.loli.net/2022/07/17/eh43vOWwtuLZVED.jpg)]
5、 ... and . Difficulties encountered in implementation and pit records
Directly use string to pass instructions ( Define various states with macros ):
Cause various problems , Including the variable length of characters increases the difficulty of programming , Wild characters invalidate the comparison , Finally, I decided to replace the string instruction with the instruction code , Various macros are defined in the header file , Later, various states of the file system also use this definition .
use memset Initialize the output array :
Realization ls When the command , Find out ls After root directory ,cd And then go to the next level of directory ls,ls The files in the current directory and the files in the root directory will be output together , The reason is that the memory may not be destroyed after the function exits , Some memory will be left by the operating system for future reuse , Finally through memset In every run ls Initializing the output array solves this problem .
Knowledge point :free Free memory , Will it be returned to the operating system ?
The end of the string appears inexplicably in the string transmission /377/177
problem :
After defining the character array, use memset Initialize it .
write Write overflow and read Read overflow of :
During echo test, both parties directly communicate data in the unit of defined cache macro , The result has brought many problems , Specific examples are as follows :
char *mes1 = "Hello World";
char *mes2 = "SINCE WHEN YOU ARE THE ONE IN CONTROL?" ;
char *mes3 = "LOOK A DEER";
int filefd = open("sockfd",O_RDWR, O_TRUNC);
write(filefd,mes1,500);
The above write Will not only read mes1, Will also mes2 and mes3 And all kinds of garbage characters 500 Read the word into the file .
And the following code
// client
int datalen = statbuf.st_size;
write(servfd,filename,strlen(filename)); //5
write(servfd,filedata,datalen); //50
// Server side
#define READ_BUFFER_SIZE 500
read(servfd,filename,READ_BUFFER_SIZE)
It will cause the server to read all the file names and file data filename Array , Cause subsequent logical exceptions .
The above reasons are all because TCP The transmission mode of is Borderless Of , It means that the reading and writing of data are not limited by the scope , once write It doesn't necessarily correspond to one read, Maybe several times read go ( For example, the incoming string is unpacked ), and N Each attribute corresponds to one write common N One output may also be read go ( Cause sticking problem ).
Therefore, every kind of information in network communication , It's best to inform each other how many bytes of data they will send ( For example, the last file , The client sends the file name in turn , file name , Long data , Finally, data transmission ), Guarantee read and write One by one , Avoid unpacking and sticking , To ensure the normal operation of network programs .
Another way is to add custom delimiters between each kind of information , But I haven't tried .
open Of mode Permission problems caused by bit :
Realization put and get When I found a strange phenomenon , Any file with the same name already exists in the corresponding path of the receiving end will cause an exception , Because I was programming on ECs , The entire root, I didn't find this problem , Later, when I switched to virtual machine programming, I found that it was my permission problem , When the file browser looks at the newly created files, they are all crossed and locked , Last to open On the function , A: , Find out mode The calculation method of bit pair permission is mode & ~umask
, Not like it chmod That way, you can modify it directly , Modified according to the reference mode Behind you , Problem solving .
Debugging problems of multi process server :
I have tried to implement the server in the way of multiple processes before , The principle is that when there is a new connection, the parent process will be different fork Create a sub process for the session , By default, the debugger can only track the parent process , Unable to check the execution of the child process , The solution will be gdb Set the instruction to set follow-fork-mode child
( stay vscode Above is the setting launch.json Corresponding to the configuration setupCommands
attribute , See the project source code for details ), So the debugger will be in fork Automatically switch to the child process .
6、 ... and . Personal experience
Don't be confident that you are ahead of schedule git Can upgrade the function code , Once the writing collapses, you will feel better , Therefore, I am confident that I can lcd Write it out to modify the molded cd function , As a result, it took me all night to cd The function of is restored .
Take the first , A better way to upgrade code is budding , For example, to modify xxx_fun function , It should be written separately xxx_fun2 To test , There are many ideas to test, you can also write xxx_fun3, xxx_fun4... In this way, in case of writing failure, you can directly change back to the original function .
Don't think about writing the best code at the beginning , You always want to package or simplify , Practice with limited knowledge often leads to products full of loopholes , Simplified functions , As the amount of code increases, it will become a piece of shit , You will feel that annotations are more important . Besides, no matter what code you are , Work N When you look back years later, you will feel like a lump of garbage .
If you are clever enough to define a state macro for your known file exceptions, you might as well ask errno.
Write put and get Feeling : Don't let go of every possible exception in the process , Keep a journal !
printf Logs may provide more than GDB Also unexpected effect .
It is more intuitive and helpful to record the implementation of your project directly with paper and pen , When I implement this project, I often write my ideas in paper , You can often see , Electronic notes should be used for later summaries .
7、 ... and . appendix
development environment
System :Ubuntu 20.04 LTS
IDE:vscode
Reference resources
https://blog.csdn.net/qq_24889575/article/details/81566164
《UNIX Environment programming 》
《UNIX Network programming 》
边栏推荐
- 记一次远程Debug调试Minecraft服务器插件经历
- 每个博主都需要问自己的7个基本问题
- 慧荣科技与江波龙协同提升手机存储竞争力
- 【集训DAY8】Interesting Number 【数位DP】
- 【集训DAY10】Linear【数学】【思维】
- Jugement des chaînes vides dans Oracle
- TinyMCE removes the P tag added by default in the editor line feed
- NepCTF2022 WP
- Major breakthrough! Successful development of the first domestic scientific computing software
- Idea running @test cannot be input from the console and is in the loading state
猜你喜欢
[wechat applet] solve the problem that the code upload exceeds the size limit and the applet is subcontracted
LeetCode 10. Regular Expression Matching
c语言:查漏补缺(一)
OSPF知识总结
Easyexcel is easy to use
05-1. Default member function: copy constructor, assignment operator overload
Dynamic rules - array compression optimization
SpaceX与芭比娃娃制造商美泰达成主题玩具产品协议
【集训DAY9】Maze【线段树】
【集训DAY10】Tree【区间DP】
随机推荐
3、Nacos 配置中心源码解析之 项目结构
Rust learning notes -rust language foundation
Spa single page learning and understanding
3. Project structure of source code analysis of Nacos configuration center
C language classic 100 questions (1-10 questions) (including answers)
群晖7.1.0万兆网络实测
【集训DAY6】Game【数学】
【集训DAY9】Light Tank【动态规划】
c语言:查漏补缺(一)
预处理——异常值检测
微店构造订单页面,自动化抢票
二维费用的背包问题(01背包)
动态规划多重背包一维
动态规划背包问题——01背包
屏幕共享软件--Deskreen
Huirong technology and jiangbolong work together to improve the competitiveness of mobile phone storage
1、Nacos 配置中心源码解析之 Hello World
Oracle中對空字符串的判斷
[Pengcheng cup 2022] baby_ re
Using completable future to implement asynchronous callback