【发布时间】:2021-11-06 11:24:54
【问题描述】:
我正在学习ZMQ。我创建了两个使用拉/推模式相互通信的简单应用程序。我在我的 Ubuntu 上创建了两个点击界面。其中一个的 IP 地址为 1.1.1.1,第二个水龙头的 IP 地址为 1.1.1.2。这些应用程序中的每一个都发送一个字符串并写入接收到的字符串stdout。一切都按我的意愿工作,但是当我运行tcpdump 时,我看到每个数据包的源地址和目标地址都是相同的。 tcpdump 还显示 ZMQ 使用我在应用程序中设置的不同端口。
这是我的应用程序 1 的代码:
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <thread>
using namespace std;
using namespace zmq;
int main()
{
context_t zmq_context = context_t();
socket_t zmq_send_socket = socket_t(zmq_context, ZMQ_PUSH);
socket_t zmq_recv_socket = socket_t(zmq_context, ZMQ_PULL);
string push_socat = "tcp://1.1.1.2:5001";
string pull_socat = "tcp://1.1.1.1:5000";
zmq_send_socket.bind(push_socat.c_str());
zmq_recv_socket.connect(pull_socat.c_str());
while(1)
{
string msg = "app 1 sends";
message_t received_message;
if(msg.size())
{
message_t send_message(msg);
zmq_send_socket.send(send_message, send_flags::dontwait);
}
auto result = zmq_recv_socket.recv(received_message, recv_flags::dontwait);
if (result.has_value())
{
cout<<received_message<<endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
}
}
这是我的应用 2 代码:
#include <zmq.hpp>
#include <string>
#include <iostream>
#include <thread>
using namespace std;
using namespace zmq;
int main()
{
context_t zmq_context = context_t();
socket_t zmq_send_socket = socket_t(zmq_context, ZMQ_PUSH);
socket_t zmq_recv_socket = socket_t(zmq_context, ZMQ_PULL);
string push_socat = "tcp://1.1.1.1:5000";
string pull_socat = "tcp://1.1.1.2:5001";
zmq_send_socket.bind(push_socat.c_str());
zmq_recv_socket.connect(pull_socat.c_str());
while(1)
{
string msg = "app 2 sends";
message_t received_message;
if(msg.size())
{
message_t send_message(msg);
zmq_send_socket.send(send_message, send_flags::dontwait);
}
auto result = zmq_recv_socket.recv(received_message, recv_flags::dontwait);
if (result.has_value())
{
cout<<received_message<<endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
}
}
应用 1 打印“应用 2 发送”,应用 2 打印“应用 1 发送”。
tcpdump 的示例输出:
22:01:51.724290 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 77: 10.100.100.1.5000 > 10.100.100.1.45624: Flags [P.], seq 649:660, ack 1, win 512, options [nop,nop,TS val 62388355 ecr 62385355], length 11
22:01:51.724325 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 66: 10.100.100.1.45624 > 10.100.100.1.5000: Flags [.], ack 660, win 512, options [nop,nop,TS val 62388355 ecr 62388355], length 0
22:01:51.967303 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 79: 10.100.100.2.5001 > 10.100.100.2.41982: Flags [P.], seq 767:780, ack 1, win 512, options [nop,nop,TS val 2441785963 ecr 2441782963], length 13
22:01:51.967335 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 66: 10.100.100.2.41982 > 10.100.100.2.5001: Flags [.], ack 780, win 512, options [nop,nop,TS val 2441785963 ecr 2441785963], length 0
22:01:54.724516 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 77: 10.100.100.1.5000 > 10.100.100.1.45624: Flags [P.], seq 660:671, ack 1, win 512, options [nop,nop,TS val 62391355 ecr 62388355], length 11
22:01:54.724551 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 66: 10.100.100.1.45624 > 10.100.100.1.5000: Flags [.], ack 671, win 512, options [nop,nop,TS val 62391355 ecr 62391355], length 0
22:01:54.967509 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 79: 10.100.100.2.5001 > 10.100.100.2.41982: Flags [P.], seq 780:793, ack 1, win 512, options [nop,nop,TS val 2441788963 ecr 2441785963], length 13
22:01:54.967542 00:00:00:00:00:00 > 00:00:00:00:00:00, ethertype IPv4 (0x0800), length 66: 10.100.100.2.41982 > 10.100.100.2.5001: Flags [.], ack 793, win 512, options [nop,nop,TS val 2441788963 ecr 2441788963], length 0
您知道为什么源地址和目标地址相同,为什么我会看到奇怪的端口吗?
【问题讨论】: