我正在为套接字做一个简单的c++包装器,我在windows上遇到了一个问题。我初始化服务器套接字时没有任何错误,但是当我尝试使用netcat localhost 4242进行连接时,连接不被接受(服务器套接字上的accept从不返回)。
这是我正在使用的代码
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
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
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;
}你知道我做错了什么吗?
发布于 2021-02-08 17:29:22
有点晚了,但当我在“用C进行网络编程”的time_server示例中遇到同样的问题时,我遇到了这个问题。这个问题给了我到netcat的链接(尽管我使用了ncat),这帮助我弄清楚了。使用NULL作为getaddrinfo的pNodeName参数并不一定首先返回本地主机ip地址。使用"localhost“或"127.0.0.1”。(您可以在getaddrinfo返回的地址上使用getnameinfo来检查您正在使用什么来创建套接字。)
https://stackoverflow.com/questions/48924742
复制相似问题