PracticeDev/study_clang/zmq/curve_one_key/client.c

62 lines
1.8 KiB
C

/*************************************************************************
> File Name : client.c
> Author : TL Song
> EMail : songtianlun@frytea.com
> Created Time : Wed 02 Dec 2020 04:50:49 PM CST
************************************************************************/
#include <stdio.h>
#include <zmq.h>
#include <string.h>
#define ZMQ_PUBLIC_KEY_1 "@M[-VQ%R2MKguD/h/0w7.fNq%GGjV!1Q:bVeu*u>"
#define ZMQ_PRIVATE_KEY_1 "S*{[[V$7f<(jA<XWT?&baG>p^]M>I!n?dN]SXgB]"
#define ZMQ_PUBLIC_KEY_2 "p=lDOa9WKUKz!I9{G)uPX4@&CrV-(>tDg:kaSGzE"
#define ZMQ_PRIVATE_KEY_2 "q0]#)<fZP>iuwR*H5hz.}<AR^RSWunZ8H+oc8l1k"
int main()
{
const char *ip = "127.0.0.1";
int port = 5678;
char szPubKey[64] = {0};
char szPriKey[64] = {0};
char szSerKey[64] = {0};
void * ctx = zmq_ctx_new();
void *sock = zmq_socket(ctx, ZMQ_REQ);
strcpy(szSerKey, ZMQ_PUBLIC_KEY_1);
char szaddr[128] = {0};
snprintf(szaddr, sizeof(szaddr), "tcp://%s:%d", ip, port);
zmq_setsockopt(sock, ZMQ_CURVE_SERVERKEY, szSerKey, strlen(szSerKey));
zmq_curve_keypair(szPubKey, szPriKey);
//strcpy(szPubKey, ZMQ_PUBLIC_KEY_1);
//strcpy(szPriKey, ZMQ_PRIVATE_KEY_1);
zmq_setsockopt(sock, ZMQ_CURVE_PUBLICKEY, szPubKey, strlen(szPubKey));
zmq_setsockopt(sock, ZMQ_CURVE_SECRETKEY, szPriKey, strlen(szPriKey));
zmq_connect(sock, szaddr);
int count = 0;
char szmsg[1024] = {0};
while(1) {
bzero(szmsg, sizeof(szmsg));
snprintf(szmsg, sizeof(szmsg), "I say %d", ++count);
printf("send msg : [%s]\n", szmsg);
zmq_send(sock, szmsg, strlen(szmsg), 0);
zmq_recv(sock, szmsg, sizeof(szmsg) - 1, 0);
printf("recv msg : [%s]\n", szmsg);
getchar();
}
zmq_close(sock);
zmq_ctx_term(ctx);
zmq_ctx_destroy(ctx);
return 0;
}