'return false'不会从函数返回

时间:2013-10-26 09:11:41

标签: c++ network-programming

А正在为在Linux下运行的基于ARM的机器人实现tcp-connection C ++包装器 这是我的代码:

bool tcp_connection::wait_connection(int port)
{
  is_server = true;

  if (!create_socket())
    return false;

  local_addr.sin_port = htons(port);
  const int result = bind(sockfd, (struct sockaddr *) &local_addr, sizeof(local_addr));
  if (result < 0)
  {
    std::cout << "tcp_connection.cpp: Error on binding '" << strerror(errno) << "'" << std::endl;
    std::cout << "return false" << std::endl;
    return false;
    std::cout << "after return false???" << std::endl;
  }

  std::cout << "listening...." << std::endl;
  .....
  return true;
}

这是我的输出:

tcp_connection.cpp: Error on binding 'Address already in use'
return false
listening....

怎么会发生?

1 个答案:

答案 0 :(得分:2)

它正在回归。如果不是,您的输出将是:

tcp_connection.cpp: Error on binding 'Address already in use'
return false
after return false???
listening....

listening...打印可能来自后续调用wait_connection(),其中未if (result < 0)分支。