#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#include <rtdm/ipc.h>
pthread_t svtid, cltid;
#define IDDP_SVPORT 12
#define IDDP_CLPORT 13
static const char *msg[] = {
"Surfing With The Alien",
"Lords of Karma",
"Banana Mango",
"Psycho Monkey",
"Luminous Flesh Giants",
"Moroccan Sunset",
"Satch Boogie",
"Flying In A Blue Dream",
"Ride",
"Summer Song",
"Speed Of Light",
"Crystal Planet",
"Raspberry Jam Delta-V",
"Champagne?",
"Clouds Race Across The Sky",
"Engines Of Creation"
};
static void fail(const char *reason)
{
perror(reason);
exit(EXIT_FAILURE);
}
static void *server(void *arg)
{
socklen_t addrlen;
char buf[128];
size_t poolsz;
int ret, s;
if (s < 0)
fail("socket");
poolsz = 32768;
&poolsz, sizeof(poolsz));
if (ret)
fail("setsockopt");
ret = bind(s, (struct sockaddr *)&saddr, sizeof(saddr));
if (ret)
fail("bind");
for (;;) {
addrlen = sizeof(saddr);
ret = recvfrom(s, buf, sizeof(buf), 0,
(struct sockaddr *)&claddr, &addrlen);
if (ret < 0) {
close(s);
fail("recvfrom");
}
printf("%s: received %d bytes, \"%.*s\" from port %d\n",
__FUNCTION__, ret, ret, buf, claddr.sipc_port);
}
return NULL;
}
static void *client(void *arg)
{
int ret, s, n = 0, len;
struct timespec ts;
if (s < 0)
fail("socket");
ret = bind(s, (struct sockaddr *)&clsaddr, sizeof(clsaddr));
if (ret)
fail("bind");
for (;;) {
len = strlen(msg[n]);
ret = sendto(s, msg[n], len, 0,
(struct sockaddr *)&svsaddr, sizeof(svsaddr));
if (ret < 0) {
close(s);
fail("sendto");
}
printf("%s: sent %d bytes, \"%.*s\"\n",
__FUNCTION__, ret, ret, msg[n]);
n = (n + 1) % (sizeof(msg) / sizeof(msg[0]));
ts.tv_sec = 0;
ts.tv_nsec = 500000000;
}
return NULL;
}
int main(int argc, char **argv)
{
struct sched_param svparam = {.sched_priority = 71 };
struct sched_param clparam = {.sched_priority = 70 };
pthread_attr_t svattr, clattr;
sigset_t set;
int sig;
sigemptyset(&set);
sigaddset(&set, SIGINT);
sigaddset(&set, SIGTERM);
sigaddset(&set, SIGHUP);
pthread_sigmask(SIG_BLOCK, &set, NULL);
pthread_attr_init(&svattr);
pthread_attr_setdetachstate(&svattr, PTHREAD_CREATE_JOINABLE);
pthread_attr_setinheritsched(&svattr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&svattr, SCHED_FIFO);
pthread_attr_setschedparam(&svattr, &svparam);
if (errno)
fail("pthread_create");
pthread_attr_init(&clattr);
pthread_attr_setdetachstate(&clattr, PTHREAD_CREATE_JOINABLE);
pthread_attr_setinheritsched(&clattr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&clattr, SCHED_FIFO);
pthread_attr_setschedparam(&clattr, &clparam);
if (errno)
fail("pthread_create");
sigwait(&set, &sig);
pthread_cancel(svtid);
pthread_cancel(cltid);
return 0;
}