0

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

Alireza Sanaee
  • 465
  • 1
  • 7
  • 21
  • According to the documentation I found, you need to check if `res` is null or not to know if there is an error, and if so, `rc` will contain the error that you can use `memcached_strerror` to describe. It's also an enum, so you should be able to match it up that way too. It would help to see all of your code, including the variable definitions and initialization to see if perhaps there's something wrong there. – Retired Ninja Mar 19 '18 at 13:09
  • @RetiredNinja Thank you for the reply I just updated the code, Actually the error is "SERVER HAS FAILED AND IS DISABLED UNTIL TIMED RETRY", how it is possible while the server is fine with UDP set requests!!!?? – Alireza Sanaee Mar 19 '18 at 13:24
  • @AlirezaSanaee I explained that below, did you overlook that? – Ctx Mar 19 '18 at 13:29
  • @Ctx Yeah I see thanks :) Bad luck for me I need to implement it on my own then:))) – Alireza Sanaee Mar 19 '18 at 13:30

1 Answers1

0

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.

Ctx
  • 18,090
  • 24
  • 36
  • 51