c - Linux 上 C 中的 UDP 发送方和 Windows 上 Qt 中的接收方不工作?

标签 c windows linux qt

<分区>

Qt broadcastreceiver 不能在 Linux 上使用 sender。发件人是用c写的。

我已经在 Windows 上用 Qt broadcastsender 测试了 Qt broadcastreceiver,这没问题,而且 Qt broadcastsender 在 C 语言的 linux 接收器上也能正常工作。

在 windows 上从 linux 接收数据时可能出现什么问题。

这是相同的代码。

发件人(Qt/windows): main.cpp

#include <QApplication>
#include "receiver.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Receiver receiver;
    receiver.show();
    return app.exec();
}

接收器.h

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QtGui>
#include <QtNetwork/QUdpSocket>
#include <QWidget>

class Receiver : public QWidget
{
    Q_OBJECT

public:
    Receiver(QWidget *parent = 0);

private slots:
    void processPendingDatagrams();

private:
    QLabel *statusLabel;
    QPushButton *quitButton;
    QUdpSocket *udpSocket;
};

#endif

接收器.cpp

#include <QtGui>
#include <QtNetwork>

#include "receiver.h"

#define PORT 45454

Receiver::Receiver(QWidget *parent): QWidget(parent)
{
    statusLabel = new QLabel(tr("Listening for broadcasted messages"));
    statusLabel->setWordWrap(true);

    quitButton = new QPushButton(tr("&Quit"));

    udpSocket = new QUdpSocket(this);
    bool b = udpSocket->bind(QHostAddress::Any,PORT, QUdpSocket::ShareAddress);

    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(quitButton);
    buttonLayout->addStretch(1);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(statusLabel);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);

    setWindowTitle(tr("Broadcast Receiver"));
}

void Receiver::processPendingDatagrams()
{
    while (udpSocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(), datagram.size());
        statusLabel->setText(statusLabel->text() + tr("Received datagram: \"%1\"").arg(datagram.data()));
    }
}

广播接收器

HEADERS       = receiver.h
SOURCES       = receiver.cpp \
                main.cpp
QT           += network

# install
target.path = $$[QT_INSTALL_EXAMPLES]/network/broadcastreceiver
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS broadcastreceiver.pro
sources.path = $$[QT_INSTALL_EXAMPLES]/network/broadcastreceiver
INSTALLS += target sources

symbian: {
    TARGET.CAPABILITY = NetworkServices
    include($$PWD/../../symbianpkgrules.pri)
}
maemo5: include($$PWD/../../maemo5pkgrules.pri)
contains(MEEGO_EDITION,harmattan): include($$PWD/../../harmattanpkgrules.pri)

sender.c (C/Linux)

#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>

#define BUFLEN 512
#define NPACK 10
#define PORT 45454

#define SRV_IP "xxx.xxx.xxx.xxx"

void diep(char *s)
{
    perror(s);
    exit(1);
}


int main(void)
{
    struct sockaddr_in si_other;
    int s, i, slen=sizeof(si_other);
    char buf[BUFLEN];

    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
    {
        diep("socket");
        printf("\n\tSocket error\n");
    }

    memset((char *) &si_other, 0, sizeof(si_other));
    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(PORT);
    if (inet_aton(SRV_IP, &si_other.sin_addr)==0)
    {
         fprintf(stderr, "inet_aton() failed\n");
        printf("\n\tInet_aton error\n");
        exit(1);
    }

    for (i=0;; i++)
    {
        printf("Sending packet %d\n", i);
        sprintf(buf, "This is packet %d\n", i);
        if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1)
     {
            diep("sendto()");
         printf("\n\tSendto error\n");
     }

    printf("Data written on (IP = %s and port : %d\n\n",inet_ntoa(si_other.sin_addr),ntohs(si_other.sin_port));
    sleep(1);
    }

    close(s);
    return 0;
}

问候, 古尔米特

最佳答案

我已经尝试了您的代码,几乎一切正常。 唯一的问题是,到达的包裹没有正确显示在您的 View 中。 第一个包裹到达后,它说:

Listening for broadcasted messagesReceived datagram: "This is packet

然后它停止了,因为数据包的内容是一个终止 QString 的 0 字节。 这也意味着不再显示所有后续消息。

我不知道这是否能解决您的问题,也许根本没有数据到达。 我使用的是 Linux,我无法向您保证任何适用于 Windows 的信息(尽管 Qt 在 Windows 上应该和在 Linux 上一样工作)但我可以向您保证您的代码可以在 Linux 上运行。

关于c - Linux 上 C 中的 UDP 发送方和 Windows 上 Qt 中的接收方不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11816062/

相关文章:

linux - Nginx : the rule( location =/) in server context do not work

linux - mpm-itk 安装 -- make : missing: Command not found on ubuntu

c - 使用哈希搜索来搜索链表

c - 指针只存内存地址吗?

c++ - my_string(char* s) 是什么意思?

c# - WPF .NET 应用程序崩溃、组合框和菜单事件未在装有 Windows 10 Creators Update、.NET 4.7 或 KB4034658 的平板电脑上触发

c++ - 奇怪地改变状态码

java - 小便器算法 - 一个简单的优化

c# - 确定 Windows 应用商店应用程序是否已从 Windows 服务中暂停或逻辑删除

php - linux 上的 blueimp jquery 上传删除不起作用