[zeromq-dev] Address in use error when re-binding socket
Pieter Hintjens
ph at imatix.com
Sat Apr 5 15:37:33 CEST 2014
It's not reusing the closed socket, so much as trying to bind to an
endpoint that's not (yet) free. Depending on the case (especially if
you have clients still connected), or if you are trying very rapidly
(as in this case), TCP has not yet freed the endpoint.
In this case I suspect it's because zmq_close is asynchronous and you
aren't giving the system any time to process that. Hence looping will
eventually "fix" the issue.
On Sat, Apr 5, 2014 at 3:19 PM, Matthias Kluwe <mkluwe at gmail.com> wrote:
> Hi!
>
> I noticed some unexpected behaviour when trying to re-use a closed
> socket in my server. It boils down to this small example:
>
> #include <cassert>
> #include <iostream>
>
> #include "zmq.h"
>
> int main() {
> void *ctx = zmq_ctx_new();
> assert( ctx );
> void *skt;
>
> skt = zmq_socket( ctx, ZMQ_REP );
> assert( skt );
> assert( zmq_bind( skt, "tcp://*:5555" ) == 0 );
> assert( zmq_close( skt ) == 0 );
>
> skt = zmq_socket( ctx, ZMQ_REP );
> assert( skt );
> int fail = 0;
> while ( zmq_bind( skt, "tcp://*:5555" ) ) { ++fail; }
> std::cout << fail << std::endl;
> }
>
> I'm using ZeroMQ 4.0.3 on Windows XP SP3, compiler is VS 2008.
> libzmq.dll has been built with the provided Visual Studio solution.
>
> This prints 1 here when doing a "Debug" build (both of the code above
> and of libzmq.dll) and 0 using a "Release" build. Strange enough, when
> running the code above with mixed build configuration (Debug with
> Release lib), fail counts up to 6.
>
> I've posted this on SO
> (http://stackoverflow.com/questions/22656225/zeromq-address-in-use-error-when-re-binding-socket)
> and it was suggested that the problem might be that the TCP socket
> entered the TIME-WAIT state. Is that correct?
>
> Regards,
> Matthias
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev
More information about the zeromq-dev
mailing list