1

我尝试制作简单的 ftp 客户端来获取 bsd 套接字上的文件列表。这里是:

connectTo(int client_socket, 
              struct sockaddr_in* addr,
              char* ipv4Address,
              char* user,
              char* password,
              char* response) // buffer wchich store responses
{
    client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if(socket < 0)
    {
        printf("Can not create socket");
        return 2;
    }

    addr->sin_family = AF_INET; // address family - internet socket
    addr->sin_port = htons(PORT_NUMBER);
    addr->sin_addr.s_addr = inet_addr(ipv4Address);
    if(connect(client_socket,(struct sockaddr*)addr,sizeof(*addr)) < 0)
    {
        printf("Can not connect!\n");
        return 3;
    }

    int size_read = recv(client_socket, response, BUF_SIZE, 0);

        printf("size_read = %d\n",size_read);
        response[size_read] = '\0'; // for not printing rubbish
        printf("%s\n",response);

    printf("sending username\n");
    char *username = strdup("USER trenkinan\r\n");
    send(client_socket,username,strlen(username)+1,0);


    size_read = recv(client_socket, response, BUF_SIZE, 0);

   //     printf("size_read = %d\n",size_read);
        response[size_read] = '\0'; // for not printing rubbish
        printf("%s",response);
    if(strstr(response,"331"))
    {
        //printf("sending password\n");
        char *passwd = strdup("PASS test\r\n");

       // printf("passwd string: %s",passwd);
        int sent = send(client_socket,passwd,strlen(passwd)+1,0);
        size_read = recv(client_socket, response, BUF_SIZE, 0);
        response[size_read] = '\0'; // for not printing rubbish
        printf("%s",response);

        //printf("sent bytes: %d\n", sent); 
    }

    return 0;
}

我在我的工作站上使用来自 debian 存储库(ftpd)的 ftp 服务器。我尝试执行此代码,当我发送密码服务器时回答 500 '':命令不理解。我使用wireshark查看会发生什么,我的程序中的所有包看起来都很好,我使用ftp命令(Linux上的轻量级ftp客户端)进行连接,它可以工作,但来自该客户端的包看起来与我自己的客户端相同。有任何想法吗?

4

1 回答 1

0

所以,我发现了一个错误:strlen(username) 而不是 strlen(username)+1。我希望它对某人有所帮助。

于 2012-11-09T18:45:18.173 回答