首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Winsock接受不返回

Winsock接受不返回
EN

Stack Overflow用户
提问于 2018-02-22 18:12:37
回答 1查看 640关注 0票数 0

我正在为套接字做一个简单的c++包装器,我在windows上遇到了一个问题。我初始化服务器套接字时没有任何错误,但是当我尝试使用netcat localhost 4242进行连接时,连接不被接受(服务器套接字上的accept从不返回)。

这是我正在使用的代码

代码语言:javascript
复制
skt::tcp::Socket serv_socket(port_);

while (!stop_flag_)
{
    skt::tcp::Socket cli_socket;
    std::cout << "Accepting clients..." << std::endl;
    serv_socket.accept(cli_socket);
    std::cout << "New client connected : " << std::endl;
    //blabla handle the connection
}

BaseSocket.cpp

代码语言:javascript
复制
    BaseSocket::BaseSocket(int domain, int type, int protocol, const std::string &port)
{
    struct addrinfo hints;

#ifdef _WIN32
  WSADATA wsa{};

  if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
  {
    throw SocketException(std::string("Unable to initialize the socket: ") +
                          printLastError());
  }
#endif

  memset(&hints, 0, sizeof hints); // make sure the struct is empty
  hints.ai_family = domain;     // don't care IPv4 or IPv6
  hints.ai_socktype = type; 
  hints.ai_protocol = protocol;
  hints.ai_flags = AI_PASSIVE;     // fill in my IP for me

  if (getaddrinfo(NULL, port.c_str(), &hints, &servinfo_) != 0) {
      printf("getaddrinfo failed");
      //WSACleanup();
  }


  mFd = socket(servinfo_->ai_family, servinfo_->ai_socktype, servinfo_->ai_protocol);
  std::cout << "Error init socket " << WSAGetLastError() << " " << mFd << std::endl;

  if (mFd == INVALID_SOCKET)
    throw SocketException(std::string("Unable to initialize the socket: ") +
                          printLastError());
}


BaseSocket::BaseSocket(int domain, int type, int protocol)
{

#ifdef _WIN32
    WSADATA wsa{};

    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        throw SocketException(std::string("Unable to initialize the socket: ") +
            printLastError());
    }
#endif

}

void BaseSocket::bind()
{
  if (::bind(mFd, servinfo_->ai_addr, (int)servinfo_->ai_addrlen) == SOCKET_ERROR)
    throw SocketException(std::string("address binding failed: ") + printLastError());
}

void BaseSocket::listen(int backlog)
{
  if (::listen(mFd, backlog) ==  SOCKET_ERROR)
    throw SocketException(std::string("listen setting failed: ") + printLastError());
}

void BaseSocket::accept(BaseSocket &clientSocket, sockaddr *addr, socklen_t *addrlen)
{
    std::cout << "pre accept" << std::endl;
  clientSocket.mFd = ::accept(mFd, addr, addrlen);

  if (clientSocket.mFd == INVALID_SOCKET) {
      printLastError();
      std::cout << "Error onm accept " << WSAGetLastError() << std::endl;
      throw SocketException(std::string("accept failed: ") + printLastError());
  }
}

Socket.cpp

代码语言:javascript
复制
Socket::Socket(int port) :
        BaseSocket(AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "4242")
    {
        std::cout << "Init server socket" << std::endl;
        enableReuseAddr();
        bind();
    }

    Socket::Socket() :
        BaseSocket(SocketDomain::IP, SocketType::TCP, 0)
    {
        std::cout << "Init client socket" << std::endl;

    }

    void Socket::accept(Socket &clientSocket, int backlog)
    {
      sockaddr addr{};
      socklen_t addrlen;

      listen(backlog);
      BaseSocket::accept(clientSocket,nullptr, nullptr);

      //clientSocket.servinfo_ = addr;
    }

你知道我做错了什么吗?

EN

回答 1

Stack Overflow用户

发布于 2021-02-08 17:29:22

有点晚了,但当我在“用C进行网络编程”的time_server示例中遇到同样的问题时,我遇到了这个问题。这个问题给了我到netcat的链接(尽管我使用了ncat),这帮助我弄清楚了。使用NULL作为getaddrinfo的pNodeName参数并不一定首先返回本地主机ip地址。使用"localhost“或"127.0.0.1”。(您可以在getaddrinfo返回的地址上使用getnameinfo来检查您正在使用什么来创建套接字。)

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

https://stackoverflow.com/questions/48924742

复制
相关文章

相似问题

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