3

I am trying to implement an app/thread that listens to an ipaddr:port on which another app/thread is already listening. I know I need to update both the apps to set SO_REUSEADDR in setsockopt(...) before bind() to avoid "Address Already in use" error when the 2nd app tries to bind().

The problem is that these apps(libs) are in an existing system that uses ZeroMQ on it's own. I cannot use linux socket lib directly. I have to use ZeroMQ sockets only.

Apparently zmq::setsockopt() does not understand SO_REUSEADDR as an option as its not defined in the zmq.h header. At least there is no ZMQ_SO_REUSEADDR

Or may be I am not using the right option.

Can someone help me with this issue. Does ZMQ socket support SO_REUSEADDR or not support at all, in which case how to go about this issue?

user3666197
  • 1
  • 6
  • 50
  • 92

1 Answers1

4

There's a couple things going on here.

  1. SO_REUSEADDR is not going to help you. [see here].

SO_REUSEADDR will not allow you to share the same socket signature between two applications. So, if you are bound to a specific address:port in your first application and still using that address and port in that application, then you will not be able to bind to it in your second application. SO_REUSEADDR allows you to grab an address:port signature for a new application once an old application has given it up but it's still lingering, waiting for the buffer to clear.

  1. SO_REUSEPORT is what you're looking for [see same link as above].

SO_REUSEPORT is intended to allow multiple applications to share the same address signature. This seems to be what you're looking for.

  1. SO_REUSEPORT is not supported in ZMQ [see here].

You cannot bind to the same address:port in multiple applications on the same host in ZMQ. It's not supported as of June of last year.

... if you wish to just handle the case where another service is spinning down and a new service wishes to bind on the same address:port signature, you'll have to set ZMQ_LINGER to 0 on the original connection so that it won't hold the socket to clear the buffer.

Community
  • 1
  • 1
Jason
  • 13,606
  • 2
  • 29
  • 40