c++ - 在 Wireshark 中看到的数据报,Qt UDP 套接字未收到

标签 c++ qt sockets networking udp

我正在编写一个通过 UDP 套接字与 FPGA 通信的 Qt (5.6) 应用程序。数据包以 2 KHz 的速率流式传输到 PC(所有数据包大小相同,1272 字节)。 Wireshark 显示正在发送数据包,并且 UDP header 符合预期。问题是,我正在使用的 Qt UDP 套接字从未收到这些数据包。 readyRead 信号永远不会被调用。

这是一个代码片段:

UdpConnection::UdpConnection(QObject* parent)
{
    fpgaConnection = QSharedPointer<QUdpSocket>(new QUdpSocket);

    qDebug() << connect(fpgaConnection.data(), &QUdpSocket::readyRead, this, &UdpConnection::readyRead);

    if (fpgaConnection->bind(QHostAddress("192.168.10.10"), 1920))
    {
        qDebug() << "Successfully Bound!";
    }
    else
    {
        qDebug() << "BINDING FAILURE";
    }

    fpgaConnection->connectToHost(QHostAddress("192.168.10.200"), 1919);

    sendArpRequest();
}

void UdpConnection::readyRead()
{
    while (fpgaConnection->hasPendingDatagrams())
    {
        QByteArray buffer;
        buffer.resize(fpgaConnection->pendingDatagramSize());

        QHostAddress sender;
        quint16 senderPort;

        fpgaConnection->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
        qDebug() << "Message from:" << sender;
        qDebug() << "Message port:" << senderPort;
        qDebug() << buffer;
    }
}
  • UdpConnection 没有运行在与主线程不同的线程上。应该吗?
  • 我绑定(bind)成功,并且我认为“connectToHost”正在工作,因为我能够向远程主机发送消息。
  • 该应用程序已添加到防火墙异常(exception)列表(再次,ARP 握手证明它们能够通信)。
  • 该接口(interface)是 FPGA 和 PC 之间的直接以太网连接。

为什么 Wireshark 可以看到这些消息,而我的程序却看不到?

更新#1 Wireshark 将 2KHz 数据包作为 LLC 数据包。以太网 header 显示正确的目标(我的 MAC 地址)、源地址(在 FPGA 中硬编码)和长度。 IP header 的源 IP 为 192.168.10.200,目标 IP 为 192.168.10.10,UDP header 的源端口为 1920,目标端口为 1919。

更新#2 Wireshark 日志:paste.ee/p/98c1H 如您所见,数据包以 2KHz 的频率从 FPGA 重复发送。 ARP传输和回复可以在第5、10和11个数据包中找到。

更新#3 传入数据包的 IP 数据包具有未设置为 0x0000 的正确校验和。

最佳答案

我不太熟悉 Qt,但对于 BSD 套接字 API,我通常使用 bind 来设置我的接收端口,但不使用 connect 进行 UDP 连接,尤其是在使用不同的端口时,例如 1919 和 1920 对于套接字 API,使用 sendto() 发送具有目标 IP/端口的数据包。

所以,尝试注释掉connectToHost,发送时直接使用writeDatagram

关于c++ - 在 Wireshark 中看到的数据报,Qt UDP 套接字未收到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41005455/

相关文章:

关于文件格式和od的C++问题

c++ - 基于系统架构的 C++ 中的类大小

c++ - 类似于 C# 中的模板化回调参数的逆变

c++ - 如何配置 Qt - DirectX - Xinput.h

c++ - Qt : Software using QOpenGLWidget crash on mainwindow. 显示()

c++ - 如何将参数传递给 C++ 应用程序

c++ - 从 QML 调用多个 C++ 函数且第一个函数包含连接调用时,QML 在触发回调之前继续

c - EPIPE block 服务器

c - TCP连接: server doesn't 'understand' that receiving is over

c++ - 检索 h_addr_list[0] C++ 时出现访问冲突