1 /* SDR Example
2 * Copyright (C) 2000 David Helder
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <string.h>
24
25 #include <glib.h>
26 #include <gnet.h>
27
28 #define MAXLINE 4096
29
30
31
32 int
main(int argc,char ** argv)33 main(int argc, char** argv)
34 {
35 gchar buf[MAXLINE + 1];
36 gint n;
37
38 struct sap_packet {
39 guint32 sap_header;
40 guint32 sap_src;
41 char sap_data[1];
42 } * sapptr;
43
44 GMcastSocket* ms;
45 GInetAddr* ia;
46 gint rv;
47
48 gnet_init ();
49
50 /* Create a multicast socket */
51 ms = gnet_mcast_socket_new_with_port (9875);
52 g_assert(ms != NULL);
53
54 /* Make sure it derives properly from GUdpSocket */
55 gnet_udp_socket_ref ((GUdpSocket *) ms);
56 gnet_udp_socket_unref ((GUdpSocket *) ms);
57
58 /* Let's check this as well then, just for fun */
59 gnet_mcast_socket_ref (ms);
60 gnet_mcast_socket_unref (ms);
61
62 /* Get address of our group */
63 ia = gnet_inetaddr_new ("sap.mcast.net", 9875);
64 g_assert(ia != NULL);
65
66 /* Join the group */
67 rv = gnet_mcast_socket_join_group (ms, ia);
68 g_assert(rv == 0);
69
70 printf("Joined %s:%d...\n", gnet_inetaddr_get_name(ia), gnet_inetaddr_get_port(ia));
71
72 /* Print some information about our multicast socket */
73 g_print ("My addresss: %s\n", gnet_inetaddr_get_host_name());
74 g_print ("Loopback: %d\n", gnet_mcast_socket_is_loopback (ms));
75 g_print ("Mcast TTL: %d\n", gnet_mcast_socket_get_ttl(ms));
76
77 /* Turn off loopback */
78 rv = gnet_mcast_socket_set_loopback (ms, 0);
79 g_assert (rv == 0);
80
81 for(;;)
82 {
83 g_print ("Waiting for packet...\n");
84
85 /* Receive packet */
86 n = gnet_mcast_socket_receive (ms, buf, MAXLINE, NULL);
87 buf[n] = 0;
88
89 sapptr = (struct sap_packet*) buf;
90
91 n -= 2 * sizeof(guint32);
92 if (n <= 0)
93 {
94 g_warning ("SAP packet too small");
95 continue;
96 }
97
98 g_print ("%s\n", sapptr->sap_data);
99
100 }
101 }
102