-1

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.

MD XF
  • 7,860
  • 7
  • 40
  • 71
Jam
  • 5
  • 3

1 Answers1

1

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • Is it ok if I don't call WSACleanup if I try to reconnect again to the server when there is another input? Since my server connect code has the WSAStartup. – Jam Sep 03 '13 at 06:15
  • You should move WSAStartup as well. Call WSAStartup once when your program starts, and call WSACleanup once when your program exits. – john Sep 03 '13 at 08:51
  • thank you very much for your help. I just made the server to accept two signals and just sort of rearranged my client code. The sql does not give the error anymore. – Jam Sep 03 '13 at 09:10
  • That being said, you *can* call `WSAStartup()`/`WSACleanup()` multiple times, as long as they are balanced correctly. Winsock is reference counted internally. The first `WSAStartup()` actually initializes Winsock, then subsequent `WSAStartup()` calls increment the reference count. `WSACleanup()` decrements the reference count, and when the reference count falls to 0 then the final call actually finalizes Winsock. – Remy Lebeau Sep 04 '13 at 01:21