Created
April 22, 2013 11:05
-
-
Save hintjens/5433953 to your computer and use it in GitHub Desktop.
Decentralized chat app in C (40 lines)
This file contains 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 <czmq.h> | |
static void | |
listener_thread (void *args, zctx_t *ctx, void *pipe) | |
{ | |
void *listener = zsocket_new (ctx, ZMQ_SUB); | |
zsocket_set_subscribe (listener, ""); | |
int address; | |
for (address = 1; address < 255; address++) | |
zsocket_connect (listener, "tcp://192.168.69.%d:9000", address); | |
while (true) { | |
char *message = zstr_recv (listener); | |
if (!message) | |
break; | |
puts (message); | |
free (message); | |
} | |
} | |
int main (void) | |
{ | |
zctx_t *ctx = zctx_new (); | |
zthread_fork (ctx, listener_thread, NULL); | |
void *broadcaster = zsocket_new (ctx, ZMQ_PUB); | |
zsocket_bind (broadcaster, "tcp://*:9000"); | |
while (true) { | |
char message [1024]; | |
if (!fgets (message, 1024, stdin)) | |
break; | |
message [strlen (message) - 1] = 0; | |
zstr_send (broadcaster, "hintjens: %s", message); | |
} | |
zctx_destroy (&ctx); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment