Can't connect to couchbase via C SDK


#1

Sorry guys, I am at Day 1 using this platform, and I can’t seem to get by the first basic steps! lcb_connect is calling the error handler with “Could not connect to server within allotted time”. Below is the code. I get LCB_SUCCESS as the immediate return from the connect call (the error is detected in the error handler).

I have verified firewall allows connection (and I telnet’d in, and if I hit enter or type anything, it says “ERROR”). Obviously, I blanked out the pwd. I was not sure what to use as the user since that was not specified when the bucket was created, only the password.

I am a little lost – thought this was going to be a one-day integration!!! :slight_smile:

Thanks,
Bret

#include <stdio.h>
#include "libcouchbase/couchbase.h"

#define CHECK(t,e) 	printf("%s; %s\n",t, (e==LCB_SUCCESS?"Pass":"Fail"));

static enum {
	CB_CONNECT = 1,
	CB_GET,
	CB_ERROR = 999,
	CB_NONE
};

int STATE = 0;

void cb_config (lcb_t cbi, lcb_configuration_t cbconfig) {
	printf ("Config Callback: ");
	switch (cbconfig) {
		case LCB_CONFIGURATION_NEW: printf ("NEW"); break;
		case LCB_CONFIGURATION_CHANGED: printf ("CHANGED"); break;
		case LCB_CONFIGURATION_UNCHANGED: printf ("UNCHANGED"); break;
		default: printf ("OTHER"); break;
	}
	printf ("\n");
}

void cb_get (lcb_t cbi, const void * cookie, lcb_error_t cberr, const lcb_get_resp_t * rsp) {
	STATE = (cberr==LCB_SUCCESS ? STATE : CB_ERROR);
}

void cb_error (lcb_t cbi, lcb_error_t cberr, const char * message) {
	STATE = (cberr==LCB_SUCCESS ? STATE : CB_ERROR);
}

int main (int argc, char ** argv) {

	// init the couchbase lib

	lcb_t cbi;
	struct lcb_create_st cbopts;
	memset (&cbopts, 0, sizeof(cbopts));

	cbopts.version = 1;
	cbopts.v.v1.host = "levycodev.com:11211";
	cbopts.v.v1.bucket = "DNP";
	cbopts.v.v1.user = "DNP";
	cbopts.v.v1.passwd = "*********";
	cbopts.v.v1.type = LCB_TYPE_BUCKET;
	lcb_error_t cberr = lcb_create (&cbi, &cbopts);
	CHECK ("LCB:Create", cberr);

	// configure handlers

	lcb_set_configuration_callback (cbi, cb_config);
	lcb_set_error_callback (cbi, cb_error);
	lcb_set_get_callback (cbi, cb_get);

	// connect to the cluster/instance

	STATE = CB_CONNECT;

	cberr = lcb_connect (cbi);
	CHECK ("LCB:Connect", cberr);
	lcb_wait(cbi);
	CHECK ("LCB:Connect; after wait", STATE);

	// read the test document

	STATE = CB_GET;

	lcb_get_cmd_t * cmd = (lcb_get_cmd_t*) calloc(1,sizeof(lcb_get_cmd_t));
	cmd->version = 0;
	cmd->v.v0.key = "this-is-a-test";
	cmd->v.v0.nkey = strlen((const char *)cmd->v.v0.key);
	lcb_get_cmd_st * cmds[] = { cmd };
	cberr = lcb_get (cbi, "test", 1, cmds);
	CHECK ("LCB:Get", cberr);
	lcb_wait(cbi);
	CHECK ("LCB:Get; after wait", STATE);

	// tests done

	return (0);

}

#2

Turned out it was quite simple! Even though I specified 11211 as the port for my bucket, I still have to interact over 8091. Oh well, works just fine now!


#4

I met the same problem, how did you solve your problem? :smile:


#5

Curious. What version are you using? Newer code should employ the ‘bootstrap callback’ and not the ‘configuration callback’ or ‘error callback’. Both these functions are deprecated and will go away soon.

I’m interested in knowing what your starting point was with these examples, so we can fix them :smile:


#6

Not sure what version I am using right now. I have not updated it since I initially installed it about a year ago. There are/were a lot of bugs in the libcouchbase code, so once I worked around all those, I didn’t want to change! Oddly, one of the two main bugs I found had to do with the bootstrap callback, so I ended up not using it. The other had to do with setting timeouts. If you set a timeout, once that interval expires, libcouchbase will trap on some Windows OS function (forgot what it was). Here’s my current “start up” code (relevant part anyway), with the bootstap and timeout stuff commented out.

The documention I was using at the time was the version before this new Doxygen version. The old version was better because it was easier to find what you were looknig for. The new version would be okay if there was a search function, IMHO.

Best,
Bret

int CCouchbase::open (const char * host, const char * database) {

    // init the config

    CBuffer url(host == NULL ? DBHost.Data() : host);
    //url.Cat (":8091");

	memset (&myopts, 0, sizeof(myopts));

	myopts.version = 1;
	myopts.v.v1.host = url.Data();
	myopts.v.v1.bucket = (database == NULL ? DBName.Data() : database);
	myopts.v.v1.user = DBUser.Data();
	myopts.v.v1.passwd = DBPwd.Data();
	myopts.v.v1.type = LCB_TYPE_BUCKET;
	lcb_error_t cberr = lcb_create (&mycbi, &myopts);

	// configure handlers

	lcb_set_configuration_callback (mycbi, configCallback);
	lcb_set_error_callback (mycbi, errorCallback);
	lcb_set_get_callback (mycbi, getCallback);
	lcb_set_store_callback (mycbi, putCallback);
	lcb_set_remove_callback (mycbi, deleteCallback);
	//lcb_set_bootstrap_callback (mycbi, bootstrapCallback);

    // setup timeouts

    //lcb_uint32_t timeout = 10*1000*1000;
    //lcb_set_timeout (mycbi, timeout);
    //lcb_cntl(mycbi, LCB_CNTL_SET, LCB_CNTL_OP_TIMEOUT, &timeout);


#7

Doxygen allows a search function; though I don’t know if it would fit your needs. All library symbols can be found at http://docs.couchbase.com/sdk-api/couchbase-c-client-2.4.4/globals.html.

About 80% of the code was completely rewritten and released in June of this year (version 2.4.0), we’re at version 2.4.4 now. The 2.4.x series has many improvements, including much better Windows support. From the description you provide, it would seem as if you used a pre-release version of the library.

Additionally, if you are using a Couchbase-type bucket and a cluster version 2.5 or greater, there does not need to be any communication via port 8091. You can enforce this by adding the bootstrap_on=cccp option in the connection string.