我有一个简单的PUB / SUB消息程序。我正在尝试将其绑定到tcp://*:5556
范围,以便在所有可用的端口上发布ips。当套接字绑定时,错误返回:
资源暂时无法使用。
我正在尝试首先启动发布商线程。港口是免费的。 绑定过程有什么问题?
出版商:
int main (void) {
void *context = zmq_ctx_new();
void *publisher = zmq_socket(context, ZMQ_PUB);
zmq_bind(publisher, "tcp://*:5556");
printf("Binding: %s\n", zmq_strerror(errno));
srandom((unsigned) time(NULL));
while(true) {
char update[20];
sprintf(update, "%05d %d %d", randof(10000), randof(215) - 80, randof(50) + 10);
sleep(1);
zmq_send(publisher, update, strlen(update), 0);
printf("Sending: %s", zmq_strerror(errno));
}
zmq_close(publisher);
zmq_ctx_destroy(context);
return 0;
}
订户:
int main () {
void *context = zmq_ctx_new();
void *subscriber = zmq_socket(context, ZMQ_SUB);
zmq_connect(subscriber, "tcp://localhost:5556");
printf("Connection: %s\n", zmq_strerror(errno));
while(true) {
char update[20];
zmq_recv(subscriber, update, 20, 0);
printf("Receiving: %s\n", zmq_strerror(errno));
printf("Message: %s\n", update);
}
zmq_close(subscriber);
zmq_ctx_destroy(context);
return 0;
}
答案 0 :(得分:0)
根据给出的信息,很可能其他东西已经绑定到该套接字。您的计算机上是否已运行程序实例?或者使用该端口号的另一个进程。
尝试用ipc替换tcp。对于编写这些示例程序,可以避免这样的问题。
另外:你的SUB套接字没有订阅任何内容,所以即使绑定工作,SUB也不会收到任何内容。