MongoDB C++ driver connecting to local server reports “Address Invalid”

165 views
Skip to first unread message

Larry Zhao

unread,
Nov 9, 2015, 4:02:53 PM11/9/15
to mongodb-user

I am trying to connect to a local MongoDB Server(3.0.7) using MongoDB C++ Legacy Driver 1.0.6 on Windows inside Visual Studio 2015.


The program is as simple as following:

// MongoTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <WinSock2.h>
#include "mongo\client\dbclient.h"

int main()
{
    mongo::DBClientConnection c;

    c.connect("127.0.0.1");

    mongo::client::initialize();

    try {
        std::cout << "connected ok" << std::endl;
    }
    catch (const mongo::DBException &e) {
        std::cout << "caught " << e.what() << std::endl;
    }

    return 0;
}


But I keep getting unhandled exception thrown from c.connect("localhost");, with errmsg: "couldn't initialize connection to host 127.0.0.1, address is invalid". But I've checked that the server is running correctly with connecting to it with mongo 127.0.0.1:27017


Why is that? How could I fix it?

Andrew Morrow

unread,
Nov 9, 2015, 4:15:41 PM11/9/15
to mongod...@googlegroups.com

Hi -

Thanks for posting a detailed question with the driver version and compilable example. However, I cannot reproduce your results. I just built legacy-1.0.6 from source, started MongoDB, compiled your program (with some small modifications) on OS X, and was able to connect. So, I think you should look into other reasons, outside of the code, that are preventing you from connecting, like firewalls or authentication settings in mongod.

Also, with respect to the code, I have a few comments:

- Including with '\' is not idiomatic, I recommend changing that to '/'
- You should call mongo::client::initialize before using any types or methods of the C++ driver. In particular, you should move your call to mongo::client::initialize above the construction of the DBClientConnection, however:
- Directly constructing DBClientInterface like this is not idiomatic. You should use the ConnectionString::parse and ConnectionString::connect methods. The examples in the src/mongo/client/examples directory all use this mechanism and you should update your code as well.

It is also possible that the misplaced call to initialize is causing more serious problems on Windows than on my system. So, I recommend making the above code changes and re-testing.

Thanks,
Andrew




--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/c1513acc-43e3-418e-9c5a-b49d981cbd90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages