PracticeDev/study_clang/zmq/mult-thread-reqrep/server.c

51 lines
1.3 KiB
C
Raw Permalink Normal View History

2022-12-20 17:31:11 +08:00
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
void *context;
void *responder;
int SendData(const void *pvData, unsigned int uiDataLen)
{
return zmq_send (responder, pvData, uiDataLen, 0);
}
int RecvData(void *pvData, unsigned int uiDataLen)
{
return zmq_recv (responder, pvData, uiDataLen, 0);
}
int main (void)
{
// Socket to talk to clients
context = zmq_ctx_new ();
responder = zmq_socket (context, ZMQ_REP);
unsigned int uiTimeout = 5 * 1000;
int rst = 0;
zmq_setsockopt(responder, ZMQ_RCVTIMEO, &uiTimeout, sizeof(uiTimeout));
zmq_setsockopt(responder, ZMQ_SNDTIMEO, &uiTimeout, sizeof(uiTimeout));
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
while (1) {
char buffer [10];
// zmq_recv (responder, buffer, sizeof buffer, 0);
printf("start\n");
rst = RecvData(buffer, sizeof buffer);
printf("end\n");
if(rst <= 0 )
{
printf("recv error: %d\n", rst);
continue;
}
printf ("Received %s\n", buffer);
sleep (3); // Do some 'work'
// zmq_send (responder, buffer, sizeof buffer, 0);
SendData(buffer, sizeof buffer);
// sleep(4);
}
return 0;
}