0

我目前正在尝试向打开 tcp 连接的方法添加超时。我大致使用的是此处找到的指南,只是我尝试使用poll()而不是选择。然而,我的调用poll()立即返回通知 fd 已准备好写入,尽管连接未打开。这是我制作的简化代码示例:

#include <cstring>
#include <poll.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <cerrno>
#include <iostream>
#include <cstdlib>

int main() {
   struct addrinfo hints;

   ::memset( &hints, 0, sizeof( hints ) );
   hints.ai_socktype = 0;
   hints.ai_family = AF_UNSPEC;
   hints.ai_protocol = 0;
   hints.ai_flags = AI_CANONNAME;


   struct addrinfo * info;

   if( ::getaddrinfo( "127.0.0.1", "49999", &hints, &info ) != 0 ) {
      std::cerr << "Error: getaddrinfo" << std::endl;
      exit( 1 );
   }


   int soc;

   if( (soc = ::socket( info->ai_family, info->ai_socktype, info->ai_protocol ) ) < 0 ) {
      std::cerr << "Erorr: socket" << std::endl;
   }

   // Set mode to non-blocking for timeout handling
   int arg;
   if( (arg = ::fcntl( soc, F_GETFL, 0 )) < 0 ) {
      std::cerr << "Error: fcntl" << std::endl;
      exit( 1 );
   }

   arg |= O_NONBLOCK;
   if( ::fcntl(soc, F_SETFL, arg) < 0) {
      std::cerr << "Error: fcntl" << std::endl;
      exit( 1 );
   }

   int res = ::connect(soc,info->ai_addr,info->ai_addrlen);

   if( (res != 0) && (errno != EINPROGRESS) ) {
      std::cerr << "Error: connect" << std::endl;
      exit( 1 );
   }

   pollfd pfd;
   pfd.fd = soc;
   pfd.events = POLLOUT;

   res = ::poll( &pfd, 1, 50000 );

   if( res < 0 ) {
      std::cerr << "Error: poll" << std::endl;
      exit( 1 );
   }
   else if( res == 0 ) {
      std::cerr << "Error: poll" << std::endl;
      exit( 1 );
   }

   // Set blocking mode again
   if( (arg = ::fcntl(soc, F_GETFL, NULL)) < 0) {
      std::cerr << "Error: fcntl" << std::endl;
      exit( 1 );
   }
   arg &= (~O_NONBLOCK);
   if( ::fcntl(soc, F_SETFL, arg) < 0) {
      std::cerr << "Error: fcntl" << std::endl;
      exit( 1 );
   }

   return 0;
}

由于我的机器上的端口49999已关闭,我预计会出现错误。相反,程序以返回值结束0

我还尝试使用上面链接中的选择示例。如果我将调用替换为poll()完整示例,则会收到以下错误消息:

Operation now in progress: Operation now in progres

我尝试使用 select 减少代码,但是当我减少它时,我得到一个正确的connection refused message.

编辑

注意:“操作正在进行中”消息是由于我这边的错误处理代码中的错误造成的。更正此问题后,我从“getsockopt()”收到了正确的错误消息。这也解释了为什么我不能减少这个例子。

4

2 回答 2

1

如果连接不成功,您只需要进行轮询。连接到“localhost”时,它通常会立即成功。

于 2013-04-17T21:56:18.070 回答
0

问题在于丢失的getsockopt()电话。我假设poll()如果不成功会返回一个错误connect(),但只要状态可以确定,它就会通过。一旦我添加了getsockopt()示例中的调用,它就可以正常工作了。

下面是一些使用 poll 的(不是那么干净的)示例代码:

#include <cstring>
#include <poll.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <cerrno>
#include <iostream>
#include <cstdlib>
#include <cstdio>

int main() {
   struct addrinfo hints;

   ::memset( &hints, 0, sizeof( hints ) );
   hints.ai_socktype = 0;
   hints.ai_family = AF_UNSPEC;
   hints.ai_protocol = 0;
   hints.ai_flags = AI_CANONNAME;


   struct addrinfo * info;

   if( ::getaddrinfo( "127.0.0.1", "49999", &hints, &info ) != 0 ) {
      std::cerr << "Error: getaddrinfo" << std::endl;
      exit( 1 );
   }


   int soc;

   if( (soc = ::socket( info->ai_family, info->ai_socktype, info->ai_protocol ) ) < 0 ) {
      std::cerr << "Erorr: socket" << std::endl;
   }

   // Set mode to non-blocking for timeout handling
   int arg;
   if( (arg = ::fcntl( soc, F_GETFL, 0 )) < 0 ) {
      std::cerr << "Error: fcntl" << std::endl;
      exit( 1 );
   }

   arg |= O_NONBLOCK;
   if( ::fcntl(soc, F_SETFL, arg) < 0) {
      std::cerr << "Error: fcntl" << std::endl;
      exit( 1 );
   }

   int res = ::connect(soc,info->ai_addr,info->ai_addrlen);

   if( res < 0 ) {
      if( errno == EINPROGRESS ) {
         pollfd pfd;
         pfd.fd = soc;
         pfd.events = POLLOUT;

         std::cout << "Polling" << std::endl;
         res = ::poll( &pfd, 1, 50000 );

         if( res < 0 ) {
            std::cerr << "Error: poll" << std::endl;
            exit( 1 );
         }
         else if( res == 0 ) {
            std::cerr << "Error: poll" << std::endl;
            exit( 1 );
         } else {
            socklen_t lon = sizeof(int);
            int valopt;
            if (getsockopt(soc, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) {
               fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno));
               exit(0);
            }
            // Check the value returned...
            if (valopt) {
               fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt));
               exit(0);
            }
         }
      }
      else {
         std::cerr << "Error: connect" << std::endl;
         exit( 1 );
      }
   }

   // Set blocking mode again
   if( (arg = ::fcntl(soc, F_GETFL, NULL)) < 0) {
      std::cerr << "Error: fcntl" << std::endl;
      exit( 1 );
   }
   arg &= (~O_NONBLOCK);
   if( ::fcntl(soc, F_SETFL, arg) < 0) {
      std::cerr << "Error: fcntl" << std::endl;
      exit( 1 );
   }

   return 0;
}
于 2013-04-18T09:57:51.143 回答