Failed to connect to Redis (using redis-plus-plus) - c++

So I installed Redis in OpenSuse 15.0 and everything was working well. I was using redis-cli for checking the communication with redis server and it was ok.
I wanted to use redis-plus-plus as I'm coding in C++. Installation was succesfully as it passed all tests.
But then I created my own script (inside general redis-plus-plus folder) with the following code:
#include <sw/redis++/redis++.h>
#include <iostream>
#include <unordered_set>
using namespace sw::redis;
int main()
{
// Create an Redis object, which is movable but NOT copyable.
auto redis = Redis("tcp://127.0.0.1:6379");
redis.set("key", "val");
}
It's a very simple code, just to check that connection was succesfully.
And I was compiling it as follows:
/usr/bin/g++-10 -std=c++17 -o main main.cpp /home/user/redis-plus-plus/test/libredis++.a /home/user/hiredis/build/libhiredis_static.a -pthread
Compilation was ok, but when I execute the ./main I'm getting the following error:
terminate called after throwing an instance of 'sw::redis::Error'
what(): unknown error code: Failed to connect to Redis:
Abortado (`core' generado)
And I have another window with the server running, so I don't know why I can't connect with Redis server and I don't know what else can I try... The problem is with the line
redis.set("key", "val");
because if I remove this line I have no error. Any help will be deeply apreciated, thanks!
UPDATE: The problem above was solved, now I'm facing something similar. I'm trying to implement two more lines:
#include <sw/redis++/redis++.h>
#include <iostream>
using namespace sw::redis;
int main()
{
auto redis = Redis("tcp://127.0.0.1:6379");
redis.set("key", "new_val270622");
auto redis_cluster = RedisCluster("tcp://127.0.0.1:16379");
redis_cluster.set("key", "new_val");
}
And I'm getting this error:
CLUSTERDOWN The cluster is down
If I do
ss -tulpn
I get the following ports:
In redis.config I have made the following changes:
cluster-enabled yes
cluster-config-file nodes-16379.conf
cluster-require-full-coverage yes
And I also used:
redis-cli --cluster fix 127.0.0.1:6379
What am I doing wrong? Thank you so much!!!!

Related

How to launch a macos .app from C++ (nonblocking)?

What's the best way to launch a macosx .app from a C++ program, without blocking the C++ program?
ie given a macos x app at path:
/somewhere/foo.app
How can I within a C++ program:
int main() {
run_mac_app("/somewhere/foo.app"); // returns immediately
// after main exits, foo.app continues running.
}
How do I implement run_mac_app?
I guess this is what you need to run in the terminal:
open -a /somewhere/foo.app
If you want this app to run in the background, then you have to add -g
open -g -a /somewhere/foo.app
So Finally,
#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
std::system("open -g -a /somewhere/foo.app");
}
Reference from:
https://en.cppreference.com/w/cpp/utility/program/system
https://pubs.opengroup.org/onlinepubs/009695399/functions/exec.html

Compiling without "-L /usr/local/lib" yields a "cannot connect to socket" error while trying out jaeger opentracing in C++

I'm trying out OpenTracing Jaeger and have the following file test.cpp:
#include <opentracing/tracer.h>
#include <jaegertracing/Tracer.h>
#include <yaml-cpp/yaml.h>
#include <iostream>
void setUpTracer(const char* configFilePath)
{
auto configYAML = YAML::LoadFile(configFilePath);
auto config = jaegertracing::Config::parse(configYAML);
auto tracer = jaegertracing::Tracer::make(
"example-service", config, jaegertracing::logging::consoleLogger());
opentracing::Tracer::InitGlobal(
std::static_pointer_cast<opentracing::Tracer>(tracer));
}
int main(int argc, char* argv[]){
setUpTracer(argv[1]);
std::cout<<"Hello\n";
return 0;
}
And consider config.yml to be:
disabled: false
reporter:
logSpans: true
sampler:
type: const
param: 1
Now if I compile test.cpp with g++ test.cpp -lopentracing -ljaegertracing -lyaml-cpp and run ./a.out config.yml, I get a
ERROR: cannot connect to socket: Cannot connect socket to remote address { scheme="http", host="127.0.0.1", port=5778, path="/sampling", query="" }
While if I compile with g++ test.cpp -L /usr/local/lib -lopentracing -ljaegertracing -lyaml-cpp, I get a good
INFO: Initializing logging reporter
Hello
The contents of my /usr/local/lib are:
cmake libopentracing.so.1.6.0 libthriftqt.so
libjaegertracing.a libthrift-0.11.0.so libthrift.so
libjaegertracing.so libthrift.a libthriftz-0.11.0.so
libjaegertracing.so.0 libthrift_c_glib.a libthriftz.a
libjaegertracing.so.0.6.0 libthrift_c_glib.la libthriftz.la
libopentracing.a libthrift_c_glib.so libthriftz.so
libopentracing_mocktracer.a libthrift_c_glib.so.0 libyaml-cpp.a
libopentracing_mocktracer.so libthrift_c_glib.so.0.0.0 pkgconfig
libopentracing_mocktracer.so.1 libthrift.la python2.7
libopentracing_mocktracer.so.1.6.0 libthriftqt-0.11.0.so python3.6
libopentracing.so libthriftqt.a
libopentracing.so.1 libthriftqt.la
I'm using Jaeger in conjunction with a larger project involving ROS and get the same error even if I add_compile_options(-L /usr/local/lib) for CMakeLists.txt of the appropriate package; so, I wanted to better understand what the exact cause of the above error is, so hopefully that helps me to fix the one involved in ROS.
Thanks!
You are missing the configuration of Jaeger address. Since you did not provided it, it is trying to connect to the default one, which is TCP protocol, 127.0.0.1 and port 5778.
Check for details the configuration section here.

Shutting down and Restarting PC via C++ in WSL

Here is my program to shutdown the PC in c++, and I use vs code editor and WSL to run this program:
#include<iostream>
#include<stdlib.h>
int main()
{
system("C:\\Windows\\System32\\shutdown /i ");
}
I got this message sh: 1: C:WindowsSystem32shutdown: not found.
Make sure you use the appropriate path. The correct form for WSL via Linux is "/mnt/c/Windows/System32/shutdown.exe" as mentioned by prog-fh and code_fodder.
So this will work: (I haven't tested it in WSL, but the above users did and know better)
std::system("/mnt/c/Windows/System32/shutdown.exe /i");
or for shutdown you can use s as well:
std::system("/mnt/c/Windows/System32/shutdown.exe /s");
Likewise, for restarting, use r:
std::system("/mnt/c/Windows/System32/shutdown.exe /r");

Query on ACE framework

Of late, I am facing a problem with a code snippet which doesn't return expected values and this is causing an application to fail.
The code is built on Redhat linux 7.1 using the following command -
g++ ace-test.cpp -I<path-to-ace-6.2-root> -L<path-to-ace-6.2-root>/ace/Linux -g -lACE
The code snippet being built is pasted below -
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ace/MEM_Addr.h"
#include <iostream>
using namespace std;
int main(int argc, char* []) {
ACE_MEM_Addr addr ;
cout << "ACE_MEM_Addr::addr.get_host_name() " << addr.get_host_name() << endl ;
}
On execution, the code prints the following line
ACE_MEM_Addr::addr.get_host_name() **unknown**
on a system running linux which was recently patched.
On a different system which was not patched, the same code returns the correct hostname.
I am trying to figure out which patch caused this problem, but couldn't make much progress.
If someone can please extend a bit of help, would greatly appreciate it.
If required, I can share the list of patches which were applied.
Finally managed to find the culprit.
Here are the entries in /etc/nsswitch.conf:
hosts: files dns - (On the host without patches)
versus
hosts: files dns myhostname - (On the host which was patched)
Removal of the myhostname entry in the latter fixed the problem.
Associated RHEL knowledge base entry -
https://access.redhat.com/solutions/2766251
Thanks a bunch for all the help !

Trying to get fastcgi to work in nginx and c++

Below is my c++ code and my config file.
when I run spawn-fcgi -a120.0.0.1 -p9000 -n ./rtb.o
I get this error
spawn-fcgi: exec failed: Exec format error
Here is my c++ code that I complied as rtb.o
#include "fcgi_stdio.h"
#include <stdlib.h>
using namespace std;
int main()
{
int count = 1;
while(FCGI_Accept() >= 0)
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello!</title>"
"<h1>FastCGI Hello!</h1>"
"Request number %d running on host \n",
++count);
return 0;
}
So, what did I do wrong?
You're attempting to run a program called rtb.o? Is this an object file or an executable? You might want to show us how you compile your program. If you're doing something like
g++ -c rtb.cpp
Then you will get an object file and you need to link it to get a working program. Try to run it from your terminal using ./rtb.o. If it prints the same message, then you've got an object file and need to try something like this instead:
g++ -o rtb rtb.cpp
Remember to add a reference to the FCGI library when you link (use the -l option).