c++socketstcpwinsockets

C++ socket server doesn't accept any clients / client won't connect


I have a simple class that starts a server and listens to clients and after they connect it starts a thread for them, my problem is that when I run the server it seems to be listening and when I try to connect using simple python code the server doesn't accept a socket.

Obviously I thought the problem is with the client connection but after trying to change to address from loopback to my local address I get the same result so I'm not sure what's causing this.

Server code:

Communicator::Communicator()
{
    WSADATA wsaData;

    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        throw std::runtime_error("Failed to startup WSA");
    }

    m_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (m_sock == INVALID_SOCKET)
    {
        throw std::runtime_error("Failed to create socket");
    }
}


Communicator::~Communicator()
{
    try
    {
        WSACleanup();
        closesocket(m_sock);
    }
    catch (...) {}
}

void Communicator::bindAndListen()
{
    SOCKADDR_IN sa;
    sa.sin_port = Config::getConfig()["port"]; // 8080
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = INADDR_ANY;

    if (bind(m_sock, (const SOCKADDR*) & sa, sizeof(sa)) == SOCKET_ERROR)
    {
        throw std::runtime_error("Failed to bind socket");
    }

    if (listen(m_sock, SOMAXCONN) == SOCKET_ERROR)
    {
        throw std::runtime_error("Failed to start listening");
    }

    std::cout << "Listening" << std::endl; // This gets printed
    while (true)
    {
        SOCKET clientSock = accept(m_sock, NULL, NULL); // Nothing accepted, program stuck here
        std::cout << "Connection" << std::endl;
        if(clientSock == INVALID_SOCKET)
        { 
            throw std::runtime_error("Invalid connection");
        }

        startThreadForNewClient(clientSock);

    }

}

Testing python code:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
s.connect(('127.0.0.1', 8080))
s.send(b"h")

The only error I'm getting is [WinError 10061] No connection could be made because the target machine actively refused it on my client, the server is continuously listening.


Solution

  • My problem was that I forgot to put the port in htons, sorry for my rookie mistake.

    void Communicator::bindAndListen()
    {
        SOCKADDR_IN sa;
        sa.sin_port = htons(Config::getConfig()["port"]); 
        sa.sin_family = AF_INET;
        sa.sin_addr.s_addr = INADDR_ANY;
    
        if (bind(m_sock, (const SOCKADDR*) & sa, sizeof(sa)) == SOCKET_ERROR)
        {
            throw std::runtime_error("Failed to bind socket");
        }
    
        if (listen(m_sock, SOMAXCONN) == SOCKET_ERROR)
        {
            throw std::runtime_error("Failed to start listening");
        }
    
        std::cout << "Listening" << std::endl;
        while (true)
        {
            SOCKET clientSock = accept(m_sock, NULL, NULL);
            std::cout << "Connection" << std::endl;
            if(clientSock == INVALID_SOCKET)
            { 
                throw std::runtime_error("Invalid connection");
            }
    
            startThreadForNewClient(clientSock);
    
        }
    
    }