2016-06-20 9 views
0

私はこの記事の後ろに「http://hintjens.com/blog:49」と書いてあります。 "stonehouse"のテストコードは "PUSH/PULL"を使用し、 "PUB/SUB"を使用するように修正しようとしています。しかし、私の初期の素朴な試みは失敗します(プッシュ/プルのために働く以下のコード)。私が見逃したことはありますか?ところで、czmqは本当にPUB/SUBで動作しますが、これはセキュリティ関連の行を削除してもまだ動作しません。お手伝いありがとう。czmq: "PUB/SUB + security"のコード例を検索

#include <czmq.h> 

int main (int argc, char **argv) 
{ 
// Create context and start authentication engine 
zctx_t *ctx = zctx_new(); 
zauth_t *auth = zauth_new (ctx); 
zauth_set_verbose (auth, true); 
zauth_allow (auth, "127.0.0.1"); 

zauth_configure_curve (auth, "*", CURVE_ALLOW_ANY); 

if (argc == 1) 
{ zcert_t *server_cert = zcert_new(); 
    char *server_key = zcert_public_txt (server_cert); 
    printf ("%d: BEGIN '%s' END\n", strlen (server_key), server_key); 

    void *server = zsocket_new (ctx, ZMQ_PUB/*PUSH*/); 
    zcert_apply (server_cert, server); 
    zsocket_set_curve_server (server, 1); 
    zsocket_bind (server, "tcp://*:9000"); 
    printf ("Hit any key to start sending...\n"); 
    getchar(); 
    int i=5; 
    while (i > 0) 
    { printf ("%d ", i); fflush (stdout); 
     sleep (1); 
     --i; 
    } 
    zstr_send (server, "Hello"); 
    printf ("sent\n"); 
    zcert_destroy (&server_cert); 
} 
else 
{ zcert_t *client_cert = zcert_new(); 

    void *client = zsocket_new (ctx, ZMQ_SUB/*PULL*/); 
    zcert_apply (client_cert, client); 
    zsocket_set_curve_serverkey (client, argv[1]); 
    zsocket_connect (client, "tcp://127.0.0.1:9000"); 

    char *message = zstr_recv (client); 
    printf ("received: %s\n", message); 
    assert (streq (message, "Hello")); 
    free (message); 
    puts ("Stonehouse test OK"); 

    zcert_destroy (&client_cert); 
} 

zauth_destroy (&auth); 
zctx_destroy (&ctx); 
return 0; 
} 

答えて

0

最後に、加入者は「ZMQ_SUBSCRIBE」のオプションを設定する必要があります。それ以外の場合は、何も受信しません。私はこれまでとは逆に、間違っていると仮定します。