sql server has gone away error because of WSACleanup

273 views Asked by At

I would like to ask for help since I don't know what to do anymore. I have a simulator created in c++, where it accepts an id input from a user and checks if it is in the database (created in mysql workbench) which is only in the localhost.

sqlQuery = "SELECT staffaccess.card_number FROM proxycardsim.staffaccess WHERE staffaccess.card_number = " 
            + inputID;

if(mysql_ping(theInstance.connects))
{

}

int queryState = mysql_query(theInstance.connects, sqlQuery);
resultSet = mysql_store_result(theInstance.connects);
rowNum = mysql_num_rows(theInstance.resultSet);

if (rowNum == NULL)                     
{                                       
    mysql_free_result(theInstance.resultSet);
    return false;
}
else
{
    mysql_free_result(theInstance.resultSet);
    return true;
}

The thing is that the simulator is connected to another computer that serves as a server (connected through winsock). If the server is up, it works ok or if all the inputs are wrong, but if the server is down(my code will try to connect again to the server pc so I have to call WSACleanup) after inputting one correct value and I input another mysql_query returns an error that mysql server has gone away. Then the program will break when it goes to mysql_num_rows.

I have this code in another function and when I commented them out one by one, I found out that the error is because of WSACleanup(). If the WSACleanup line is not there my query runs ok.

    if ( false == theInstance.compareID(m_IDEntry))
{
    addData(ConsoleLog,rec,0,0);
}
else
{
    // Send an initial buffer
    iResult = send( connectSocket, sendBuf, (int)strlen(sendBuf), 0 );
    if(false == theInstance.addToLog(m_IDEntry))
    {
        addData(ConsoleLog,rec,0,3);
    }

    if (iResult == SOCKET_ERROR) {
        closesocket(connectSocket);
        WSACleanup();
        serverConnect();
        iResult = send( connectSocket, sendBuf, (int)strlen(sendBuf), 0 );
    }

    if (iResult != SOCKET_ERROR) {
        addData(ConsoleLog,rec,0,1);
    }

    iResultRcv = recv(connectSocket, recvbuf, recvbuflen, 0);
    if ( iResultRcv <= 0 ) 
    {
       addData(ConsoleLog,rec,0,7);
    }

}

I hope someone can help me out.

1

There are 1 answers

4
john On BEST ANSWER

Don't call WSACleanup. WSACleanup is intended to be used when you no longer want to do any socket communication. That's not the case for you.