Skip to content

Instantly share code, notes, and snippets.

@somdoron
Last active October 16, 2017 04:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save somdoron/9423196a228775c8f5af to your computer and use it in GitHub Desktop.
Save somdoron/9423196a228775c8f5af to your computer and use it in GitHub Desktop.
radiodish
zsock_t *radio = zsock_new_radio ("inproc://zframe-test-radio");
zsock_t *dish = zsock_new_dish ("inproc://zframe-test-radio");
// Use following for multicast
// zsock_t *radio = zsock_new_radio ("udp://239.0.0.1:55555");
// zsock_t *dish = zsock_new_dish ("udp://239.0.0.1:55555");
// Join to group
rc = zsock_join (dish, "World");
assert (rc == 0);
zframe_t *message = zframe_new ("Hello", 5);
rc = zframe_set_group (message, "World");
assert (rc == 0);
rc = zframe_send (&message, radio, 0);
assert (rc == 0);
message = zframe_recv (dish);
assert (message);
assert (zframe_streq (message, "Hello"));
@ju-emb
Copy link

ju-emb commented Jun 28, 2017

very nice.

I tried to put the radio and dish in 2 programs and let them communicate over TCP.
When I start the radio first then the dish all works as expected.
When I start the dish first, the dish cannot be interrupted by ctlC until I start the radio.

I'm new to zmq so maybe I missed something.

I use ØMQ version 4.2.3 on a Ubuntu box

here my source code
TestRadio.c

#include "czmq.h"

int main(int argc, char **argv) {

	int zmq_major, zmq_minor, zmq_patch;
	zmq_version (&zmq_major, &zmq_minor, &zmq_patch);
	fprintf (stdout, "Current ØMQ version is %d.%d.%d\n", zmq_major, zmq_minor, zmq_patch);

	int rc = 0;
	int dd = 0;
	zsock_t *radio = zsock_new_radio ("tcp://lo:5555");
	if (!radio) {
		fprintf(stderr,"create dish failes : %s \n", zmq_strerror (errno));
	}
	assert (radio);

	while (!zsys_interrupted) {
		char s[20] = "";
		sprintf(s, "msg No. : %d\n", dd++);
		zframe_t *message = zframe_new (s, strlen(s));
		rc = zframe_set_group (message, "World");
		assert (rc == 0);
		rc = zframe_send (&message, radio, 0);
		assert (rc == 0);
		zframe_destroy(&message);
		zclock_sleep (500);
	}
	zsock_destroy(&radio);

	return EXIT_SUCCESS;
}

TestDish.c

#include <stdio.h>
#include "czmq.h"


int main(int argc, char **argv) {

	int zmq_major, zmq_minor, zmq_patch;
	zmq_version (&zmq_major, &zmq_minor, &zmq_patch);
	fprintf (stdout, "Current ØMQ version is %d.%d.%d\n", zmq_major, zmq_minor, zmq_patch);

	int rc = 0;

	zsock_t *dish = zsock_new_dish ("tcp://localhost:5555");
	if (!dish) {
		fprintf(stderr,"create dish failes : %s \n", zmq_strerror (errno));
	}
	assert (dish);

	// Join to group
	rc = zsock_join (dish, "World");
	assert (rc == 0);

	while (!zsys_interrupted) {
		zframe_t *message = zframe_recv (dish);
		assert (message);
		char *s = zframe_strdup (message);
		fprintf(stdout,"received :'%s'\n",s);
		free(s);
		zframe_destroy(&message);
	}
	zsock_destroy(&dish);

	return EXIT_SUCCESS;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment