PracticeDev/study_clang/Mimic/OperateNos/test_server/server.c

76 lines
2.0 KiB
C
Raw 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"
#define WEB_EXEC_FAILED 201
#define WEB_EXEC_SUCCES 202
typedef enum Web_Op_E
{
NOS_POWER_ON=1,
NOS_POWER_OFF,
NOS_RESTART
}Web_Op_T;
typedef struct Web_Msg_S
{
unsigned int nos_id;
Web_Op_T nos_op;
unsigned int result; //0:defaule 201:failed 202:succeed
}Web_Msg_T;
int main()
{
const char *ip = "*";
int port = 8201;
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};
Web_Msg_T msg;
while(1) {
memset(&msg, 0x00, sizeof(msg));
zmq_recv(sock, &msg, sizeof(msg) - 1, 0);
printf("recv msg from nos %d, option %d, result %d\n", msg.nos_id, msg.nos_op, msg.result);
msg.result = WEB_EXEC_SUCCES;
zmq_send(sock, &msg, sizeof(msg), 0);
printf("send msg from nos %d, option %d, result %d\n", msg.nos_id, msg.nos_op, msg.result);
zmq_send(sock, &msg, sizeof(msg), 0);
}
zmq_unbind(sock, szaddr);
zmq_close(sock);
zmq_ctx_term(ctx);
zmq_ctx_destroy(ctx);
return 0;
}