141 lines
3.7 KiB
C
141 lines
3.7 KiB
C
|
/*
|
||
|
* =====================================================================================
|
||
|
*
|
||
|
* Filename: client.c
|
||
|
*
|
||
|
* Description:
|
||
|
*
|
||
|
* Version: 1.0
|
||
|
* Created: 09/28/2018 07:14:50 PM
|
||
|
* Revision: none
|
||
|
* Compiler: gcc
|
||
|
*
|
||
|
* Author: YOUR NAME (),
|
||
|
* Organization:
|
||
|
*
|
||
|
* =====================================================================================
|
||
|
*/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <zmq.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#define fb_debug(fmt, arg...) \
|
||
|
do{\
|
||
|
printf("%s %d : ", __FILE__, __LINE__); \
|
||
|
printf(fmt, ##arg); \
|
||
|
printf("\n"); \
|
||
|
}while(0)
|
||
|
|
||
|
#define fb_assert(x, info) \
|
||
|
do{\
|
||
|
if(!(x)) { \
|
||
|
fb_debug(info); \
|
||
|
return -1;\
|
||
|
} \
|
||
|
}while(0)
|
||
|
|
||
|
int without_curve(const char *ip, int port) {
|
||
|
fb_debug("test tZMQ without curve");
|
||
|
void * ctx = zmq_ctx_new();
|
||
|
fb_assert(ctx, "create zmq context faild");
|
||
|
|
||
|
void *sock = zmq_socket(ctx, ZMQ_DEALER);
|
||
|
fb_assert(sock, "create zmq socket faild");
|
||
|
|
||
|
char szaddr[128] = {0};
|
||
|
snprintf(szaddr, sizeof(szaddr), "tcp://%s:%d", ip, port);
|
||
|
zmq_connect(sock, szaddr);
|
||
|
|
||
|
char szmsg[1024] = {0};
|
||
|
int count = 0;
|
||
|
while(1) {
|
||
|
snprintf(szmsg, sizeof(szmsg), "I say %d", ++count);
|
||
|
fb_debug("send msg : [%s]", szmsg);
|
||
|
zmq_send(sock, szmsg, strlen(szmsg), 0);
|
||
|
zmq_recv(sock, szmsg, sizeof(szmsg) - 1, 0);
|
||
|
fb_debug("recv msg : [%s]", szmsg);
|
||
|
getchar();
|
||
|
}
|
||
|
zmq_unbind(sock, szaddr);
|
||
|
zmq_close(sock);
|
||
|
zmq_ctx_term(ctx);
|
||
|
zmq_ctx_destroy(ctx);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
char *get_server_publickey(void *ctx, char *publickey, int len, const char *ip, int port) {
|
||
|
void *sock = zmq_socket(ctx, ZMQ_DEALER);
|
||
|
char szmsg[1024] = {0};
|
||
|
snprintf(szmsg, sizeof(szmsg), "tcp://%s:%d", ip, port);
|
||
|
zmq_connect(sock, szmsg);
|
||
|
snprintf(szmsg, sizeof(szmsg), "%s", "request publickey");
|
||
|
zmq_send(sock, szmsg, strlen(szmsg), 0);
|
||
|
bzero(szmsg, sizeof(szmsg));
|
||
|
zmq_recv(sock, szmsg, sizeof(szmsg), 0);
|
||
|
snprintf(publickey, len, "%s", szmsg);
|
||
|
|
||
|
return publickey;
|
||
|
}
|
||
|
|
||
|
int with_curve(const char * ip, int port) {
|
||
|
fb_debug("test tZMQ with curve");
|
||
|
void * ctx = zmq_ctx_new();
|
||
|
fb_assert(ctx, "create zmq context faild");
|
||
|
|
||
|
void *sock = zmq_socket(ctx, ZMQ_DEALER);
|
||
|
fb_assert(sock, "create zmq socket faild");
|
||
|
|
||
|
char szaddr[128] = {0};
|
||
|
snprintf(szaddr, sizeof(szaddr), "tcp://%s:%d", ip, port);
|
||
|
|
||
|
char szmsg[1024] = {0};
|
||
|
get_server_publickey(ctx, szmsg, sizeof(szmsg), ip, port + 1);
|
||
|
fb_debug("server publickey : [%s]", szmsg);
|
||
|
zmq_setsockopt(sock, ZMQ_CURVE_SERVERKEY, szmsg, strlen(szmsg));
|
||
|
|
||
|
char szpubkey[64] = {0};
|
||
|
char szprikey[64] = {0};
|
||
|
zmq_curve_keypair(szpubkey, szprikey);
|
||
|
zmq_setsockopt(sock, ZMQ_CURVE_PUBLICKEY, szpubkey, strlen(szpubkey));
|
||
|
zmq_setsockopt(sock, ZMQ_CURVE_SECRETKEY, szprikey, strlen(szprikey));
|
||
|
fb_debug("start secert comunication");
|
||
|
zmq_connect(sock, szaddr);
|
||
|
|
||
|
int count = 0;
|
||
|
while(1) {
|
||
|
bzero(szmsg, sizeof(szmsg));
|
||
|
snprintf(szmsg, sizeof(szmsg), "I say %d", ++count);
|
||
|
fb_debug("send msg : [%s]", szmsg);
|
||
|
zmq_send(sock, szmsg, strlen(szmsg), 0);
|
||
|
zmq_recv(sock, szmsg, sizeof(szmsg) - 1, 0);
|
||
|
fb_debug("recv msg : [%s]", szmsg);
|
||
|
getchar();
|
||
|
}
|
||
|
zmq_close(sock);
|
||
|
zmq_ctx_term(ctx);
|
||
|
zmq_ctx_destroy(ctx);
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int main(int argc, char * argv[]) {
|
||
|
const char *ip = "127.0.0.1";
|
||
|
int port = 5678;
|
||
|
|
||
|
int is_withvurve = 0;
|
||
|
int i = 0;
|
||
|
for(i = 0; i < argc; i++) {
|
||
|
if(strcmp("-s", argv[i]) == 0) {
|
||
|
is_withvurve = 1;
|
||
|
}
|
||
|
}
|
||
|
if(is_withvurve) {
|
||
|
with_curve(ip, port);
|
||
|
} else {
|
||
|
without_curve(ip, port);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|