The lifecycle of the socket of zeromq is as follows:
Create and destroy sockets: zmq_socket (), zmq_close ()
Configure and read socket options: zmq_setsockopt (), zmq_getsockopt ()
Establish a connection for the socket: zmq_bind (), zmq_connect ()
Send and receive messages: zmq_send (), zmq_recv ()
The socket of zeromq is always a null pointer type, and the message is a struct type.
Zmq is asynchronous and highly scalable.
Zmq connections are different from traditional TCP connections, mainly including:
Multiple protocols are used: inproc (in-process), IPC (in-process), TCP, PGM (broadcast), and epgm;
When the client uses zmq_connect (), the connection has been established, and a service of this endpoint is not required to be bound using zmq_bind;
The connection is asynchronous and buffered by a group of message queues;
The connection will display a certain message mode, which is determined by the socket type of the connection to be created;
A socket can have multiple input and output connections;
Zmq does not provide functions similar to zmq_accept (), because it automatically starts to accept connections when the socket is bound to an endpoint;
ApplicationProgramYou cannot directly deal with these connections because they are encapsulated at the bottom layer of zmq.
The difference between TCP socket and zmq socket in data transmission:
Zmq socket transmits messages instead of bytes (TCP) or frames (UDP ). A message refers to a piece of binary data block with a specified length. We will talk about the message below. This design is designed for performance optimization, so it may be hard to understand.
Zmq socket performs I/O operations in the background, that is to say, whether it is receiving or sending messages, it will first be transmitted to a local buffer queue, the size of this memory queue can be configured.
Zmq sockets can be connected to multiple Sockets (if the socket type is allowed ). The TCP protocol can only perform point-to-point connections, while zmq can perform one-to-many (similar to wireless broadcast), many-to-many (similar to post office), and many-to-one (similar to mailbox ), of course, this includes one-to-one scenarios.
Zmq socket can send messages to multiple endpoints (fan-out model) or receive messages from multiple endpoints (fan-in Model)