PracticeDev/study_clang/zmq/curve_one_key/server.c

61 lines
1.7 KiB
C
Raw Permalink Normal View History

2022-12-20 17:31:11 +08:00
/*************************************************************************
> File Name : server.c
> Author : TL Song
> EMail : songtianlun@frytea.com
> Created Time : Wed 02 Dec 2020 04:50:40 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 = "*";
int port = 8102;
char szPubKey[64] = {0};
char szPriKey[64] = {0};
void * ctx = zmq_ctx_new();
void *sock = zmq_socket(ctx, ZMQ_REP);
strcpy(szPubKey, ZMQ_PUBLIC_KEY_1);
strcpy(szPriKey, ZMQ_PRIVATE_KEY_1);
int option = 1;
zmq_setsockopt(sock, ZMQ_CURVE_SERVER, &option, sizeof(option));
zmq_setsockopt(sock, ZMQ_CURVE_SECRETKEY, szPriKey, strlen(szPriKey));
char szaddr[128] = {0};
snprintf(szaddr, sizeof(szaddr), "tcp://%s:%d", ip, port);
zmq_bind(sock, szaddr);
char szmsg[1024] = {0};
while(1) {
bzero(szmsg, sizeof(szmsg));
zmq_recv(sock, szmsg, sizeof(szmsg) - 1, 0);
printf("recv msg : [%s]\n", szmsg);
if(strcasecmp("bye", szmsg) == 0) {
printf("send msg : [%s]\n", szmsg);
zmq_send(sock, szmsg, strlen(szmsg), 0);
break;
}
strcat(szmsg, ", too");
printf("send msg : [%s]\n", szmsg);
zmq_send(sock, szmsg, strlen(szmsg), 0);
}
zmq_unbind(sock, szaddr);
zmq_close(sock);
zmq_ctx_term(ctx);
zmq_ctx_destroy(ctx);
return 0;
}