Two clients using QUdpSocket bound in localhost at same port on same machine : only first gets readyRead()
-
Hello everyone,
I have a problem using "QUdpSocket":http://qt-project.org/doc/qt-5/qudpsocket.html.
I have a server (not written with Qt) sending data on localhost at port 3333.
I have a client Qt application which reads the data on localhost at port 3333.
My code works fine if I run only one instance of my client, but doesn't work for another.
All the apps (server + client n°1 + ... + client n°N) must run on the same machine.Here it is :
@
// in the constructor
_socket = new QUdpSocket(this);
qDebug()<<"UDP Socket stored at "<<&_socket<<"trying to bind";
if (_socket->bind(QHostAddress::LocalHost, 3333, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint))
{
qDebug()<<"UDP Socket bound. trying to connect signals to app slots";
connect(_socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorSocket()));
qDebug()<<"UDP Socket connected to slots";
}
else
{
qDebug()<<"error binding the Udp Socket :\n"<<_socket->errorString();
}
@@
void TuioEventsListener::errorSocket()
{
qDebug()<<"Udp Socket error :\n"<<_socket->errorString();
}
@@
void TuioEventsListener::readyRead()
{
qDebug()<<"data received";QByteArray buffer; buffer.resize(_socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; _socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort); // Here I'm proccessing data
}
@Output client n°1 :
@
UDP Socket stored at 0x1e58ad8 trying to bind
UDP Socket bound. trying to connect signals to app slots
UDP Socket connected to slots
data received
data received
....
@Output client n°2 :
@
UDP Socket stored at 0x1d78ad8 trying to bind
UDP Socket bound. trying to connect signals to app slots
UDP Socket connected to slots
@When I close the first instance of the client, the second one receives the readyRead() signal.
Like "clogwog":http://qt-project.org/mebmer/9158 in this "possible duplicate question":http://qt-project.org/forums/viewthread/7723/ , I think that the readDatagram() could be the cause. If I understood the problem, the first instance of the client has already read the datagram, which sets QUdpSocket::hasPendingDatagram to false ; so the second instance doesn't receives the readyRead() signal.
What should I do ? Any idea / suggestion is welcome.
Using Qt5.3.1 on Win7-64b with mingw32 compiler
-
This "post":http://stackoverflow.com/questions/2772324/can-two-different-udp-socket-in-a-system-bind-same-port may answer your questions.
-
I'm not sure what you're trying to do will work even without Qt. Only one network packet is created by your server when the message is sent and this will enter only one of the receiving sockets packet queues. Sharing the address only means the sockets have the same address/port, not that they will all receive all data. You need to look at broadcasting or multicasting or having whichever client receives the packet share it with the others via another IPC method (shared memory for example).
-
Thanks for your replies. I'm currently looking at "QSharedMemory":http://qt-project.org/doc/qt-5/qsharedmemory.html and the "Shared Memory Example":http://qt-project.org/doc/qt-5/qtcore-sharedmemory-example.html