Earnie
Earnie

Reputation: 31

FD_ACCEPT detected, but then call 'accept' fails with 10038 - WSAENOTSOCK

Well basically, that is it. Meanwhile, the client's call to connect is a success. How is that possible ? I haven't added any code because I have no idea where the bug is.

Server: Detects FD_ACCEPT. Call to accept () fails.

Client: Call to connect () succeeds. It then detects FD_CONNECT. The following send () succeeds. The send () after that fails (10053 - WSAECONNABORTED).

void Server::get_addressinfo() {
    // Resolve the local address and port to be used by the server
    const char * p = port.c_str();
    int iResult = getaddrinfo(NULL, p, &hints, &result);
    if (iResult != 0) {
        throw MyException("getaddrinfo failed.");
    }
}

void Server::create_socket() {
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (ListenSocket == INVALID_SOCKET) {
        throw MyException("socket creation failed.");
    }
}

void Server::bind_socket() {
    int iResult = bind(ListenSocket, result->ai_addr, (int) result->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        closesocket(ListenSocket);
        throw MyException("bind failed.");
    }
}

void Server::listen_for_connection() {
    int iResult = listen(ListenSocket, SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        closesocket(ListenSocket);
        throw MyException("listen failed.");
    }
}

void Server::goLive() {
    get_addressinfo();
    create_socket();
    bind_socket();
    listen_for_connection();
    wait_for(FD_ACCEPT);
    accept_connection();
}

void Server::wait_for(u_int event_type) {
    WSAEVENT event = WSACreateEvent();
    WSAEventSelect(ListenSocket, event, event_type);
    WSAEVENT lphEvents[1] = {event};
    WSANETWORKEVENTS NetworkEvents = {0};
    int nReturnCode = WSAWaitForMultipleEvents(1, &lphEvents[0], false, WSA_INFINITE, false);
    if (nReturnCode==WSA_WAIT_FAILED) 
        throw MyException("WSA__WAIT_FAILED.\n");
    if (WSAEnumNetworkEvents(ListenSocket, lphEvents[0], &NetworkEvents) == SOCKET_ERROR) 
        throw MyException("WSAEnumNetworkEvents => SOCKET_ERROR.\n");
    WSACloseEvent(event);  ***// THIS WAS THE BUG !!!***
}

void Server::accept_connection() {
    ClientSocket = accept(ListenSocket, NULL, NULL);
    if (ClientSocket == INVALID_SOCKET) {
        closesocket(ListenSocket);
        throw MyException("accept failed.");
    }
}

Upvotes: 0

Views: 577

Answers (1)

Earnie
Earnie

Reputation: 31

I worked on the code a little bit and made it better, but the bug was still in there. Now I've found the problematic line:

WSACloseEvent(event);

After commenting it out, the server accepted the connection.

From the MSDN WSACloseEvent function documentation:

The WSACloseEvent function closes the handle to an event object and frees resources associated with the event object.

I guess this means it freed the socket => hence 10038 - WSAENOTSOCK. I fell for the socket value which was unequal to INVALID_SOCKET. Therefore I thought the socket would be valid. It wasn't...

Upvotes: 1

Related Questions