PracticeDev/study_clang/zmq/zhelpers_server2.c

58 lines
1.2 KiB
C
Raw Permalink Normal View History

2022-12-20 17:31:11 +08:00
#include "zhelpers.h"
struct Msg
{
int stat;
char id[100];
};
int main (int argc, char *argv [])
{
const char* param = NULL;
if (argc > 1)
{
param = argv[1];
}
else
{
printf("connect where: ip/port!\n");
}
char addr[40];
sprintf(addr, "tcp://%s", param);
// 准备上下文和PUB套接字
void *context = zmq_init (1);
void *publisher = zmq_socket (context, ZMQ_PUB);
zmq_bind (publisher, addr);
//zmq_bind (publisher, "ipc://weather.ipc");
// 初始化随机数生成器
srandom ((unsigned) time (NULL));
while (1) {
// 生成数据
int protocol_serial;
protocol_serial = randof (1);
char buf [200];
size_t buf_len = 0;
memset(buf, 0, sizeof(buf));
printf("buf: %d %s\n", strlen(buf), buf);
struct Msg msg;
msg.stat = 0;
const char * str = "aa";
memcpy(msg.id, str, strlen(str));
sprintf (buf, "%d|%d|%d|%s", protocol_serial, msg.stat, strlen(msg.id), msg.id);
printf("buf: %d %s\n", strlen(buf), buf);
printf("send %d %d %d %s --> %s \n", protocol_serial, msg.stat, strlen(msg.id), buf, addr);
s_send (publisher, buf);
sleep(2);
}
zmq_close (publisher);
zmq_term (context);
return 0;
}