Reputation: 11
I want to set up a connection between my PC and an HTTP server on port 80. Is that feasible?
I tried the following code but it didn't work. It reports an error:
Connection setup failure: connection time out
From my perspective, UDT uses UDP, so is my plan going to work?
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;
}
And I get the server address as follow pseudocode
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) )
I replace gethostbyname()
with getaddrinfo()
and now it looks like:
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 );
}
Upvotes: 0
Views: 138