标题:求高手解决ftp服务器
只看楼主
火中精灵
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-8-17
 问题点数:0 回复次数:0 
求高手解决ftp服务器
一、                                 编写FTP服务器:
 

二、    连接协议:
所有ftp协议没有2进制数字,全部用文本格式收发。
发送协议:协议号+描述+CRLF
接收协议:协议键+描述+CRLF
其中,协议号和协议键是关键字,不能随意变换。
#define CRLF “\r\n”
CRLF作为校验使用,因此是必须出现在发送或者接收字串的结尾。
发送长度必须等于strlen或者string.length。发送长度也是校验条件之一。
协议键不区分大小写给以处理。
例如:创建ftp端口21,使用IE浏览器执行登录。
收到Accept连接后立即返回Send1,之后使用Receive接收你将收到Recv2。
依次向下收发,得到的测试结果如下:
Send1=220 127.0.0.1 FtpD for free
Recv2=USER test
Send3=331 Password required for test .
Recv4=PASS test
Send5=230 User test logged in , proceed
Recv6=opts utf8 on
Send7=502 Command OPTS utf8 on not implemented
Recv8=syst
Send9=215 Windows
Recv10=site help
Send11=502 Command SITE help not implemented
Recv12=PWD
Send13=257 "/" is current directory
Recv14=CWD /subing/
Send15=250 "/subing/" is current directory.
Recv16=TYPE A
Send17=200 Type set to ASCII.
Recv18=PASV
Send19=425 Cannot open data connection (192.168.1.13 0 create failed)
Recv20=TYPE A
Send21=200 Type set to ASCII.
Recv22=PORT 127,0,0,1,4,226
Send23=200 Port command okay.
Recv24=LIST
Send25=150 Opening ASCII NO-PRINT mode data connection for ls -l.
三、    协议内容:
每一个协议都是一个判断分支语句,得到是或否的结果再返回(发送)。

1、    Accept:
收到连接请求,根据对方的IP判断是否允许访问。
是:发送220协议,相关描述自定义。
否:关闭Accept产生的收发套接字(socka)。[关闭前也可以发送一条错误信息描述]
2、    USER:
判断描述字符串是否在用户列表中。
是:发送331协议,接续校对密码。
否:发送530协议,登录失败,关闭收发套接字。
3、    PASS:
根据上次收到的USER到用户列表<map>中去超找密码,判断和当前收到的PASS是否一致。
是:发送230协议。
否:发送530协议,登陆失败,关闭收发套接字。
其他协议,在没有完成登录的情况下收到,都不予以处理,返回530错误关闭套接字。
4、    Opts:
5、    Syst:
判断服务器的操作系统环境,返回215协议。
6、    Site:
7、    PWD:
判断FTP根目录是否存在。(例如:D:\FTP\.)
是:发送250协议,根目录可以使用。
否:发送503协议,根目录不能访问。
8、    CWD:
变更当前目录(Change Work Directory)。判断“根目录+子目录”是否存在。
是:发送250协议,当前目录已改变。
否:发送550协议,该目录无法访问。
9、    TYPE:
ftp可以浏览有2种传输格式,文本[A]和二进制格式[I]。
变更或保存对方要求的格式。[等到开始传输时以其中一种格式打开文件]
发送:200协议,格式类型已设置好。

10、    PASV:正向连接模式
创建另个侦听端口:用于接受对方connect,浏览文件信息端口。判断创建侦听端口成功。
成功:发送227协议,例如:Entering Passive Mode (192,168,1,13,4,41)
失败:发送425协议,创建失败。
格式:5个“,”分别代表IP4个段,端口2个段:高位,低位。
Accept产生一个收发端口(pasvSocket),等待发送文件信息。
11、    LIST:
列出文件信息。
发送前:发送150协议,准备开始发送。
发送后:发送226协议,文件信息发送成功。
中间:由pasvSocket连续该目录下所有文件的信息发送,之后关闭pasvSocket。
格式:目录是d,文件是-
Send18=drwxrwxrwx  1 test      nogroup            0 Aug 30 02:47 .
Send19=drwxrwxrwx  1 test      nogroup            0 Aug 30 02:47 ..
Send20=-rwxrwxrwx  1 test      nogroup            9 Aug 30 02:48 Test.txt

12、    NOOP:进入命令模式
发送200协议,Command okay。
13、    DELE:删除文件。
成功:250,失败:553
14、    RNFR:重命名文件开始。
记录OldName后,发送350,Requested new file name。
15、    RNTO:重命名结束:
成功:250,失败:553
16、    MKD:新建文件夹
成功:250,失败:553
17、    RMD:删除文件夹
成功:250,失败:553
18、    STOR:上传文件,
参数:文件名或(带相对路径的文件名)
根据TYPE(I或A)采用2进制或者文本模式,打开绝对路径的文件。
打开成功,使用协议通道(socka)发送150协议,失败发送426返回。
使用数据传输通道(sockt)循环接收数据,并根据接收返回值长度写入文件。
完成后关闭文件,使用协议通道发送226协议。
19、    RETR下载文件
参数:文件名或(带相对路径的文件名)
根据TYPE(I或A)采用2进制或者文本模式,打开绝对路径的文件。
打开成功,使用协议通道(socka)发送150协议,失败发送426返回。
循环读取文件,使用数据传输通道(sockt)根据读取返回值发送读取的数据
完成后关闭文件,使用协议通道发送226协议。
20、    SIZE:获取要下载文件的长度。
参数:文件名或(带相对路径的文件名)
获取按绝对路径的文件,如果文件存在发送213协议,后跟长度数字[描述]。
如果文件不存在也发送213协议,后跟0数字[描述]。
21、    PORT:反连接模式,跟PASV相反的模式
参数:IP和PORT
使用sockt创建0端口,并连接参数中的IP和PORT。
连接成功:发送200协议。 PORT command successful.
连接失败:发送425协议。 Cannot open data connection.
四、    参考资料:
Table 1.10 FTP Server Return Codes
Code    Meaning
119    Terminal not available, will try mailbox.
120    Service ready in nnn minutes.
125    Data connection already open; transfer starting.
225    Data connection open; no transfer in progress.
150    File status okay; about to open data connection.
151    User not local; will forward to user@host.
152    User unknown; mail will be forwarded by the operator.
250    Requested file action okay, completed.
200    Command okay.
211    System status, or system help reply.
212    Directory status.
213    File status.
214    Help message.
220    Service ready for new user.
221    Service closing Telnet connection.
226    Closing data connection; requested file action successful (for example, file transfer or file abort).
227    Entering passive mode.
230    User logged in; proceed.
331    User name okay; need password.
332    Need account for login.
350    Requested file action pending further information.
450    Requested file action not taken: file unavailable (for example, file busy).
421    Service not available, closing Telnet connection. This can be a reply to any command if the service must shut down.
425    Cannot open data connection.
426    Connection closed; transfer aborted.
530    Not logged in.
532    Need account for storing files.
550    Requested action not taken.

五、    参阅函数:
1、    _mkdir
2、    _rmdir
3、    _remove
4、    _rename
5、    _findfirst
6、    _findnext
7、    _findclose
8、    

六、    FTP Commands
!    Runs the specified command on the local computer.
?     Displays descriptions for ftp commands. Identical to help.
append    Appends a local file to a file on the remote computer, using the current file type setting.
ascii    Sets the file transfer type to ASCII, the default.
bell    Toggles a bell to ring after each file transfer command is completed. By default, the bell is off.
binary    Sets the file transfer type to binary.
bye    Ends the FTP session with the remote computer and exits ftp.
cd    Changes the working directory on the remote computer.
close    Ends the FTP session with the remote server and returns to the command interpreter.
debug    Toggles debugging. When debugging is on, each command sent to the remote computer is printed, preceded by the string --->. By default, debugging is off.
delete    Deletes files on remote computers.
dir    Displays a list of a remote directory’s files and subdirectories.
disconnect    Disconnects from the remote computer, retaining the ftp prompt.
get    Copies a remote file to the local computer, using the current file transfer type. Identical to recv.
glob    Toggles file name globbing. Globbing permits use of wildcard characters in local file or path names. By default, globbing is on.
hash    Toggles hash-mark (#) printing for each 2048 bytes data block transferred. By default, hash-mark printing is off.
help    Displays descriptions for FTP commands.
lcd    Changes the working directory on the local computer. By default, the current directory on the local computer is used.
literal    Sends arguments, verbatim, to the remote FTP server. A single FTP reply code is expected in return. Identical to quote.
ls    Displays an abbreviated list of a remote directory’s files and subdirectories.
mdelete    Deletes multiple files on remote computers.
mdir    Displays a list of a remote directory’s files and subdirectories. Allows you to specify multiple files.
mget    Copies multiple remote files to the local computer using the current file transfer type.
mkdir     Creates a remote directory.
mls     Displays an abbreviated list of a remote directory’s files and subdirectories.
mput     Copies multiple local files to the remote computer, using the current file transfer type.
open     Connects to the specified FTP server.
prompt    Toggles prompting. During multiple file transfers, ftp provides prompts to allow you to selectively retrieve or store files; mget and mput transfer all files if prompting is turned off. By default, prompting is on.
put    Copies a local file to the remote computer, using the current file transfer type. Identical to send.
pwd    Prints the current directory on the remote computer.
quit    Ends the FTP session with the remote computer and exits ftp.
quote    Sends arguments, verbatim, to the remote FTP server. A single FTP reply code is expected in return. Identical to literal.
recv    Copies a remote file to the local computer, using the current file transfer type. Identical to get.
remotehelp    Displays help for remote commands.
rename    Renames remote files.
rmdir    Deletes a remote directory.
send    Copies a local file to the remote computer, using the current file transfer type. Identical to put.
status    Displays the current status of FTP connections and toggles.
trace    Toggles packet tracing; displays the route of each packet when running an FTP command.
type     Sets or displays the file transfer type.
user     Specifies a user to the remote computer.
verbose    Toggles verbose mode. If on, all FTP responses are displayed; when a file transfer completes, statistics regarding the efficiency of the transfer are also displayed. By default, verbose is on.

七、    
八、
搜索更多相关主题的帖子: 服务器 ftp 
2009-08-17 10:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-282971-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.415536 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved