My code monitors a datagram socket (implemented in the Poco framework), and I'd like to get notified if there were no messages received after a certain time. I have an infinite loop to monitor the socket, but the loop stops at receiveFrom()
function, if no messages were received.
Here is the code:
#include <iostream>
#include <string>
//Poco includes
#include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/SocketAddress.h"
int port = 5000;
Poco::Net::SocketAddress sa(Poco::Net::IPAddress(), port);
Poco::Net::DatagramSocket dgs(sa, true);
char buffer[1024];
for (;;)
{
int n = dgs.receiveFrom(buffer, sizeof(buffer)-1, sender);
buffer[n] = '\0';
std::string line = buffer;
//Do something with the message
};
You should set a timeout on your Poco socket.
Poco::Timespan wait_time( 10000 ); // microseconds
// from header file Poco/Net/Socket.h
sa.getReceiveTimeout( wait_time )
Then check the number of bytes returned by receiveFrom(..)
is > 0 or check for a timeout exception. Use this opportunity to do your other work.