Search code examples
androidqtsocketspacketdatagram

In Android how to know when a DatagramPacket has been received?


Assume that a transmitter is sending DatagramPacket at random intervals. Is there a way to know when a packet is received at the receiver? In C++ and using QT it is possible to connect the socket to a readyRead() signal for example:

connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));

Then everytime a packet is arraived at receiver a SIGNAL is emitted and we jump to processPendingDatagrams() routine.

Is it possible to do something similar in Android? If not what is the best way to read received packets as soon as they arrive?


Solution

  • In Android you must implement your own thread witch waits until there is a datagram packet aviable .You can send a message to the main thread using a handler.

    A good tutorial on how to do this you can find here.