latelee@localhost:~$ ftp 192.168.18.88 Connected to 192.168.18.88. 220 Serv-U FTP Server v6.0 for WinSock ready... Name (192.168.18.88:latelee): latelee 331 User name okay, need password. Password: 230 User logged in, proceed. Remote system type is UNIX. Using binary mode to transfer files. ftp> passive // pasv模式 Passive mode on. ftp> ls 227 Entering Passive Mode (192,168,18,88,25,172) 150 Opening ASCII mode data connection for /bin/ls. drw-rw-rw- 1 user group 0 May 4 18:04 . drw-rw-rw- 1 user group 0 May 4 18:04 .. drw-rw-rw- 1 user group 0 May 4 18:22 test 226 Transfer complete. ftp> LIST // 无此命令 ?Invalid command ftp> list // 无此命令 ?Invalid command
服务器对应命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Connected to 192.168.18.111 (Local address 192.168.18.88) 220 Serv-U FTP Server v6.0 for WinSock ready... USER latelee 331 User name okay, need password. PASS xxxxx User LATELEE logged in 230 User logged in, proceed. SYST 215 UNIX Type: L8 PASV 227 Entering Passive Mode (192,168,18,88,44,213) LIST 150 Opening ASCII mode data connection for /bin/ls. 226 Transfer complete. QUIT 221 Goodbye! Closing connection for user LATELEE (00:00:15 connected)
二、主动模式
代码流程: 客户端:生成随机的大于1024的端口NUM,发PORT A,B,C,D,n,m\r\n命令到21端口(其中A,B,C,D为本机IP,n=NUM/256,m=NUM%256) 服务器:通过21端口向客户端发送是否成功的信息(成功:200 PORT Command successful.) 客户端:监听上面生成的NUM端口,如有连接,则Accept得到新的socket,数据通道即使用该socket。 注:资料2的文章说在监听NUM+1的端口,但我实际测试使用NUM可以,而监听NUM+1即不行,不知原因为何。另外,“PORT A,B,C,D,n,m\r\n”最后的回车换行和之前的字符是不能有空格的!因为写代码时是用sprintf组装字段的,习惯性多带个空格。正确的是示例如下:
latelee@localhost:~$ ftp 192.168.18.88 Connected to 192.168.18.88. 220 Serv-U FTP Server v6.0 for WinSock ready... Name (192.168.18.88:latelee): latelee 331 User name okay, need password. Password: 230 User logged in, proceed. Remote system type is UNIX. Using binary mode to transfer files. ftp> ls 200 PORT Command successful. 150 Opening ASCII mode data connection for /bin/ls. drw-rw-rw- 1 user group 0 May 4 18:04 . drw-rw-rw- 1 user group 0 May 4 18:04 .. drw-rw-rw- 1 user group 0 May 6 11:24 test 226 Transfer complete. ftp> LIST ?Invalid command ftp> list ?Invalid command ftp>
服务器对应命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Connected to 192.168.18.111 (Local address 192.168.18.88) 220 Serv-U FTP Server v6.0 for WinSock ready... USER latelee 331 User name okay, need password. PASS xxxxx User LATELEE logged in 230 User logged in, proceed. SYST 215 UNIX Type: L8 PORT 192,168,18,111,175,229 200 PORT Command successful. LIST 150 Opening ASCII mode data connection for /bin/ls. 226 Transfer complete. QUIT 221 Goodbye!