[zeromq-dev] Binding to TCP port 0
Ian Barber
ian.barber at gmail.com
Wed Feb 8 23:18:36 CET 2012
On Thu, Jan 26, 2012 at 2:57 PM, Martin Lucina <martin at lucina.net> wrote:
>
> zmq_bind(foo, "tcp://XXXX:*"); // "tcp://*:*" if you want INADDR_ANY
> char endpoint [ZMQ_ENDPOINT_MAX];
> zmq_getsockopt(foo, ZMQ_GET_ENDPOINT, endpoint, sizeof endpoint);
>
>
Hi all,
I have knocked up a relatively simple patch to implement this on IPC and
TCP transports, would appreciate comments as I'm pretty sure there's some
dodgy stuff in there! I've made a pull req, though mainly for comment
(please don't pull it chuck/mikko/pieter!):
https://github.com/zeromq/libzmq/pull/238
The TCP is handled by binding to port 0 then using getsockname to retrieve
the details in tcp_listener. This has an added get_address function which
is called from socket_base to populate a field in options. That field is
ZMQ_LAST_ENDPOINT just to make very explicit that it will get overwritten
with subsequent socket calls. The IPC uses tempnam, and otherwise the same
mechanism. At the moment is only looks for the wildcards on bind, connect
looked a bit trickier, but I'm sure it would just be finding the right
place. Inproc should be fairly straightforward, but I don't think there's
as much of a use case there.
#include "zmq.h"
#include <stdio.h>
int main (void)
{
size_t len = 255;
char endpoint[len];
void *context = zmq_init(1);
void *sock = zmq_socket(context, ZMQ_REQ);
// Test 1: test with specified host portion
zmq_bind(sock, "tcp://127.0.0.1:*");
zmq_getsockopt(sock, ZMQ_LAST_ENDPOINT, &endpoint, &len);
printf("%d - %s\n", (int)len, endpoint);
// Test 2: Test with IPC
len = 255; // reset the length
zmq_bind(sock, "ipc://*");
zmq_getsockopt(sock, ZMQ_LAST_ENDPOINT, &endpoint, &len);
printf("%d - %s\n", (int)len, endpoint);
zmq_close(sock);
zmq_term(context);
return 0;
}
Ian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20120208/a06addc2/attachment.htm>
More information about the zeromq-dev
mailing list