0

我想在我的 PC 和 HTTP 服务器之间的 80 端口建立连接。这可行吗?

我尝试了以下代码,但没有成功。它报告一个错误:

连接设置失败:连接超时

从我的角度来看,UDT 使用 UDP,那么我的计划会奏效吗?

if ( UDT::startup() == UDT::ERROR )
{
    cout << "UDT::startup: " << UDT::getlasterror().getErrorMessage() << "and " << strerror ( errno ) << endl;
    exit ( -1 ); 
}

UDTSOCKET conn_serv_fd;
struct sockaddr_in serv_addr;

if (  ( conn_serv_fd = UDT::socket ( AF_INET, SOCK_STREAM, 0 ) ) == UDT::INVALID_SOCK  )
{
    cout << "create UDT::conn_serv_fd: " << UDT::getlasterror().getErrorMessage() << "and " << strerror ( errno ) << endl;
    exit ( -1 );
}

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(80);
serv_addr.sin_addr = *((struct in_addr *)h->h_addr);
//if udt is udp based which is connectionless, why connect() API is needed ???
if (  UDT::ERROR == UDT::connect ( conn_serv_fd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr) )  )
{
    //bug: connect: Connection setup failure: connection time out.
    cout << "connect: " << UDT::getlasterror().getErrorMessage() << "and " << strerror ( errno ) << endl;
    exit ( -1 );
}

if (  UDT::ERROR == UDT::send ( conn_serv_fd, first_req, first_req_size, 0 )  )
{
    cout << "send: " << UDT::getlasterror().getErrorMessage();
    exit ( -1 );
}
else
{
    cout<<"send first req already!"<<endl;
}

我得到服务器地址如下伪代码

char* hostname = gethostname(char* request); // I printed it and it worked
struct hostent *h = gethostbyname ( hostName ); // I printed it and it worked   
struct sockaddr_in serv_addr;
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(80);
serv_addr.sin_addr = *((struct in_addr *)h->h_addr);
UDT::connect ( conn_serv_fd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr) )

我替换gethostbyname()getaddrinfo(),现在看起来像:

        char *hostName = getHostName ( first_req, first_req_size );

        struct addrinfo* res, * cur;

        struct addrinfo hints;
        memset(&hints, 0, sizeof(struct addrinfo));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_STREAM;

        if (0 != getaddrinfo( hostName, NULL, &hints, &res))
        {
            cout<<"can't getaddrinfo! "<<strerror ( errno )<<endl;
            exit ( -1 );
        }

        bool connected = 0;
        for ( cur = res; cur != NULL; cur = cur->ai_next ) 
        {

            struct sockaddr_in* addr = (struct sockaddr_in *)cur->ai_addr;

            char ipbuf[16];
            cout << "try to connect to ip: "<< inet_ntop ( AF_INET, &addr -> sin_addr, ipbuf, 16) << endl;

            serv_addr.sin_addr = addr->sin_addr;

            if (  UDT::ERROR == UDT::connect ( conn_serv_fd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr) )  )
            {
                cout << "connect: " << UDT::getlasterror().getErrorMessage() << "and sys error: " << strerror ( errno ) << endl;
                UDT::close ( conn_serv_fd );
                continue;
            }
            else
            {
                cout << "connect successfully!" << endl;
                connected = 1;
                break;
            }
        }

        freeaddrinfo(res);
        delete []hostName;
        delete []first_req;

        if ( !connected )
        {
            cout << "connection setup failure!" <<endl;
            exit ( -1 );
        }
4

0 回答 0