Saad Saadi
Saad Saadi

Reputation: 1061

CZMQ: Getting started on Raspberry-Pi

I made one simple client-server application on desktop PC and ran it successfully while making client and server on same PC.

Then i cross compiled the app to run it on raspberry. When i am running this app on raspberry, both client and server on raspberry, it is working perfectly. I can see the sent and received messages.

Now i made PC as server and raspberry as client but i can't see the received messages. Here is my code.

PC Side Code:

zctx_t *ctx = zctx_new ();
void *reader = zsocket_new (ctx, ZMQ_PULL);
int rc = zsocket_connect (reader, "tcp://PC-IP:5555");
printf("wait for a message...\n");
while(1)
    {
        char *message = zstr_recv (reader);
        Sleep(10);
        printf("Message: %s",message);
    }
zctx_destroy (&ctx);

Raspberry Side Code:

zctx_t *ctx = zctx_new ();
void *writer = zsocket_new (ctx, ZMQ_PUSH);
int rc = zsocket_bind (writer, "tcp://PC-IP:5555");
while(1)
    {
        cout<<"sending................."<<endl;
        zstr_send (writer, "HELLO");
    }
zsocket_destroy (ctx, writer);

How can i make it work?

Upvotes: 0

Views: 249

Answers (1)

Elijan9
Elijan9

Reputation: 1294

A server should always bind to its own interface (either its local IP address or 0.0.0.0 for all IPv4 interfaces or 0::0 for both IPv4 and IPv6).

A client should always connect to the remote IP address.

Since you want your PC to be a server pulling messages from the Raspberry Pi client, I think you should use the following:

PC Side Code

zctx_t *ctx = zctx_new ();
void *reader = zsocket_new (ctx, ZMQ_PULL);
int rc = zsocket_bind (reader, "tcp://PC-IP:5555");
printf("wait for a message...\n");
while(1)
    {
        char *message = zstr_recv (reader);
        Sleep(10);
        printf("Message: %s",message);
    }
zctx_destroy (&ctx);

Raspberry Side Code

zctx_t *ctx = zctx_new ();
void *writer = zsocket_new (ctx, ZMQ_PUSH);
int rc = zsocket_connect (writer, "tcp://PC-IP:5555");
while(1)
    {
        cout<<"sending................."<<endl;
        zstr_send (writer, "HELLO");
    }
zsocket_destroy (ctx, writer);

Of course you could also run the Raspberry Pi as pushing server, and the PC side as pulling client. In that case you could use:

...
int rc = zsocket_connect (reader, "tcp://raspberrypi-ip:5555");
...

and:

...
int rc = zsocket_bind (writer, "tcp://raspberrypi-ip:5555");
...

Upvotes: 1

Related Questions