I coded a c++ server/client program using windows sockets. getting 10061 from wsagetlasterror() trying to connect using my public ip addr

Im attempting to connect to my server via my client and if i use my local host address 127.0.0.1 everything works perfectly fine. it connects and im able to send data. however whenever i switch it to my public address it fails to connect. i already went through my router and forwarded the port im using. and ive tested if the port is open with canyouseeme.org and port forward network utilities. the error code i get from getlasterror() is 10061. i also called my isp and they told me the port is open so i should be able to connect but im not. i turned off my firewall and AV as well. i have windows 10 64 bit. im at a loss and would really appreciate the help. the error code means that the server is actively refusing the connection. but nothing happens on my server side...it stays in the accept() call. this is the server code. how do i solve this issue? i currently have both the server and client on one computer.

class Server
{
public:
    Server(int port)
        :
        port(port)
    {
        std::cout << "Initializing server...\n";

        if (WSAStartup(version, &wsData) == 0)
        {
            auto result = createListenSock();

            //if no value is returned, it was a success
            if (!result.has_value())          
                std::cout << "Successfully created a server! Accepting Clients now.\n";
            else
                std::cout << result.value() << std::endl;
        }
        else
            std::cout << "Failed to initialize WinSock. Error : " << WSAGetLastError() << std::endl;

    }
       
    ~Server()
    {
        closesocket(listensock);
        closesocket(client);
        WSACleanup();
    }
    std::optional<std::string> acceptClient()
    {
        //create a new socket thats connected to the client once we found one. after this call clientinfo is filled
        // SOCKET clientSocket = accept(listening, (sockaddr*)&clientinfo, &size);

        int sz = sizeof(clientinfo);

        if ((client = accept(listensock, (sockaddr*)&clientinfo, &sz)) == INVALID_SOCKET)
            return {};
        else
        {
            char clientipaddr[100];
   
            inet_ntop(AF_INET, &clientinfo.sin_addr, clientipaddr, sizeof(clientipaddr));

            return std::string(clientipaddr);
        }

    }
private:

    std::optional<std::string> createListenSock()
    {
        //AF_INET for ipv4
        //SOCK_STREAM A socket type that provides sequenced, reliable, two-way,
        //connection-based byte streams with an OOB data transmission mechanism.
        if ((listensock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
            return "Failed to created listen socket...\n";

        initConnectionInfo();

        if (bind(listensock, (sockaddr*)&serverInfo, sizeof(serverInfo)) == SOCKET_ERROR)
            return "Failed to bind...\n";

        if (listen(listensock, SOMAXCONN) == SOCKET_ERROR)
            return "Failed to put into listening mode...\n";

        //if it returns an empty container, that means it was successful
        return {};

    }
    void initConnectionInfo()
    {
        //make it use ipv4
        serverInfo.sin_family = AF_INET;
        //htons converts the port from host to network byte order.
        serverInfo.sin_port = htons(port);
        //specify that we can accept any ip address
        serverInfo.sin_addr.S_un.S_addr = INADDR_ANY;
    }
private:
    WSADATA wsData;
    int version = MAKEWORD(2,2);
    SOCKET listensock;
    sockaddr_in serverInfo;
    int port;

private:
    //client variables
    SOCKET client;
    sockaddr_in clientinfo;
};

int main()
{

    //create server
    Server server(1024);
    auto clientip = server.acceptClient();
    if (!clientip.has_value())
    {
        std::cout << "Couldn't connect to client.";
        system("pause");
        return 0;
    }
    std::cout << "Connected to client!" << std::endl << "Client IP Address: " << clientip.value() << std::endl;

and this is the client code

SOCKET startclient()
{
    WSADATA data;
    //server to connect to
    std::string ipAddress = "127.0.0.1"; //IF I CHANGE THIS TO MY PUBLIC IP ADDRESS IT FAILS TO CONNECT
    int port = 1024;

    // initialize winsock
    if (WSAStartup(MAKEWORD(2, 2), &data) != 0)
    {
        std::cout << "Could not initialize WinSock...\n";
        return SOCKET_ERROR;
    }
    
    //create a socket
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);

    if (sock == INVALID_SOCKET)
    {

        std::cout << "Couldn't create a socket, Error " << WSAGetLastError() << std::endl;
        WSACleanup();
        return SOCKET_ERROR;
    }
    //this tells our socket what info to use to connect to the server
    sockaddr_in serverinfo;
    serverinfo.sin_family = AF_INET;
    //htons converts the port number from host to network byte order
    serverinfo.sin_port = htons(port);

    //convert ip address to network byte order in ipv4 format
    inet_pton(AF_INET, ipAddress.c_str(), &serverinfo.sin_addr);

    std::cout << "Attempting to connect..." << std::endl;

    if (connect(sock, (sockaddr*)&serverinfo, sizeof(serverinfo)) == SOCKET_ERROR)
    {
        std::cout << "Failed to connect... Error : " << WSAGetLastError() << std::endl;
        WSACleanup();
        closesocket(sock);
        return SOCKET_ERROR;
    }
    else
        std::cout << "Connected!" << std::endl;
   return sock;
}

int main()
{
  SOCKET client = startclient();

    if (client == SOCKET_ERROR)
    {
        system("pause");
        return 0;
    }



Read more here: https://stackoverflow.com/questions/64943118/i-coded-a-c-server-client-program-using-windows-sockets-getting-10061-from-ws

Content Attribution

This content was originally published by SkyLuvv at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: