c++ - 将ftp PASS write进去出错:500 Invalid command: try being more creative
高洛峰
高洛峰 2017-04-17 13:14:32
0
0
897

我用boost的asio来尝试一下写个ftp,于是先测试怎么登陆。然后死活登不上去。

/* 账号和密码都没错*/
#include <iostream>
#include <boost/asio.hpp>
#include <string>
#include <boost/array.hpp>

using namespace boost::asio;

io_service service;
ip::tcp::endpoint ep(ip::address::from_string("172.28.89.13"), 21);
ip::tcp::socket sock(service);
boost::array<char, 4096> buf;

void read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred)
{
    if (!ec)
    {
        std::cout << std::string(buf.data(), bytes_transferred) << std::endl;
        sock.async_read_some(buffer(buf), read_handler);
    }
    else
    {
        std::cerr << "read error!" << std::endl;
    }
}

void connect_handler(const boost::system::error_code &ec)
{
    if (!ec)
    {
        sock.async_read_some(buffer(buf), read_handler);
    }
    else
    {
        std::cerr << "connect error!" << std::endl;
    }
}

int main(void)
{
    char sendbuf[100];
    char readbuf[100];
    
    sock.async_connect(ep, connect_handler);

    sprintf(sendbuf, "USER %s\r\n", "temp");
    write(sock, buffer(sendbuf));
    sock.async_read_some(buffer(buf), read_handler);

    sprintf(readbuf, "PASS %s\r\n", "");
    write(sock, buffer(readbuf));
    sock.async_read_some(buffer(buf), read_handler);
    
    service.run();

    return 0;
}

然后出错:

[fusae@localhost swget]$ g++ test1.cpp -o test1 -lboost_system -lpthread
[fusae@localhost swget]$ ./test1
220 ProFTPD 1.3.4a Server (Ubuntu) [172.28.89.13]

331 Password required for temp
500 Invalid command: try being more creative

用C语言再测试了一遍,倒也没错。

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define oops(msg) {perror(msg); exit(1);}

int main(int ac, char *av[])
{
    struct sockaddr_in servadd;
    struct hostent *hp;
    int sock_id, sock_fd;
    char message[BUFSIZ];
    int messlen;
    char send_buf[BUFSIZ];
    char read_buf[BUFSIZ];

    sock_id = socket(AF_INET, SOCK_STREAM, 0);
    if (sock_id == -1)
        oops("socket");

    bzero(&servadd, sizeof(servadd));
    
    hp = gethostbyname(av[1]); // ip
    if (hp == NULL)
        oops(av[1]);

    bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);
    servadd.sin_port = htons(atoi(av[2])); // port
    servadd.sin_family = AF_INET;

    // 连接
    if (connect(sock_id, (struct sockaddr *)&servadd, sizeof(servadd)) != 0)
        oops("connect");

    messlen = read(sock_id, message, BUFSIZ);
    printf("%s\n", message);

    // 账号
    sprintf(send_buf, "USER %s\r\n", "temp");
    write(sock_id, send_buf, strlen(send_buf));
    read(sock_id, read_buf, BUFSIZ);
    printf("%s\n", read_buf);

    // 密码
    sprintf(send_buf, "PASS %s\r\n", "");
    write(sock_id, send_buf, strlen(send_buf));
    read(sock_id, read_buf, BUFSIZ);
    printf("%s\n", read_buf);

    return 0;
}
[fusae@localhost swget]$ ./test2 172.28.89.13 21
220 ProFTPD 1.3.4a Server (Ubuntu) [172.28.89.13]

331 Password required for temp

230 User temp logged in
 temp

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

모든 응답(0)
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿