Skip to content

Instantly share code, notes, and snippets.

@ultrasilicon
Created January 25, 2019 22:12
Show Gist options
  • Save ultrasilicon/a58545f9fadc2c1698d9a507bc8c5309 to your computer and use it in GitHub Desktop.
Save ultrasilicon/a58545f9fadc2c1698d9a507bc8c5309 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <PUdpSocket.h>
#include <PTimer.h>
#include <PFunction.h>
using namespace std;
using namespace Parsley;
static Loop *loop;
static UdpSocket* server;
static Timer* timer;
static int counter = 0;
void send_cb(Timer *)
{
if(counter == 100)
{
timer->stop();
server->stop();
}
server->write("255.255.255.255", 66666, "hello: " + to_string(++ counter));
}
void receive_cb(string& data, IPAddress ip)
{
cout << ip.toIPString() << ": " << data << '\n';
}
int main()
{
loop = new Loop();
server = new UdpSocket("0.0.0.0", 66666, loop);
connect(&server->onReadyRead, &receive_cb);
server->start();
timer = new Timer(1, 5, loop);
connect(&timer->onTimedOut, &send_cb);
timer->start();
return loop->run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment