summaryrefslogtreecommitdiff
path: root/lib/tests/udp-server.c
blob: 768fba0eb931b637fa33753f75bb8be3b3e1e1cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdlib.h>
#include <stdio.h>
#include "udp.h"

ssize_t
handle_data (const uint8_t *buf, ssize_t len)
{
  int i;

  printf ("# len: %ld\n", len);
  for (i = 0; i < len; i++)
    {
      printf ("%02x%s", buf[i], (i+1) % 8 ? " " : "   ");
      if ((i + 1) % 16 == 0)
	printf ("\n");
    }
  printf ("\n");
  return len;
}

int
main (int argc, char *argv[])
{
  int n, i;
  struct timeval tv;
  struct polldata *data;

#define TIMEOUT 1 		/* Seconds.  */

  tv.tv_sec = TIMEOUT;
  tv.tv_usec = 0;
  data = udp_server (argv[1], &tv, handle_data);

  for (i = 0, n = udp_poll (data); n == 0 && i < 3; n = udp_poll (data), i++)
    {
      fprintf (stderr, "waiting another %ld second%s\n",
	       tv.tv_sec, tv.tv_sec > 1 ? "s" : "");
    }

  if (data)
    {
      if (data->timeout)
	free (data->timeout);
      free (data);
    }
  return (n <= 0);
}