/*
* Run with a port number. Receives UDP messages on that port and
* prints them.
*/
/*
* This file is part of the Cleansocks library. It is released under the
* Lesser GNU GPL. See the accompanying files COPYING and COPYING.LESSER.
*/
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include "cleansocks.h"
#include "cleanip.h"
using namespace std;
using namespace cleansocks;
void doit(const IPport &p)
{
UDPsocket rcver;
bind(rcver, IPendpoint(IPaddress::any(), p));
int tot = 0;
while(true) {
cout << "Waiting...";
cout.flush();
char buf[1000];
IPendpoint from;
int amt = recvfrom(rcver, buf, sizeof buf, from);
cout << amt << " bytes from " << from << endl;
cout.write(buf, amt);
cout << endl;
tot += amt;
if(strncmp(buf, "STOP", 4) == 0) break;
}
close(rcver);
}
int main(int argc, char **argv)
{
if(argc != 2) {
cerr << "Provide exactly one port number." << endl;
exit(2);
}
IPport p = atoi(argv[1]);
try {
doit(p);
} catch(socket_db_error e) {
cout << "Socket db error: " << e.what() << endl;
}
catch(socket_error e) {
cout << "Socket error: " << e.what() << endl;
}
catch(runtime_error e) {
cout << "Runtime error: " << e.what() << endl;
}
}