Matthew
Matthew

Reputation: 1

WinSock2 send returns SOCKET_ERROR after 6 loops iterations... Why?

After 6 iterations, the send returns -1 aka SOCKET_ERROR. I tried adding a sleep function so it would wait a second before looping through again, but that made it so that it would only iterate 2 times. What is causing this error?

#pragma comment(lib, "ws2_32.lib")

    #include <iostream>
    #include <windows.h>
        void main ()
    {
        int reqVersion = 2;

        WSADATA wsaData;

        if (WSAStartup(MAKEWORD(reqVersion,0), &wsaData)==0)
    {
        // Check if major version is at least reqVersion
        if (LOBYTE(wsaData.wVersion) >= reqVersion)
        {
            SOCKADDR_IN addr;
            int addrlen = sizeof(addr);

            SOCKET sListen;
            SOCKET sConnect;

            sConnect = socket(AF_INET, SOCK_STREAM, NULL);

            addr.sin_addr.s_addr = inet_addr("192.168.0.7");
            addr.sin_family = AF_INET;
            addr.sin_port = htons(1234);


            if(connect(sConnect, (SOCKADDR*)&addr, sizeof(addr)) == 0)
            {
                char message[10] = "Hellooooo";
                for(int i = 0; i <50; i++)
                {
                    int sendOutcome = send(sConnect, message, sizeof(message), 0);
                    if(sendOutcome == SOCKET_ERROR)
                    {
                        std::cout << "Socket Error..." << std::endl;
                    }
                    else
                    {
                        std::cout << "SENT..." << std::endl;
                    }
                    Sleep(1000);
                }
            }
            else
            {
                std::cout << "Not Connected..." << std::endl;
            }

        }
        else
        {
            std::cout << "Required version not available..." << std::endl;
        }

        // Cleanup winsock
        if (WSACleanup()!=0)
        {
            std::cout << "Cleanup failed..." << std::endl;
        }
    }
    else
    {
        std::cout << "Startup failed..." << std::endl;
    }
        system("PAUSE");
    }

Upvotes: 0

Views: 5085

Answers (1)

Len Holgate
Len Holgate

Reputation: 21616

It's always useful to obtain the actual error, as sarnold's comment suggested.

In general, socket's code that checks for errors will have a call to WSAGetLastError() after the code that detects the issue by checking for a SOCKET_ERROR return value.

The error code that you get from WSAGetLastError() is important and you can usually reason about the problem once you know what it means.

You can display the text of this error code using FormatMessage() or you can look it up by hand in the WinError.h header.

In this case, 10053 is WSAECONNABORTED for which the error text says "An established connection was aborted by the software in your host machine."

So, I would assume that the server that you are connecting to is aborting the connection for some reason. Without seeing the server source code it's hard to know why that might be.

Upvotes: 1

Related Questions