我在linux上使用C++中的套接字連接到使用connect()的服務器,但它似乎沒有超時值。給它幾秒鐘暫停的最簡單/最好的方法是什麼?我會後我有最多的代碼,包括阻塞調用連接():在C++中使用connect()時設置超時
using namespace std;
int main(int argc, char* argv[])
{
// Interpret the command line arguments
string uri_string = "";
if (argc != 2)
{
cerr << "Usage: " << argv[0];
cerr << " <URI>" << endl;
return 1;
}
else
{
uri_string = argv[1];
}
// Create URI object and have it parse the uri_string
URI *uri = URI::Parse(uri_string);
if (uri == NULL)
{
cerr << "Error: Cannot parse URI." << endl;
return 2;
}
// Check the port number specified, if none use port 80
unsigned port = 80;
if (uri->Is_port_defined())
{
port = uri->Get_port();
}
// Create TCP socket and connect to server
int tcp_sock = socket(AF_INET, SOCK_STREAM, 0);
if (tcp_sock < 0)
{
cerr << "Unable to create TCP socket." << endl;
return 3;
}
sockaddr_in server;
socklen_t slen = sizeof(server);
server.sin_family = AF_INET;
server.sin_port = htons(port);
cout << "Resolving " << uri->Get_host() << "... ";
hostent *hostp = gethostbyname(uri->Get_host().c_str());
memcpy(&server.sin_addr, hostp->h_addr, hostp->h_length);
struct in_addr **pptr;
pptr = (struct in_addr **)hostp->h_addr_list;
string ip_addr = "";
while(*pptr != NULL) {
ip_addr += inet_ntoa(**(pptr++));
}
cout << ip_addr << endl;
cout << "Connecting to " << uri->Get_host() << "|" << ip_addr << "|:";
cout << port << "... ";
if (connect(tcp_sock, (sockaddr*)&server, slen) < 0)
{
cerr << "Unable to connect to server via TCP." << endl;
close(tcp_sock);
return 4;
}
cout << "connected." << endl;
// rest of code
}
儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/評論/低質量帖/ 16911940) – wasthishelpful 2017-08-03 08:24:51