cmemcachedlibmemcachedlibmemcache

Unable to send UDP get request to memcached server with libmemcached module


I'm writing a Memcached UDP client with libmemcached/memcached.h to send some arbitrary loads on Memcached server. I can send set requests in UDP but I'm unable to send get requests, here is the snippet I wrote for this!

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libmemcached/memcached.h>
#include <time.h>
#include <assert.h>
#include <stdlib.h>
#include <math.h>
#include <signal.h>

int main(int argc, char *argv[])
{
  memcached_server_st *servers = NULL;
  memcached_st *memc;
  memcached_return rc;
  char * res;



  char *key= "kay";
  int size = 1000;
  char * value = malloc(size);
  uint32_t flags;
  size_t return_value_length;
  char * ip = argv[2];

  memset(value, 1, size);
  memc = memcached_create(NULL);
  servers= memcached_server_list_append(servers, ip, 11211, &rc);
  rc = memcached_server_push(memc, servers);
  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_USE_UDP, (uint64_t)1);

  rc = memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0);

  if (rc == MEMCACHED_SUCCESS)
    fprintf(stderr,"Key stored successfully\n");
  else
    fprintf(stderr,"Couldn't store key: %s\n",memcached_strerror(memc, rc));


  while ( true)  
  {
    res = memcached_get(memc, key, strlen(key), &return_value_length, &flags, &rc);  
    free(res);
    if(rc != MEMCACHED_SUCCESS)
    {
      fprintf(stderr, "%s\n", memcached_strerror(memc, rc));
    }
  }

  memcached_free(memc);

  return 0;

Actually, get requests always are unsuccessful!!! I also monitored the packets with Wireshark the client doesn't even send any packet out to the server!!! And it just got failed while sending get request.

Is there any obvious problem in the code which I couldn't see?

Thank you


Solution

  • According to the manpage of memcached_behaviour_set():

    The following operations will return MEMCACHED_NOT_SUPPORTED when executed with the MEMCACHED_BEHAVIOR_USE_UDP enabled:

    memcached_version(), memcached_stat(), memcached_get(), memcached_get_by_key(), memcached_mget(), memcached_mget_by_key(), memcached_fetch(), memcached_fetch_result(), memcached_fetch_execute().

    So indeed, memcached_get() does not work when the memcached-handle is set to UDP transport. You will have to use TCP for that.