C++ で Redis のデータを読む (Read)

```cpp:redis_read.cpp
// --------------------------------------------------------------------
/*
	redis_read.cpp

					May/31/2018

*/
// --------------------------------------------------------------------
#include <iostream>
#include <boost/algorithm/string.hpp>

#include <arpa/inet.h>

using namespace std;
#define PORT 6379
// --------------------------------------------------------------------
string redis_received_data_parser
	(ssize_t bytes_recieved,char incoming_data_buffer[])
{
	string str_rvalue;

	vector<string> vv;
	boost::algorithm::split (vv,incoming_data_buffer,boost::is_any_of("\n"));


	if (vv[0][1] == '-')
		{
		str_rvalue = "";
		}
	else
		{
		str_rvalue = vv[1];
		}

	return	str_rvalue;
}
// --------------------------------------------------------------------
void json_record_display (string key_in,int valread,char buffer[])
{
	cout << key_in << "\t";
	cout << valread << "\n";
	string json_str =  redis_received_data_parser (valread,buffer);
	cout << json_str << "\n";
}

// --------------------------------------------------------------------
int main (int argc,char *argv[])
{
	cerr << "*** 開始 ***\n";

	sockaddr_in address;
	sockaddr_in serv_addr;

	int sock = 0;

	char buffer[1024] = {0};
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		{
		printf("\n Socket creation error \n");
		return -1;
		}

	memset(&serv_addr, '0', sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(PORT);

	if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) 
		{
		printf("\nInvalid address/ Address not supported \n");
		return -1;
		}

	printf("sock = %d\n",sock);
	printf("sizeof(serv_addr) = %ld\n",sizeof(serv_addr));

	if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
		{
		printf("\nConnection Failed \n");
		return -1;
		}

	string keys[] = {"t1851","t1852","t1853","t1854","t1855", 
			"t1856","t1857","t1858","t1859"};
	const int nmax = 9;


	for (int it=0; it < nmax; it++)
		{
		const string key = keys[it];
		cerr << "it = " << it << "\t";
		cerr << "key = " << key << "\t";
		const string str_aa = "get " + key +  "\r\n";
		const char *hello = str_aa.c_str();
		send(sock , hello , strlen(hello) , 0 );
		int valread = read( sock , buffer, 1024);
//		printf("%s\n",buffer );
		json_record_display (key,valread,buffer);
		}


	cerr << "*** 終了 ***\n";

	return 0;
}

// --------------------------------------------------------------------
```

```text:Makefile
redis_read: redis_read.cpp
	clang++ -o redis_read redis_read.cpp -ljson11
clean:
	rm -f redis_read
```

実行結果

```text
$ ./redis_read
*** 開始 ***
sock = 3
sizeof(serv_addr) = 16
it = 0	key = t1851	t1851	71
{"date_mod": "1922-8-27", "name": "福井", "population": 76129}
it = 1	key = t1852	t1852	70
{"date_mod": "1922-2-2", "name": "敦賀", "population": 98637}
it = 2	key = t1853	t1853	71
{"date_mod": "1922-7-21", "name": "小浜", "population": 42915}
it = 3	key = t1854	t1854	71
{"date_mod": "1922-5-25", "name": "大野", "population": 51238}
it = 4	key = t1855	t1855	70
{"date_mod": "1922-1-5", "name": "勝山", "population": 83471}
it = 5	key = t1856	t1856	71
{"date_mod": "1922-9-22", "name": "鯖江", "population": 71249}
it = 6	key = t1857	t1857	74
{"date_mod": "1922-7-17", "name": "あわら", "population": 46185}
it = 7	key = t1858	t1858	70
{"date_mod": "1922-3-4", "name": "越前", "population": 61873}
it = 8	key = t1859	t1859	70
{"date_mod": "1922-7-9", "name": "坂井", "population": 74923}
*** 終了 ***
```

次のバージョンで確認しました。

```text
$ clang++ --version
clang version 11.1.0
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
```