C++ FTP服务器无法打开客户端的套接字

C++ FTP服务器无法打开客户端的套接字,c++,linux,sockets,ftp,C++,Linux,Sockets,Ftp,我在linux上编程一个FTP服务器。问题是,当它收到一个端口命令时,我正试图用客户端打开一个套接字,但当我向它写入时什么也没有发生,例如在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 num

我在linux上编程一个FTP服务器。问题是,当它收到一个端口命令时,我正试图用客户端打开一个套接字,但当我向它写入时什么也没有发生,例如在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发送消息

您介意发布正在运行的循环,包括数据\u套接字声明,我强烈怀疑该声明在循环中声明不正确吗?那么write返回什么呢?此外,对于connect_TCP来说,它的返回值很差,特别是因为它所包含的值很可能被误解为有效的描述符。一种可能的替代方法是,函数可以返回-1,调用方可以查询errno或其他类似配置。在该行中,如果连接失败,您也无法关闭s,从而导致它泄漏。@WhozCraig我检查了write的返回值,并且获得了非法的seek,如果连接失败,我也关闭了s。我有两个类FTPServer和ClientConnection。FTPServer::run是运行服务器的调用,它创建一个监听端口的套接字,等待accept并为该连接创建一个线程,该线程使用与我在accept中获得的文件描述符关联的fdopen初始化文件流。然后进入ClientConnection::WaitForRequests,它进入一个while循环扫描命令回复