summaryrefslogtreecommitdiff
path: root/udp/udp-client.c
blob: 23cee7b3894fbb2ebb2e153239dda6dc1fe46e38 (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

#include <string.h>
#include <stdio.h>

#include "udp-bsd.h"

gint
main (void)
{
  UDPSocketManager man;
  UDPSocket sock;
  struct sockaddr_in sin;

  udp_bsd_socket_manager_init (&man);

  if (!udp_socket_manager_alloc_socket (&man, &sock, NULL))
    g_assert_not_reached ();

  if (inet_pton (AF_INET, "127.0.0.1", &(sin.sin_addr)) < 0)
    g_assert_not_reached ();

  sin.sin_family = AF_INET;
  sin.sin_port = htons (9999);

  for (;;)
    {
      gchar buf[1024];
      guint length;

      if (fgets (buf, sizeof (buf), stdin) == NULL)
        break;

      udp_socket_send (&sock, &sin, strlen (buf), buf);
      length = udp_socket_recv (&sock, NULL, sizeof (buf), buf);
      g_print (buf);
    }

  udp_socket_close (&sock);
  udp_socket_manager_close (&man);
  return 0;
}