I'm having a problem with QUdpSockets.
Below is the code for my implementation of the socket initialization and datagram reading. Packets are read error-free in most use cases, but ALL packets of lengths 11-13 (inclusive) are getting interpreted incorrectly.
I have connected WireShark and verified that the packets are being transmitted correctly. In all instances of this corruption, the same data is received:
For packets 11-bytes: The first seven bytes are read correctly, and the final four bytes are (no matter what the transmitted content was), in decimal: 147 36 255 67
.
For packets 12-bytes: The first EIGHT bytes are read correctly, meaning the issue shifts: the final four bytes are 36 255 67 70
.
For packets 13-bytes: The first NINE bytes are read correctly, another shift: the final four bytes are 255 67 70 70
.
For packets 14-bytes: The data matches what is captured in Wireshark.
I am using Qt 4.7.3 for an embedded system, and using the 2014.4 Xilinx SDK's arm-xilinx-linux-gnueabi-gcc compiler to cross-compile for the embedded device.
Below is the relevant code, with a few of the variable declarations being done at a global scope. It should be easy enough to make sense of, but if any lines are confusing, please let me know.
Any assistance in understanding why my packets are getting corrupted (and only at that specific length) would be wonderful. Thank you in advance for your time.
//initialization method for all ethernet communication
//runs once at launch
void MainWindow::initEthernet()
{
connect(&primaryServerSocket, SIGNAL(readyRead()),
this, SLOT(handleEthernet()));
connect(&backupServerSocket, SIGNAL(readyRead()),
this, SLOT(handleEthernet()));
primaryServerSocket.bind(selfAddress, selfPort, QUdpSocket::ShareAddress);
backupServerSocket.bind(selfAddress, selfPort, QUdpSocket::ShareAddress);
}
void MainWindow::handleEthernet()
{
//determine where received data came from
QUdpSocket * sender = qobject_cast<QUdpSocket*>(this->sender());
//int keypadNumber = sender - keypadPorts;
QByteArray data;
data.resize(sender->pendingDatagramSize());
sender->readDatagram(data.data(), data.size());
//THOUGHT: Datagrams come as complete packages. No buffer appending is necessary.
//List of messages to send back
QList<QByteArray> responseToSend;
//print the raw message, as well as ascii and int values for each byte
QString error = data.data();
error.append("\n");
for(int i = 0; i < data.size(); i++)
{
error.append(data.at(i));
error.append(" ");
error.append(QString::number((int) data.at(i)));
error.append("\n");
}
showErrorMessage(error); //this prints a message to screen
}
After changing a myriad of variables (the device I was sending packets to, what language and structures I was using from Qt's to basic C to Java, the ethernet cable used, etc) I discovered the problem lies within my networking hardware on my machine.
After switching to another machine and sending out the same packets, they transmit error-free.