Skip to content

Instantly share code, notes, and snippets.

@dsdstudio
Created January 21, 2014 12:30
Show Gist options
  • Save dsdstudio/8539151 to your computer and use it in GitHub Desktop.
Save dsdstudio/8539151 to your computer and use it in GitHub Desktop.
redis client test
// redis-cli.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#define REDIS_PORT 6379
#define REDIS_HOST "127.0.0.1"
#define RedisAskingPriceTable "stockrules_recently_ap"
#define RedisMarketPriceTable "stockrules_recently_mp"
using namespace std;
#include <iostream>
static void sendPacket(SOCKET s, char *shcode, char *data, int datalen) {
char buf[2048];
int n = sprintf_s(buf, "*4\r\n$4\r\nHSET\r\n$22\r\n%s\r\n$6\r\n%s\r\n$%d\r\n%s\r\n", RedisAskingPriceTable, shcode, datalen, data);
buf[n] = '\0';
cout << buf << " " << n << endl;
if ( send(s, buf, n, 0) < 0 ) {
cout << "패킷보내기 실패 ! " << endl;
return;
}
cout << "패킷 보냄" << endl;
int recvsize;
char replybuf[1024];
if((recvsize = recv(s, replybuf, sizeof(replybuf), 0)) == SOCKET_ERROR ) {
cout << "패킷 받기 실패 !" << endl;
return;
}
replybuf[recvsize] = '\0';
cout << replybuf << endl;
}
int _tmain(int argc, _TCHAR* argv[]) {
WSAData wsaData;
SOCKET sock;
struct sockaddr_in remote;
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
cout << "실패! " << WSAGetLastError() << endl;
return -1;
}
if ( (sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) == INVALID_SOCKET ){
cout << "소켓 생성 실패" << endl;
return -1;
}
remote.sin_addr.s_addr = inet_addr(REDIS_HOST);
remote.sin_family = AF_INET;
remote.sin_port = htons(REDIS_PORT);
if ( connect(sock, (struct sockaddr *)&remote, sizeof(remote)) < 0 ) {
cout << "연결 실패 ! " << endl;
return -1;
}
cout << "레디스와 연결됨 " << endl;
sendPacket(sock, "002530", "melong kekeki", 13);
WSACleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment