MongoDB Error Status Wsastartup Failed

Repost From here http://stevepowell.ca/mongo-db-1.html
After a long and arduous struggle to get the Mongo DB C++ driver to build on Windows, I connect to the database and receive a rude reply about “WSAStartup” not being called yet. My code looked exactly the same as the stuff on the website. What could be wrong?

try
{
        mongo::DBClientConnection client;
        client.connect(std::string("localhost"));
        // Do Something useful here!
}
catch (const mongo::DBException &e) {
        std::cout << "caught " << e.what() << std::endl;
}

The connect function throws an exception with the message “getnameinfo error errno:10093 Either the application has not called WSAStartup, or WSAStartup failed.”. After digging around for a while I found out that you have to call the initialize function, which initializes WinSock. Who knew?

try
{
        mongo::client::initialize();
        mongo::DBClientConnection client;
        client.connect(std::string("localhost"));
        // Do Something useful here!
}
catch (const mongo::DBException &e) {
        std::cout << "caught " << e.what() << std::endl;
}

Now it works! Getting it to build on Windows with Visual Studio 2013 is a bit of a hacky adventure, but that’s the topic for another day.

Advertisements

3 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s