关于ftp客户端:如何从FTP服务器读取多行响应?

How to read multiline response from FTP server?

我正在用C语言编写FTP客户机程序。我使用阻塞套接字。我在while循环中放入了recv (),当最后两个接收到的字符是

时将其中断。我可以在一些服务器上运行它,但在一些服务器上,比如ftp.secyt.gov.ar,它不能读取整个消息。

我认为问题是因为来自服务器的消息包含字符

在一起。

我该如何处理这种情况?

将用户名和密码发送到ftp.secyt.gov.ar的服务器后,我想打印从服务器收到的消息。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
password = getpass("Password:");
sprintf(pass,"PASS %s

",password);

send(sockfd, pass, strlen(pass), 0);

while((no_of_bytes = recv(sockfd, message_from_server, MAXSZ, 0)) > 0)
{
    message_from_server[no_of_bytes] = '\0';
    printf("%s
", message_from_server);

    if (message_from_server[no_of_bytes-2] == '
' &&
        message_from_server[no_of_bytes-1] == '
')
        break;
}

服务器发送此消息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
230-=====================================================================

    BIENVENIDOS AL SERVIDOR FTP DE LA MINCyT  
             ----------------------------------------

Usuario anonymous, desde la maquina ::ffff:116.203.73.60, gracias por
utilizar el FTP del Ministerio de Ciencia, Tecnologia e
Innovacion Productiva.

Para sugerencias, consultas o informacin adicional nuestros correos
electrnicos son:
                     [email protected]

=========================================================================
230 User anonymous logged in.

但它只是在打印:

1
230-=====================================================================


您的代码从服务器读取一行(由

终止的字符串)。

但是根据ftp规范,ftp响应可以是多行的。

参见RFC 959第4.2节FTP回复:

Thus the format for multi-line replies is that the first line
will begin with the exact required reply code, followed
immediately by a Hyphen,"-" (also known as Minus), followed by
text. The last line will begin with the same code, followed
immediately by Space , optionally some text, and the Telnet
end-of-line code.

For example:

1
2
3
4
 123-First line  
 Second line  
   234 A line beginning with numbers  
 123 The last line

The user-process then simply needs to search for the second
occurrence of the same reply code, followed by (Space), at
the beginning of a line, and ignore all intermediary lines. If
an intermediary line begins with a 3-digit number, the Server
must pad the front to avoid confusion.

另请参阅如何知道ftp欢迎消息的结尾。