Regarding creating zeromq message queues, setting and changing IP addresses, remote access is possible, not just local links. python code.

About zeromq creation, binding local, and binding other client methods.

There are a lot of introductions about zmq communication modes on the Internet, including three types, I will not describe them in detail.

But the demos they gave were created locally as a server server and also as a client client. Both set the port to 5555. In this way, the local is connected to the local, and the local is controlled locally.

Local as a client:

As a client, send information.

import zmq

context = zmq.Context()
# Socket to talk to server
print("Connecting to hello world server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
socket.send_string(send_msg)

The local also serves as the server:

As the server side, accept information.

import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
message = socket.recv_string()

Note: REQ-REP mode is blocking, which means that the client must first send a message to the server, and then the server can return a response to the client. Any sequence error will result in an error.

When our local machine is the server, it receives the message queue sent from other computers and clients. At this point, you only need to use the local ip address as the address of the tcp protocol, the local server binds this address and port, and the remote client also links this ip address and window. Below is the corresponding python code.

PS: The command line for Windows to query the IP address is: ipconfig

The command line for Mac or Linux to query the IP address is: ifconfig

We found that the IP address of the computer serving as the server is: 192.168.3.41. Therefore, changing the tcp setting to the following can achieve the purpose of sending information remotely and receiving information locally. The port port can be set arbitrarily, 5000 or 5555, either.

Remote other computers as clients:

The client is connected to the ip address of the server, so connect is used.

import zmq

context = zmq.Context()
# Socket to talk to server
print("Connecting to hello world server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://192.168.3.41:5000")

socket.send_string(send_msg)

Local as the server:

The server receives information from this ip address and port 5000, so it is bound to it.

import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://192.168.3.41:5000")
message = socket.recv_string()

 

Guess you like

Origin blog.csdn.net/qq_32998593/article/details/114264363