Qt broadcastreceiver无法在Linux上使用发件人。发件人写在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();
}
receiver.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
receiver.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()));
}
}
broadcastreceiver.pro
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;
}
此致 Gurmeet
答案 0 :(得分:1)
我已经尝试过你的代码,几乎所有东西都运行正常。 唯一的问题是,到达的包在您的视图中无法正确显示。 第一个包裹到货后,它说:
Listening for broadcasted messagesReceived datagram: "This is packet
然后停止,因为数据包的内容是0字节,终止QString。 这也意味着不再显示以下所有消息。
我不知道这是否能解决您的问题,也许根本没有数据到来。 我正在使用Linux,我无法向你保证Windows的任何东西(虽然Qt在Windows上也可以在Linux上运行)但我可以向你保证你的代码在Linux上工作。