File: rigtestmcastrx.c

package info (click to toggle)
hamlib 4.6.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,984 kB
  • sloc: ansic: 262,996; sh: 6,135; cpp: 1,578; perl: 876; makefile: 855; python: 148; awk: 58; xml: 26
file content (111 lines) | stat: -rw-r--r-- 2,458 bytes parent folder | download | duplicates (2)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
//#pragma does not work on mingw
//#pragma comment(lib, "Ws2_32.lib")
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#endif

#define MCAST_PORT 4532
#define MCAST_ADDR "224.0.0.1"
#define BUFFER_SIZE 4096

int main()
{
    int sock;
    struct sockaddr_in mcast_addr;
    char buffer[BUFFER_SIZE];
    int bytes_received;

#ifdef _WIN32
    WSADATA wsaData;

    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        fprintf(stderr, "WSAStartup failed: %d\n", WSAGetLastError());
        return 1;
    }

#endif

    if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
    {
        perror("socket() failed");
        exit(EXIT_FAILURE);
    }

    // Set the SO_REUSEADDR option to allow multiple processes to use the same address
    int optval = 1;

    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&optval,
                   sizeof(optval)) < 0)
    {
        //rig_debug(RIG_DEBUG_ERR, "%s: setsockopt: %s\n", __func__, strerror(errno));
        //return -RIG_EIO;
        return 1;
    }


    memset(&mcast_addr, 0, sizeof(mcast_addr));
    mcast_addr.sin_family = AF_INET;
    mcast_addr.sin_port = htons(MCAST_PORT);
    mcast_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    if (bind(sock, (struct sockaddr *)&mcast_addr, sizeof(mcast_addr)) < 0)
    {
        perror("bind() failed");
        exit(EXIT_FAILURE);
    }

    struct ip_mreq mreq;

    mreq.imr_multiaddr.s_addr = inet_addr(MCAST_ADDR);

    mreq.imr_interface.s_addr = htonl(INADDR_ANY);

    if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&mreq,
                   sizeof(mreq)) < 0)
    {
        perror("setsockopt() failed");
        exit(EXIT_FAILURE);
    }

    while (1)
    {
        bytes_received = recvfrom(sock, buffer, BUFFER_SIZE, 0, NULL, 0);

        if (bytes_received < 0)
        {
            perror("recvfrom() failed");
            break;
        }

        buffer[bytes_received] = '\0';
        printf("%s\n", buffer);
    }

    // Drop membership before closing the socket
    if (setsockopt(sock, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&mreq,
                   sizeof(mreq)) < 0)
    {
        perror("setsockopt() failed");
    }

    close(sock);
#ifdef _WIN32
    WSACleanup();
#endif

    return 0;
}