My problem was with udp sockets , I have two machines connected with a cable , the problem is that I always got this error 9 from the socket and I can’t understand why?.
this my sender :
#include <QtWidgets>
#include <QtNetwork>
#include "sender.h"
Sender::Sender(QWidget *parent)
: QWidget(parent)
{
statusLabel = new QLabel(tr("Ready to send datagrams on port 45454"));
statusLabel->setWordWrap(true);
startButton = new QPushButton(tr("&Start"));
quitButton = new QPushButton(tr("&Quit"));
buttonBox = new QDialogButtonBox;
buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
adress=QHostAddress("192.168.20.230");
//udpSocket->connectToHost(adress,88);
timer = new QTimer(this);
//! [0]
udpSocket = new QUdpSocket(this);
//! [0]
messageNo = 1;
connect(startButton, SIGNAL(clicked()), this, SLOT(startBroadcasting()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
connect(timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
setWindowTitle(tr("Broadcast Sender"));
}
void Sender::startBroadcasting()
{
startButton->setEnabled(false);
timer->start(3000);
}
void Sender::broadcastDatagram()
{
//! [1]
QByteArray datagram = "RB00010040A94C";
statusLabel->setText(tr("Now sending datagram: \"%1\" Numero: %2").arg(datagram.data()).arg(messageNo));
if(udpSocket->writeDatagram(datagram.data(), datagram.size(),adress,88)==-1)
{
statusLabel->setText("can't send to server");
}
//! [1]
++messageNo;
}
and my receiver:
#include <QtWidgets>
#include <QtNetwork>
#include <QMessageBox>
#include "receiver.h"
Receiver::Receiver(QWidget *parent)
: QWidget(parent)
{
statusLabel = new QLabel(tr("Listening for messages"));
statusLabel->setWordWrap(true);
quitButton = new QPushButton(tr("&Quit"));
//! [0]
udpSocket = new QUdpSocket(this);
connect(udpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(ShowError(QAbstractSocket::SocketError)));
connect(udpSocket,SIGNAL(hostFound()),SLOT(ShowHostFound()));
connect(udpSocket,SIGNAL(stateChanged(QAbstractSocket::SocketState)),SLOT(showState(QAbstractSocket::SocketState)));
//adresse et port
QHostAddress adress=QHostAddress("192.168.20.230");
if (!udpSocket->bind(adress,88))
{
statusLabel->setText("Impossible de créer le socket en écoute");
}
//! [0]
//! [1]udpS
connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(processPendingDatagrams()),Qt::DirectConnection);
//! [1]
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(" Receiver"));
}
void Receiver::processPendingDatagrams()
{
//! [2]
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size(),&senderAddress,&senderPort);
statusLabel->setText(tr("Received datagram: \"%1\" from: \"%2\"")
.arg(datagram.data()).arg(senderAddress.toString()));
}
//! [2]
}
void Receiver::ShowError(QAbstractSocket::SocketError erreur)
{
QMessageBox::critical(this, tr("Socket Error"), tr(QString::number(erreur).toStdString().c_str()));
}
void Receiver::ShowHostFound()
{
statusLabel->setText("host found !");
}
void Receiver::showState(QAbstractSocket::SocketState state)
{
statusLabel->setText("Socket State"+QString::number(state));
}
I guess that it’s a problems with firewall or something like that because bind is never accepted ,but I’m not sure .
↧