PracticeDev/study_clang/zmq/docker_zmq_ubuntu_server/hw_server.cpp

44 lines
992 B
C++
Raw Permalink Normal View History

2022-12-20 17:31:11 +08:00
// Hello World server
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
struct send_msg
{
int msgId;
string msgData;
};
int main (void)
{
// Socket to talk to clients
void *context = zmq_ctx_new ();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
while (1) {
send_msg msg;
memset(&msg, 0, sizeof(send_msg));
char buffer [10];
cout << "Recv Message..." << endl;
zmq_recv (responder, &msg, sizeof(send_msg), 0);
cout << "Received msgid: " << msg.msgId << ", msdData: " << msg.msgData << endl;
//printf ("Received msgid: %d, msdData: %s\n",msg.msgId,msg.msgData);
sleep (1); // Do some 'work'
zmq_send (responder, "World", 5, 0);
}
return 0;
}