首页| 论坛| 消息

标题:难道我遇到了Qt的Bug?
作者:fox123
日期:2018-05-05 10:15
内容:

本人在做QUpSocket的网络通信程序。代码如下:

int MainWindow::initSocket(quint16 localPort)
{
if(udpSocket!=0){
udpSocket->abort();
delete udpSocket;
udpSocket = 0;
}
udpSocket = new QUdpSocket(this);
bool ret = udpSocket->bind(QHostAddress::AnyIPv4, localPort);
if(!ret){
QMessageBox::warning(this, "警告", QString("创建Socket失败!:%1").arg(udpSocket->errorString()));
return -1;
}
connect(udpSocket, &QUdpSocket::readyRead, this , &MainWindow::readPendingDatagrams);
return 0;
}
void MainWindow::readPendingDatagrams()
{
qDebug()hasPendingDatagrams())
{
QByteArray frame;
frame.resize(udpSocket->pendingDatagramSize());
QHostAddress address;
..


#1 [liudianwu 05-05 11:58]
如果qt封装的socket连这么几个字节的数据收发都搞不定有问题,那还要qt有啥用!
你可以把
QByteArray frame;
QHostAddress address;
quint16 port;
移到while外层试试!
#2 回 liudianwu 的帖子 [fox123 05-05 13:58]
liudianwu:如果qt封装的socket连这么几个字节的数据收发都搞不定有问题,那还要qt有啥用!
你可以把
QByteArray frame;      
QHostAddress address;
quint16 port;
....... (2018-05-05 11:58) 
感谢刘大神回复!我试了移到while外边也不行。
不过把while循环改成do while循环,数据收发确能正常进行。
qDebug()
#3 回 fox123 的帖子 [fox123 05-05 14:13]
fox123:感谢刘大神回复!我试了移到while外边也不行。
不过把while循环改成do while循环,数据收发确能正常进行。
....... (2018-05-05 13:58) 
返回false的时候frame.size=0, address.toString=""
true
3 "10.12.9.108"
2 "10.12.9.108"
false
0 ""
false
0 ""
false
0 ""
true
6 "10.12.9.108"
true
6 "10.12.9.108"
true
6 "10.12.9.108"
true
6 "10.12.9.108"
true
6 "10.12.9.108"
#4 [yuyu414 05-07 10:22]
void MainWindow::readPendingDatagrams()
{
qDebug()
#5 [yanwuyue 05-24 16:24]
我是这么打开UDP的
udp->bind(QHostAddress::Any, port, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
这行要注释掉,否则已经执行了一次了
qDebug()

回复 发表
主题 版块