Reputation: 684
I'm writing a small C++ application that is able to connect to a memcachedb
instance using the libmemcached
C++ API.
The memcachedb
instance was created using the following command:
memcachedb -m 64 -p 21201 -A 4096 -u memcachedb -l 127.0.0.1 -H /var/lib/memcachedb -f /var/lib/memcachedb/default.db -U off
I am able to retrieve the server STATS by doing:
telnet 127.0.0.1 21201
stats
STAT pid 1972
STAT uptime 124936
STAT time 1428898165
STAT version 1.2.0
STAT pointer_size 64
STAT rusage_user 5.687019
STAT rusage_system 13.329336
STAT ibuffer_size 512
STAT curr_connections 5
STAT total_connections 12
STAT connection_structures 6
STAT cmd_get 4
STAT cmd_set 4
STAT get_hits 4
STAT get_misses 0
STAT bytes_read 236
STAT bytes_written 2171
STAT threads 4
END
To retrieve the server STATS programatically I am using the following code:
#include <libmemcached/memcached.hpp>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
using namespace memcache;
int main(int argc, char **argv) {
std::string host(argv[1]);
int port;
istringstream(std::string(argv[2])) >> port;
printf("%s\n", "Instantiating client");
Memcache first_client;
printf("Adding server host=[%s] port=[%d]\n", host.c_str(), port);
first_client.addServer(host, port);
printf("%s\n", "Getting server STATS");
map<string, map<string, string> > my_stats;
bool gotStats = first_client.getStats(my_stats);
if (gotStats) {
printf("%s\n", "Got STATS");
} else {
printf("%s\n", "Unable to get STATS");
}
return EXIT_SUCCESS;
}
I am compiling the application using the following Makefile
:
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
OBJS = MemcachedExample.o
LIBS = -lmemcached
TARGET = MemcachedExample
$(TARGET): $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)
When I execute the application using the following command:
MemcachedExample 127.0.0.1 21201
It fails to retrieve the server STATS:
Instantiating client
Adding server host=[127.0.0.1] port=[21201]
Getting server STATS
Unable to get STATS
What am I doing wrong? I would appreciate any insight.
Upvotes: 0
Views: 416
Reputation: 684
Looking at the tcpdump as pointed by @Rastmaj I found that in the dump for the connection via my program there were no packets related with the connection I was trying to establish.
This made me take look at the code of the C++ interface for the C Client Library for memcached. I found that the empty constructor was initializing the memcached_st
object using memcached("", 0)
while the constructor receiving hostname
and port
arguments not only initialized the memcached_st
object using memcached("", 0)
but also added a server using the memcached_server_add
function.
Though there is no apparent reason why does it work when adding a server just after initializing the memcached_st
object, since the C++ method memcache::Memcache::addServer
does exactly the same, the solution I found was simply changing:
Memcache first_client;
for
Memcache first_client("--SERVER=" + host + ":" + std::string(argv[2]));
This solves my problem but I still lack an explanation of this behavior, further insights are appreciated.
Upvotes: 0