【问题标题】:Setting a timeout when using connect() in C++在 C++ 中使用 connect() 时设置超时
【发布时间】:2010-12-08 05:45:27
【问题描述】:

我在 linux 上使用 c++ 中的套接字使用 connect() 连接到服务器,但它似乎没有超时值。让它超时几秒钟的最简单/最好的方法是什么。我将发布我拥有的代码,包括对 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
}

【问题讨论】:

    标签: c++ sockets timeout


    【解决方案1】:

    查看this newsgroup post,其中包含有关如何在 Linux 中对套接字连接设置超时的详细信息和示例代码。基本思想是使用非阻塞套接字,然后等待连接。

    【讨论】:

      【解决方案2】:

      另一种避免 select() 痛苦的非阻塞方法是检查 GetLastError() 是否有 EWOULDBLOCK。

      // set socket as nonblocking
      unsigned long value = 1;
      if (ioctlsocket(yourSocket, FIONBIO, &value) == SOCKET_ERROR)
          return SOCKET_ERROR;
      
      // (call connect here)
      
      // check if connect would block
      if (GetLastError() == EWOULDBLOCK)
          return TIMEOUT;
      

      【讨论】:

      • 这绝对是最简单的,我会试一试。谢谢。
      • 这个问题是任何会阻塞的连接尝试都会失败。如果你要连接到另一台机器上的某个东西,那可能是每一次尝试。
      • 我必须#include 什么才能使用ioctlsocket、FIONBIO、SOCKET_ERROR、GetLastError()、TIMEOUT
      【解决方案3】:

      为您的套接字设置非阻塞模式并使用select 检查连接是否建立。 here 是样本

      【讨论】:

        【解决方案4】:

        使套接字成为非阻塞套接字,然后使用带有超时值的 select() 或 poll() 来检查可写性。如果 select() 返回超时,说明您没有及时连接,您可以关闭套接字并处理连接失败。如果它返回完成,一切都很好,你可以继续。

        【讨论】:

          猜你喜欢
          • 2010-10-09
          • 2018-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-05
          • 1970-01-01
          • 1970-01-01
          • 2019-03-27
          相关资源
          最近更新 更多