Created
July 29, 2015 11:38
-
-
Save sorenisanerd/3d6681c46827bb6c95f2 to your computer and use it in GitHub Desktop.
Reproduce ZMQ_LINGER problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <zmq.h> | |
#include <malloc.h> | |
#include <assert.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
int main(int argc, char *argv[]) { | |
void *ctx, *socket; | |
int rc, pid; | |
int msgsize = 0; | |
sscanf(argv[1], "%d", &msgsize); | |
if ((pid = fork()) == 0) { | |
printf("Sender pid: %d\n", getpid()); | |
ctx = zmq_ctx_new(); | |
char *buf; | |
buf = malloc(msgsize); | |
memset (buf, 'A', msgsize); | |
socket = zmq_socket (ctx, ZMQ_PUSH); | |
zmq_connect(socket, "tcp://127.0.0.1:12345"); | |
rc = zmq_send (socket, buf, msgsize, 0); | |
printf("Sent message of %d bytes\n", rc); | |
zmq_close (socket); | |
zmq_term (ctx); | |
free(buf); | |
printf("Sender exiting\n"); | |
} else { | |
char *buf; | |
int nbytes; | |
printf("Receiver pid: %d\n", getpid()); | |
buf = malloc(msgsize); | |
ctx = zmq_ctx_new(); | |
socket = zmq_socket (ctx, ZMQ_PULL); | |
zmq_bind(socket, "tcp://127.0.0.1:12345"); | |
nbytes = zmq_recv (socket, buf, msgsize, 0); | |
printf("Received msg of %d bytes\n", nbytes); | |
zmq_close (socket); | |
zmq_term (ctx); | |
free(buf); | |
printf("Receiver exiting\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment