CZMQ:开始使用 Raspberry-Pi
CZMQ: Getting started on Raspberry-Pi
我在桌面 PC 上制作了一个简单的 client-server
应用程序,并且 运行 在同一台 PC 上制作客户端和服务器时它成功了。
然后我在 raspberry 上将应用程序交叉编译为 运行。当我在 raspberry 上 运行ning 这个应用程序时,在 raspberry 上的客户端和服务器上,它工作得很好。我可以看到发送和接收的消息。
现在我将 PC 作为服务器,将 raspberry 作为客户端,但是我看不到收到的消息。这是我的代码。
PC端代码:
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);
树莓派代码:
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);
我怎样才能让它发挥作用?
服务器应始终绑定到它自己的接口(它的本地IP地址或0.0.0.0
用于所有IPv4接口或0::0
用于IPv4和IPv6).
客户端应始终连接 到远程 IP 地址。
由于您希望您的 PC 成为从 Raspberry Pi 客户端 提取消息的服务器,我认为您应该使用以下内容:
PC端代码
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);
树莓派代码
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);
当然也可以运行Raspberry Pi推server,PC端拉client.在这种情况下,您可以使用:
...
int rc = zsocket_connect (reader, "tcp://raspberrypi-ip:5555");
...
和:
...
int rc = zsocket_bind (writer, "tcp://raspberrypi-ip:5555");
...
我在桌面 PC 上制作了一个简单的 client-server
应用程序,并且 运行 在同一台 PC 上制作客户端和服务器时它成功了。
然后我在 raspberry 上将应用程序交叉编译为 运行。当我在 raspberry 上 运行ning 这个应用程序时,在 raspberry 上的客户端和服务器上,它工作得很好。我可以看到发送和接收的消息。
现在我将 PC 作为服务器,将 raspberry 作为客户端,但是我看不到收到的消息。这是我的代码。
PC端代码:
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);
树莓派代码:
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);
我怎样才能让它发挥作用?
服务器应始终绑定到它自己的接口(它的本地IP地址或0.0.0.0
用于所有IPv4接口或0::0
用于IPv4和IPv6).
客户端应始终连接 到远程 IP 地址。
由于您希望您的 PC 成为从 Raspberry Pi 客户端 提取消息的服务器,我认为您应该使用以下内容:
PC端代码
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);
树莓派代码
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);
当然也可以运行Raspberry Pi推server,PC端拉client.在这种情况下,您可以使用:
...
int rc = zsocket_connect (reader, "tcp://raspberrypi-ip:5555");
...
和:
...
int rc = zsocket_bind (writer, "tcp://raspberrypi-ip:5555");
...