變化
void context = (void)zmq_ctx_new();
到
void *context = (void *)zmq_ctx_new();
而且,我想你的代碼,無效的情況下=(無效)zmq_ctx_new()會導致編譯錯誤。
這是我的代碼,可以在我的OSX上工作。
gcc -o cli client.c -lzmq
gcc -o srv server.c -lzmq
client.c:
#include <zmq.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
int main (void)
{
printf ("Connecting to hello world server…\n");
void *context = (void *)zmq_ctx_new();
void *requester = zmq_socket (context, ZMQ_REQ);
zmq_connect (requester, "tcp://localhost:5555");
int request_nbr;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
char buffer [10];
char snd[] = "hello";
printf ("Sending Hello %d…\n", request_nbr);
zmq_send (requester, snd, sizeof(snd), 0);
zmq_recv (requester, buffer, 10, 0);
printf ("Received World %d\n", request_nbr);
}
zmq_close (requester);
zmq_ctx_destroy (context);
return 0;
}
server.c:
#include <zmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
int main (void)
{
// Socket to talk to clients
void *context = zmq_ctx_new();
void *responder = zmq_socket (context, ZMQ_REP);
int rc = zmq_bind (responder, "tcp://*:5555");
assert (rc == 0);
while (1) {
int nrecv;
char buffer [10];
nrecv = zmq_recv (responder, buffer, 10, 0);
printf ("[%d] Received %s\n", nrecv, buffer);
sleep (1); // Do some 'work'
zmq_send (responder, "World", 5, 0);
}
return 0;
}
我的第一個語句:無效的情況下=(無效)zmq_ctx_new();實際上,void * context =(void *)zmq_ctx_new();不知道爲什麼它沒有把*放在那裏。無論如何..只是想澄清它 – Kavita
你的調試器說segfault發生在哪裏? – Peter
爲什麼在第一個聲明中演員?在C裏投這裏是沒有必要的。如果編譯器抱怨沒有它,你可能會找到你的問題的原因。 – alk