Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QT C++;将收到的数据报与字符串进行比较_C++_String_Qt_Compare_Datagram - Fatal编程技术网

C++ QT C++;将收到的数据报与字符串进行比较

C++ QT C++;将收到的数据报与字符串进行比较,c++,string,qt,compare,datagram,C++,String,Qt,Compare,Datagram,我试图将收到的数据报中的数据与字符串进行比较,当我运行程序时,我看到我收到了“test”,但if语句不起作用 #include <QUdpSocket> #include <QTextStream> #include <string> #include <QString> #include <iostream> int main() { QTextStream qout(stdout); QUdpSocket *udp

我试图将收到的数据报中的数据与字符串进行比较,当我运行程序时,我看到我收到了“test”,但if语句不起作用

#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");

            }
        }
    }
}
#包括
#包括
#包括
#包括
#包括
int main()
{
QTextStream-qout(stdout);
QUdpSocket*udpSocket=新的QUdpSocket(0);
udpSocket->bind(3838,QUdpSocket::ShareAddress);
while(udpSocket->waitForReadyRead(-1)){
而(udpSocket->hasPendingDatagrams()){
QByteArray数据报;
resize(udpSocket->pendingDatagramSize());
QHostAddress发送者;
发送端口;
udpSocket->readDatagram(datagram.data(),datagram.size(),
&发送方和发送方端口);
qout
str2
不是“测试”,而是“测试”


“test”不等于“test”。因此比较失败。

数据报的值是多少?为什么不使用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");
}
const QString srt1(数据报);
//打印你加载了什么

qDebug()最好使用异步方法,避免在while(s)内执行此类操作

以下代码在实际应用程序中正常工作

类标题
#包括
#包括
const int UDP_套接字_端口=45454;
const QString UDP_HELLO_STR=“HELLO!”;
类YourClass:公共QObject
{
Q_对象;
公众:
YourClass();
专用插槽:
void processDatagram();
私人:
QUdpSocket*udpSocket;
}
类源
YourCLass::YourCLass():udpSocket(新的qudsocket(this))
{
udpSocket->bind(UDP_套接字_端口,QUdpSocket::ShareAddress);
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(processDatagram());
}
作废YourCLass::processDatagram()
{
QByteArray l_数据;
而(udpSocket->hasPendingDatagrams())
{
l_data.resize(udpSocket->pendingDatagramSize());
QHostAddress l_addr;
udpSocket->readDatagram(l_data.data()、l_data.size()、l_addr);
如果(UDP_HELLO_STR==l_data.data())
{
QString l_srv_addr=l_addr.toString();
如果(!l_srv_addr.isEmpty())
{

qDebug()数据报的值在调试器中显示为什么?
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");
}
#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;
}
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();
            }
        }
    }
}