【发布时间】:2016-09-03 19:42:40
【问题描述】:
我试图将接收到的数据报中的数据与字符串进行比较,当我运行程序时,我看到我收到“测试”,但 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");
}
}
}
}
我尝试了不同的比较方法,但到目前为止没有任何效果。
printf("%s", jag.c_str());当我使用 netcat 发送测试时也显示测试 有任何想法吗? 谢谢:)
【问题讨论】:
-
数据报的值在您的调试器中显示为什么?
标签: c++ string qt compare datagram