-1

Im trying to compare the data in the received datagram to a string, when i run the program i see that i receive "test", but the if statement doesnt work.

#include <QUdpSocket>
#include <QTextStream>
#include <string>
#include <QString>
#include <iostream>
int main()
{
    QTextStream qout(stdout);

    QUdpSocket *udpSocket = new QUdpSocket(0);
    udpSocket->bind(3838, QUdpSocket::ShareAddress);

    while (udpSocket->waitForReadyRead(-1)) {
        while(udpSocket->hasPendingDatagrams()) {
            QByteArray datagram;
            datagram.resize(udpSocket->pendingDatagramSize());
            QHostAddress sender;
            quint16 senderPort;

            udpSocket->readDatagram(datagram.data(), datagram.size(),
                                    &sender, &senderPort);
            qout << "received from " << sender.toString() << datagram.data() << endl;

            using namespace std;
            string jag = datagram.data();

            std::string str1 (jag.c_str());
            std::string str2 ("test.");
            printf("%s", jag.c_str());

           if (str1.compare(str2) == 0)
            {

                    printf("test ok");

            }
        }
    }
}

i have tried with different comparing methods, but nothing worked so far.

printf("%s", jag.c_str()); displays also test when i send test with netcat any ideas? thanks :)

p3tter
  • 15
  • 1
  • 7

3 Answers3

1

str2 is not "test" but "test.".

std::string str2 ("test.");

"test" is not equal to "test." so comparison fails.

4pie0
  • 29,204
  • 9
  • 82
  • 118
0

What does the value of the datagram? Why dont you use QString?

const QString srt1(datagram);
// Print what did you load
qDebug() << datagram << srt1;
const QString srt2("test.");
if (QString::compare(srt1, srt2, Qt::CaseSensitive) {
    qDebug() << ("test ok");
}
mohabouje
  • 3,867
  • 2
  • 14
  • 28
0

Better use an async approach, avoid doing such stuff inside of while(s).

The following code is working correctly in a real application.

Class Header

#include <QObject>
#include <QUdpSocket>

const int     UDP_SOCKET_PORT  = 45454;
const QString UDP_HELLO_STR    = "hello!";

class YourClass: public QObject
{
    Q_OBJECT;
public:
    YourClass();

private slots:
    void processDatagram();

private:
    QUdpSocket* udpSocket;
}

Class Source

YourCLass::YourClass():udpSocket(new QUdpSocket(this))
{
    udpSocket->bind(UDP_SOCKET_PORT, QUdpSocket::ShareAddress);
    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processDatagram()));
}

void YourCLass::processDatagram()
{
    QByteArray l_data;
    while(udpSocket->hasPendingDatagrams())
    {
        l_data.resize(udpSocket->pendingDatagramSize());

        QHostAddress l_addr;
        udpSocket->readDatagram(l_data.data(), l_data.size(), &l_addr);
        if(UDP_HELLO_STR == l_data.data())
        {
            QString l_srv_addr = l_addr.toString();

            if(!l_srv_addr.isEmpty())
            {
                qDebug() << "Received: " << l_data.data() << "from address: " << l_addr.toString();
            }
            else
            {
                qWarning() << "Datagram != " << UDP_HELLO_STR << ": " << l_data.data();
            }
        }
    }
}
JuanDeLosMuertos
  • 4,532
  • 15
  • 55
  • 87