首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >非阻塞套接字连接总是成功?

非阻塞套接字连接总是成功?
EN

Stack Overflow用户
提问于 2016-03-27 23:01:32
回答 1查看 3.1K关注 0票数 0

如果以前有人问过这个问题,我真的很抱歉。我正在编写一个使用select复用的非阻塞套接字客户端。让我困惑的是,无论服务器是在线的还是离线的,非阻塞连接总是成功的。我搜索了许多帖子并跟踪了它们的解决方案,但是没有一个在我的linux机器上工作。

代码语言:javascript
复制
static void callback_on_select_write(int connect_fd) {

  // Client write event arrived;

  int error = -1;
  socklen_t len = sizeof(error);

  if(getsockopt(connect_fd, SOL_SOCKET, SO_ERROR, &error, &len) == -1) {
    return;
  }

  // getsockopt puts the errno value for connect into erro so 0 means no-error. 
  if(error == 0) {
      // Connection ok.
  }
  else {
    cerr << "Failed to connect\n";
    return;
  }

  // Ready to write/read

}

每次select返回并调用这个总是成功的回调时,即转到“准备写入/读取”块,而不是验证失败。为什么会发生这种事?如何设计一个可移植的机制来检测连接是否真的成功?下面是我创建连接器的方式。

代码语言:javascript
复制
int make_socket_client(const ::std::string& hostname, const ::std::string& port) {

  struct addrinfo hints;
  struct addrinfo* res {nullptr};
  struct addrinfo* ptr {nullptr};

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

  int rv;
  int connector;

  if((rv = getaddrinfo(hostname.c_str(), port.c_str(), &hints, &res)) != 0) {
    return -1;
  }

  // Try to get the first available client connection.
  for(ptr = res; ptr != nullptr; ptr = ptr->ai_next) {

    // Ignore undefined ip type.
    if(ptr->ai_family != AF_INET && ptr->ai_family != AF_INET6) {
      continue;
    }

    // Create a listener socket and bind it to the localhost as the server.
    if((connector = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol)) == -1){
      continue;
    }

    make_fd_nonblocking(connector);

    if(connect(connector, (struct sockaddr*)ptr->ai_addr, ptr->ai_addrlen) < 0) {
      // This is what we expect.
      if(errno == EINPROGRESS) {
        break;
      }
      else {
        close(connector);
        continue;
      }
    }
    else {
      break;
    }
  }

  freeaddrinfo(res);

  if(ptr == nullptr) {
    return -1;
  }

  return connector;  
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-28 01:37:26

每次select返回并调用这个总是成功的回调时,即转到“准备写入/读取”块,而不是验证失败。为什么会发生这种事?

当异步TCP连接正在进行时(如connect()调用中的-1/EINPROGRESS所示),您应该将套接字传递给select()作为其准备就绪的写套接字集的一部分,以便当套接字指示它已准备好进行写入时,select()将返回。

当TCP连接成功或失败时,select()将返回套接字已准备好写(*)。当这种情况发生时,您需要弄清楚这两种可能的结果(成功还是失败)中哪一种已经发生。

下面是异步连接的套接字select()作为写准备时调用的函数。

代码语言:javascript
复制
// call this select() has indicated that (fd) is ready-for-write because
// (fd)'s asynchronous-TCP connection has either succeeded or failed.
// Returns true if the connection succeeded, false if the connection failed.
// If this returns true, you can then continue using (fd) as a normal
// connected/non-blocking TCP socket.  If this returns false, you should
// close(fd) because the connection failed.
bool FinalizeAsyncConnect(int fd)
{
#if defined(__FreeBSD__) || defined(BSD)
   // Special case for FreeBSD7, for which send() doesn't do the trick
   struct sockaddr_in junk;
   socklen_t length = sizeof(junk);
   memset(&junk, 0, sizeof(junk));
   return (getpeername(fd, (struct sockaddr *)&junk, &length) == 0);
#else
   // For most platforms, the code below is all we need
   char junk;
   return (send(fd, &junk, 0, 0L) == 0);
#endif
}

(*)附带说明:在Windows下,情况略有不同,因为Windows喜欢按自己的方式行事:在Windows下,正如上面所描述的那样,指示了一个成功的异步连接(),但是如果您希望得到有关在Windows下失败的异步连接()尝试的通知,您还需要在“除”fd_set下注册您的套接字,因为它是Windows将用来通信失败的异步连接()的“除”fd_set。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36253705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档