C ++ FTP服务器无法打开客户端

时间:2015-05-17 22:26:56

标签: c++ linux sockets ftp

我正在使用linux编程FTP服务器。问题是当它获得PORT命令时我正试图用客户端打开一个套接字,但是当我在LIST命令中写入它时没有任何反应:

else if (COMMAND("PORT")) {            
            int h1,h2,h3,h4,p1,p2;
            //h* is a byte from the host IP address
            //p* is a byte from the port number
            fscanf(fd, "%i, %i, %i, %i, %i, %i", &h1,&h2,&h3,&h4,&p1,&p2);
            printf("%i %i %i %i %i %i\n", h1,h2,h3,h4,p1,p2);

            //concatenate IP address bytes
            uint32_t host = (h1 << 24) | (h2 << 16) | (h3 << 8) | h4; 
            //concatenate port number
            uint16_t port = (p1 << 8) | p2; 

            data_socket = connect_TCP(host, port);

            printf ("Host: %i, Port: %i\n", host, port);

            if (data_socket < 0)
                fprintf(fd, "425 Can't open data connection\n");
            else
                fprintf (fd, "200 Socket succesfully opened.\n");
            fflush(fd);

        }

else if (COMMAND("LIST")) {
//             execAndSend("ls -l");
            strcpy(mesg, "ls -l en cwd\n"); //Doesn't work
            write(data_socket, mesg, sizeof(mesg)); 
            fprintf(fd, "200 OK.\n");
        }   

int connect_TCP( uint32_t address_,  uint16_t  port) {
    std::string addr = std::to_string(address_);
    const char* address = addr.c_str();

    struct sockaddr_in sin;
    struct hostent *hent;

    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);

    if(hent = gethostbyname( address ) ) 
        memcpy(&sin.sin_addr, hent->h_addr, hent->h_length);
    else if ((sin.sin_addr.s_addr = inet_addr((char*)address)) == INADDR_NONE){
         printf("No puedo resolver el nombre \"%s\"\n", address);
         return errno;
    }


    int s = socket(AF_INET, SOCK_STREAM, 0);
    if(s < 0){
       printf("No se puede crear el socket: %s\n", strerror(errno));
       return errno;
    }

    if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0){
       printf("No se puede conectar con %s: %s\n", address, strerror(errno));
       return errno;
    }
    printf("Conexion TCP abierta.\n");
    return s;
}

我不知道我做错了什么,connect_TCP()没有返回负值,但它不会发送带有write()的消息

0 个答案:

没有答案