TFTP

时间:2015-07-30 15:33:33

标签: c++ udp recv tftp

我的问题是:在TFTP期间,当客户端连接到服务器时,初始数据传输的默认端口是端口69.当服务器从客户端收到WRQ(写请求)数据包时,它返回ACK(确认)数据包。但是,此ACK数据包通过不同的端口发送回客户端。下面是我用来连接服务器的功能。请注意,server_port为69.

int TFTPClient::connectToServer() {

    socket_descriptor = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

    if (socket_descriptor == -1) {

        throw new ETFTPSocketCreate;

    }

    client_address.sin_family = AF_INET;
    client_address.sin_port = htons(server_port);   
    client_address.sin_addr.s_addr = inet_addr(this->server_ip); 

    connection = connect(socket_descriptor, (const struct sockaddr *)&client_address, sizeof(client_address));

    if (connection != 0) {

    cout << "Unable to connect to an address\n";
    return -1;

    }

    DEBUGMSG("Successfully connected");

    AfxMessageBox("Successfully connected");

    return 1;

}


int TFTPClient::sendPacket(TFTP_Packet* packet) {

    return send(socket_descriptor, (char*)packet->getData(), packet->getSize(), 0);

}

int TFTPClient::waitForPacket(TFTP_Packet* packet, int timeout_ms) {

    packet->clear();

    int receive_status;
    int iResult; 
    u_long iMode = 0;

    //-----------------------------------------------
    //  Set the socket I/O mode: In this case FIONBIO
    //  enables or disables the blocking mode for the 
    //  socket based on the numerical value of iMode.
    //  If iMode = 0, blocking is enabled; 
    //  If iMode != 0, non-blocking mode is enabled.
    //-----------------------------------------------

    iResult = ioctlsocket(socket_descriptor, FIONBIO, &iMode);

    if (iResult != NO_ERROR){

        printf("ioctlsocket failed with error: %ld\n", iResult);
    }

    receive_status = recv(socket_descriptor, (char*)packet->getData(), TFTP_PACKET_MAX_SIZE, 0);

    if (receive_status == 0) {
        cout << "Connection was closed by server\n";
        return TFTP_CLIENT_ERROR_CONNECTION_CLOSED;
    }

    if (receive_status == SOCKET_ERROR) {

        DEBUGMSG("recv() error in waitForPackage()");
        return TFTP_CLIENT_ERROR_RECEIVE;
    }

    packet->setSize(receive_status);

    return TFTP_CLIENT_ERROR_NO_ERROR;

}

我正在使用Wire Shark来确定套接字上的数据传输。我正在发送一个成功到达服务器的WRQ数据包。 ACK数据包(根据Wireshark)成功发送回客户端(在不同的端口上)。但是,当我点击“recv()”时,我的应用程序会无限期挂起。功能。我担心这可能是因为我在send()函数中调用的recv()函数中调用了相同的套接字(&#39; socket_descriptor&#39;)。端口69的规范是&#39; socket_descriptor&#39;导致这个应用失速?或者recv()接受来自服务器IP的数据包通过其他端口?请问有人能说清楚吗?

0 个答案:

没有答案