Rajeshwar
Rajeshwar

Reputation: 11671

WSAGetLastError returns WSAENOTSOCK - Cause?

I have something like this in my code

    WSADATA wsadata;

    int error = WSAStartup(0x0202, &wsadata);

    SOCKADDR_IN target; //Socket address information

    target.sin_family = AF_INET; // address family Internet
    target.sin_port = htons (5005); 
    target.sin_addr.s_addr = inet_addr ("127.0.0.1"); 

    clntSocket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); //Create socket
    

    if (::connect(clntSocket, (SOCKADDR *)&target, sizeof(target)) == SOCKET_ERROR)
    {
        //Could not connect
                    __debugbreak();
    }
    else
    { 
        //Connected - Now receive data
        do 
        {
            char my_stream[800];
            iResult =  recv(clntSocket,my_stream,sizeof(my_stream),0);
            if(iResult<0)
            {
                int a = WSAGetLastError();
                //Receives 10038 - WSAENOTSOCK
            }
        } while( iResult > 0 );    
    }

And I would sometimes (occasionally) get 10038. Which states that

Socket operation on nonsocket.

An operation was attempted on something that is not a socket. Either the socket handle parameter did not reference a valid socket,or for select, a member of an fd_set was not valid.

Am I doing something wrong while setting up the socket ? Any suggestions on how to fix the problem ?

Upvotes: 0

Views: 699

Answers (1)

user207421
user207421

Reputation: 311039

Either:

  1. You never opened the socket.
  2. You have corrupted the handle value.
  3. You have closed the socket and then continued to use it.

Upvotes: 2

Related Questions