QUDPSocket Stops Reading Data
-
Hello All,
I am writing an application that uses either a QUDPSocket or a QTCPSocket to transfer data. I am still pretty new to QT but the QTCPSocket seems to work fine but having an issue with the QUDP. I am really just trying to simulate sending and processing data at the moment between two laptops. Each transmission are 100 packets each with 255 Bytes of data. It seems that the readPendingDatagrams() just seems to eventually stop around packet 32 and often times I miss the first 10 packets. I have struggled with this for several days now so looking for any help. I searched all the old threads and have not found an exact match so I must be doing something wrong like the data is coming in too fast or something. Below is the code.
@
void MyConnection::setUpServer(bool bUsingTCP)
{
//the server just connects and listens for incoming data
bConnectionIsTCP = bUsingTCP;
bIntialized = false;if( bUsingTCP == false) { qUDPsocket = new QUdpSocket(this); qUDPsocket->bind(2345, QUdpSocket::ShareAddress); connect(qUDPsocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); } else { //Using a TCP Configuration - Connect newConnection to readPendingDatagrams if(pTCPServer == NULL) { pTCPServer = new QTcpServer(this); connect(pTCPServer, SIGNAL(newConnection()), this, SLOT(readPendingDatagrams())); if(!pTCPServer->listen(QHostAddress::Any, 2345)) { qDebug() << "Could not start Server"; } else { //throw an error qDebug() << "Server Started"; } } else { qDebug() << " Question is server listneing = " << pTCPServer->isListening(); } }
}
void MyConnection::setUpClient(QString inIPAddress, qint16 inPortNumber, bool bUsingTCP)
{
//CLIENT IS FOR WRITING DATA
//client is set up to to send data to the ETTUS box.
//Need IP address of the box to send data to.
bIntialized = false;
strIPAddress = inIPAddress;
iPortNumber = inPortNumber;
bConnectionIsTCP = bUsingTCP;if( bUsingTCP == false) { qUDPsocket = new QUdpSocket(this); qUDPsocket->connectToHost(QHostAddress(strIPAddress), iPortNumber, QIODevice::ReadWrite); //1234 connect(qUDPsocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); } else { //Don't need to do anything because if TCP was set up the listening is in the //constructor. This is a reminder. }
}
qint64 MyConnection::WriteData(QByteArray inMessage)
{if( bConnectionIsTCP == false) { qDebug() << "Writing over UDP"; //Need the IP address and port number to use GS Box qint64 packetsWritten = qUDPsocket->writeDatagram(inMessage.data(), inMessage.size(), QHostAddress(strIPAddress), iPortNumber); qUDPsocket->bytesToWrite(); //Check bytes written qDebug() << "WriteData Returned " << QString::number(packetsWritten); return packetsWritten; } else { qDebug() << "Writing over TCP"; pTCPsocket = new QTcpSocket(this); pTCPsocket->connectToHost(QHostAddress("142.80.120.100"), 1234); pTCPsocket->waitForConnected(); qint64 bytes = pTCPsocket->write(inMessage); pTCPsocket->waitForBytesWritten(); pTCPsocket->close(); pTCPsocket = NULL; qDebug() << "Bytes written = " << bytes; return bytes; }
}
void MyConnection::readPendingDatagrams()
{
QHostAddress sender;
quint16 senderPort;if( bConnectionIsTCP == false ) { while (qUDPsocket->hasPendingDatagrams()) { qDebug() << "Pending DatagramSize = " << qUDPsocket->pendingDatagramSize(); inStreamFromSatellite.clear(); inStreamFromSatellite.resize(qUDPsocket->pendingDatagramSize()); //Pending DatagramSize is size of incoming data qint64 bytesRcved = qUDPsocket->readDatagram(inStreamFromSatellite.data(), inStreamFromSatellite.size(), &sender, &senderPort); setBytesReceived(bytesRcved); //qUDPsocket->flush(); //Process packets here. processPackets( inStreamFromSatellite ); //Call this after to the CommandID gets set emit rcvdBytes(); } } else { //Using TCP //qDebug () << "Using TCP configuration"; QTcpSocket *tempSocket = pTCPServer->nextPendingConnection(); if(tempSocket->state() == QAbstractSocket::ConnectedState) { //qDebug() << "Detected a connection"; while(tempSocket->waitForReadyRead(3000)) { while(tempSocket->bytesAvailable() > 0 ) { inStreamFromSatellite.append(tempSocket->readAll()); tempSocket->flush(); } } } else { //Error code qDebug() << "Socket coult not connect"; } setBytesReceived(inStreamFromSatellite.size()); //qDebug() << "Echo bytes back = " << inStreamFromSatellite.size(); //Process packets here. processPackets( inStreamFromSatellite ); //Call this after to the CommandID gets set emit rcvdBytes(); inStreamFromSatellite.clear(); }
}
@