Skip to content

Instantly share code, notes, and snippets.

@sphaero
Created December 16, 2020 15:01
Show Gist options
  • Save sphaero/84143b5ac740fdfa0c19f4e326b41369 to your computer and use it in GitHub Desktop.
Save sphaero/84143b5ac740fdfa0c19f4e326b41369 to your computer and use it in GitHub Desktop.
zmq dgram test
// compile: gcc test-poll.c `pkg-config --cflags --libs libczmq`
#include "czmq.h"
// poll receive
int main()
{
zsock_t* mdgramr = zsock_new_dgram ("udp://239.255.42.99:1151");
assert (mdgramr);
char *mdmessage, *maddr;
zpoller_t *p = zpoller_new(mdgramr);
void *which = zpoller_wait(p, 1000);
assert(which == mdgramr);
zmsg_t *mdmsg = zmsg_recv( mdgramr );
assert (mdmsg);
maddr = zmsg_popstr (mdmsg);
mdmessage = zmsg_popstr (mdmsg);
assert (streq(mdmessage, "HELLO"));
zmsg_destroy ( &mdmsg );
zsock_destroy (&mdgramr);
zstr_free (&mdmessage);
zstr_free (&maddr);
zstr_free (&mdmessage);
}
// compile: gcc test-poll.c `pkg-config --cflags --libs libczmq`
#include "czmq.h"
// receive
int main()
{
zsock_t* mdgramr = zsock_new_dgram ("udp://239.255.42.99:1151");
assert (mdgramr);
char *mdmessage, *maddr;
zmsg_t *mdmsg = zmsg_recv( mdgramr );
assert (mdmsg);
maddr = zmsg_popstr (mdmsg);
mdmessage = zmsg_popstr (mdmsg);
assert (streq(mdmessage, "HELLO"));
zmsg_destroy ( &mdmsg );
zsock_destroy (&mdgramr);
zstr_free (&mdmessage);
zstr_free (&maddr);
zstr_free (&mdmessage);
}
// compile: gcc test-poll.c `pkg-config --cflags --libs libczmq`
#include "czmq.h"
// send
int main()
{
zsock_t* mdgrams = zsock_new_dgram ("udp://*:*");
assert (mdgrams);
int rc = zstr_sendm( mdgrams, "239.255.42.99:1151" );
assert (rc == 0);
rc = zstr_send (mdgrams, "HELLO");
assert (rc == 0);
zsock_destroy (&mdgrams);
}
// compile: gcc test-poll.c `pkg-config --cflags --libs libczmq`
#include "czmq.h"
// send and receive
int main()
{
zsock_t* mdgramr = zsock_new_dgram ("udp://225.25.25.25:7777");
assert (mdgramr);
zsock_t* mdgrams = zsock_new_dgram ("udp://*:*");
assert (mdgrams);
int rc = zstr_sendm( mdgrams, "225.25.25.25:7777" );
assert (rc == 0);
rc = zstr_send (mdgrams, "HELLO");
assert (rc == 0);
char *mdmessage, *maddr;
zmsg_t *mdmsg = zmsg_recv( mdgramr );
assert (mdmsg);
maddr = zmsg_popstr (mdmsg);
mdmessage = zmsg_popstr (mdmsg);
assert (streq(mdmessage, "HELLO"));
zmsg_destroy ( &mdmsg );
zsock_destroy (&mdgrams);
zsock_destroy (&mdgramr);
zstr_free (&mdmessage);
zstr_free (&maddr);
zstr_free (&mdmessage);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment