ComradeJoecool ComradeJoecool - 9 days ago 9
C# Question

Can I rebind a ZeroMQ socket to a different port without destroying the socket?

I have a reply ZSocket in a request-reply pattern.

Later in the process, I want to switch the binding, so the reply socket now responds to requests on a different port. The user enters the port that they want to listen on, if they don't want to listen on the default port 5555.

Can I do this by simply calling ZSocket.Bind() again, or do I have to destroy the socket object first.

If I have to destroy it first, then how can I be sure the ZSocket is ready to be rebound to something else now? I know that closing sockets is handled differently in C because it doesn't have garbage collecting. Since the C# language binding for ZeroMQ can use of garbage collecting, is it as simple as closing the socket and creating a new one on another port? Do I need to bother with setting a low Linger value or anything like that?

I couldn't find anything about this in the documentation.
Here is a related question ZeroMQ: Address in use error when re-binding socket

Here is the code I have

private ZContext context = new ZContext();
private ZSocket replier = new ZSocket(context, ZSocketType.REP);
replier.Bind("tcp://*:5555); //bind to default port
//yada yada...
//user enters new port to listen on 5556
replier.Bind("tcp://*:5556);


What is the proper way to do this? Please explain the logic behind the answer.

Answer

I'm not sure about C# bindings, but you can rebind zmq socket using plain C api and start receiving messages on new port right away. Linger period is unrelated to your case.