After a first topic that help me send correctly data to someone using UDP Protocol, I have a problem in the reception of these data. This problem is very strange and only happen the first time I launch the function for sending data. The first time, the server only receive the first frame. But after, if I re use the function everything is ok.
So here's the code that send data through UDP Protocol (my data is a structure) :
void MyUDP::sendUDP()
{
//Structure to send
typedef struct MyStructTag
{
int test1;
bool test2;
char test3;
} MyStruct;
MyStruct envoie;
envoie.test1 = 1;
envoie.test2 = true;
envoie.test3 = 97;
// Sends the datagram datagram
// to the host address and at port.
// qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
// const QHostAddress & host, quint16 port)
QByteArray buf;
QDataStream s(&buf, QIODevice::WriteOnly);
// The encoding is big endian by default, on all systems. You
// can change it if you wish.
if (false) s.setByteOrder(QDataStream::LittleEndian);
s << (qint32)envoie.test1 << (quint8)envoie.test2 << (qint8)envoie.test3;
//I'm sending 5 frames
socket->writeDatagram(buf, QHostAddress("10.100.14.79"), 4000);
socket->writeDatagram(buf, QHostAddress("10.100.14.79"), 4000);
socket->writeDatagram(buf, QHostAddress("10.100.14.79"), 4000);
socket->writeDatagram(buf, QHostAddress("10.100.14.79"), 4000);
socket->writeDatagram(buf, QHostAddress("10.100.14.79"), 4000);
}
And here's the function that allow me te receive these data :
void MyUDP::readyRead()
{
QHostAddress sender;
quint16 senderPort;
// qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize,
// QHostAddress * address = 0, quint16 * port = 0)
// Receives a datagram no larger than maxSize bytes and stores it in data.
// The sender's host address and port is stored in *address and *port
// (unless the pointers are 0).
typedef struct MyStructTag
{
int test1;
bool test2;
char test3;
} MyStruct;
MyStruct recois;
socket->readDatagram((char*)&recois, sizeof (recois), &sender, &senderPort);
qDebug() << "Message from: " << sender.toString();
qDebug() << "Message port: " << senderPort;
qDebug() << "Message: " << recois.test3;
}
Why did I only receive 1 frame the first time I launch sendUDP ?