[zeromq-dev] inproc: need to bind to an address before connect?

Chuck Remes lists at chuckremes.com
Fri Oct 19 00:45:29 CEST 2012


No, it is not guaranteed that socket3 will be ready for a connection. All binds (and connects) happen asynchronously in the I/O thread. Depending on your machine performance, it could be ready in 10ms, 100ms or more.

What I recommend doing is putting a loop around your socket2.connect call. If it fails, sleep shortly and try again (or busy loop). It will eventually connect.

For example:

while (1) {
  rc = socket2.connect("inproc://addr2");
  if (rc == 0) break;
}

cr


On Oct 18, 2012, at 12:29 PM, Kah-Chan Low wrote:

> Thanks Chuck and Lourens for your very speedy reply!
> 
> I have two threads and to overcome this problem I have set-up the code like this:
> Thread A is guaranteed to execute the relevant parts of the code first even before Thread B gets started.
> 
> Thread A
> ----------
> .
> .
> socket1.bind("inproc://addr1");
> socket1.blockingrecv();
> socket2.connect("inproc://addr2");
> .
> .
> 
> Thread B
> ----------
> .
> .
> .
> socket3.bind("inproc://addr2");
> socket4.connect("inproc://addr1");
> socket.send(msg);
> .
> .
> .
> 
> Is this guaranteed to work?
> My question is this: after socket3.bind() is done and right before socket4.connect() is executed, is socket3 guaranteed to be ready for connection?
> 
> Thanks!
> KC
> 
> From: Chuck Remes <lists at chuckremes.com>
> To: Kah-Chan Low <kahchanlow at yahoo.com>; ZeroMQ development list <zeromq-dev at lists.zeromq.org> 
> Sent: Thursday, October 18, 2012 12:57 PM
> Subject: Re: [zeromq-dev] inproc: need to bind to an address before connect?
> 
> It's a long-standing bug that is very difficult to fix. The work around is to do as you discovered which is *always bind first* and then connect when using inproc transport.
> 
> For details as to why this is hard, try searching the mailing list archive. The answer would have been given by Martin Sustrik. You can also check the bug database for a write-up on this.
> 
> cr
> 
> On Oct 18, 2012, at 11:55 AM, Kah-Chan Low wrote:
> 
>> Hi,
>> When I do this I will get a "connection refused" error:
>> 
>> socket1.connect("inproc://abc");
>> socket2.bind("inproc://abc");
>> 
>> When I reverse the order of the statements everything works fine.
>> Both sockets have been created with the same ZMQ context.
>> 
>> Is this a feature or a bug?
>> 
>> Thanks!
>> KC
>> 
>> 
>> _______________________________________________
>> zeromq-dev mailing list
>> zeromq-dev at lists.zeromq.org
>> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20121018/a6cc31d0/attachment.htm>


More information about the zeromq-dev mailing list