PDA

View Full Version : QUdpSocket hasPendingDatagrams()



mbjerkne
3rd April 2006, 23:40
I am setting up a QUdpSocket and connecting the readyRead() signal to my readDatagrams() method, however when I get into the method and check the socket with hasPendingDatagrams() it never enters my loop, i.e:



void Listener::readDatagrams()
{
while ( m_aSocket->hasPendingDatagrams() )
{
// Never gets here
}
}


however, if i skip that loop and just read a datagram from the socket I get my data:



QByteArray datagram;
datagram.resize(m_aSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;

m_aSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

if ( datagram.size() > 0 )
{
// Gets here without loop
}


Can anyone help out?

vratojr
4th April 2006, 17:12
Please,post how and when you call the readDatagram function.

mbjerkne
4th April 2006, 19:07
ControlsListener::ControlsListener(int nPort)
{
m_nPort = nPort;
m_aSocket = new QUdpSocket();
if ( !m_aSocket->bind(m_nPort) )
{
std::cout << "Could not bind udp socket" << std::endl;
}
else
{
std::cout << "Successfully created udp socket on port " << nPort << std::endl;
}
connect(m_aSocket,SIGNAL(readyRead()),
this, SLOT(readPendingDatagrams()));
}

ControlsListener::~ControlsListener()
{
delete m_aSocket;
}

void ControlsListener::readPendingDatagrams()
{

//std::cout << "readPendingDatagrams" << std::endl;
//while ( m_aSocket->hasPendingDatagrams() )
// {
QByteArray datagram;
datagram.resize(m_aSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;

m_aSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

if ( datagram.size() > 0 )
{
double * dData = (double*)datagram.data();

ByteSwap5(*dData);

double dValue = *dData;

QString sValue = QString("%1").arg(dValue);
emit messageReceived(sValue);
}
//}
}


This is the code for the whole listener. I have the while loop commented out right now, but if I uncomment the loop it never enters it. If I uncomment the std::cout << "readPendingDatagrams" << std::end; it gets printed a whole lot, but nothing ever enters the loop.

jacek
4th April 2006, 19:14
If I uncomment the std::cout << "readPendingDatagrams" << std::end; it gets printed a whole lot, but nothing ever enters the loop.
Then maybe it's one of these bugs:
http://www.trolltech.com/developer/tasktracker.html?method=entry&id=86944
http://www.trolltech.com/developer/tasktracker.html?method=entry&id=104325