I’m porting a system that involves multicast sockets. I have them
compiling and running without producing any socket-level errors
(in other words, no socket functions are returning error codes), but
I’m not getting/sending data.
I have an example program that compiles and runs under LINUX (the
source platform I am porting from) and does what I expect: Two
threads, one sends data, the other reads data that is sent by the
other thread.
Here’s the example:
////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <malloc.h>
#include <sys/socket.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <sched.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
/---------------------------------------------------------------------/
/— panic - print message and die. —/
/---------------------------------------------------------------------/
void panic (char *msg, …)
{
va_list ap;
va_start (ap, msg);
vprintf (msg, ap);
perror (“errno”);
va_end (ap);
exit(1);
}
/---------------------------------------------------------------------/
/— Receiver - listens for and collects multicast packets.
—/
/---------------------------------------------------------------------/
int Receiver (int sd)
{
char buffer[1024];
struct sockaddr_in addr;
int len = sizeof (addr);
puts (“Receiver”);
do
{
if (recvfrom(sd, buffer, sizeof (buffer), 0, (struct sockaddr *)
&addr,(socklen_t *) & len) > 0)
printf (“Got %s:%d “%s”\n”, inet_ntoa (addr.sin_addr),ntohs
(addr.sin_port), buffer);
} while (strcmp (buffer, “bye\n”) != 0);
fprintf (stderr, “Receiver exiting\n”);
exit (0);
}
/---------------------------------------------------------------------/
/— main - creates multicast-capable socket and sends test
packets—/
/---------------------------------------------------------------------/
int main (int argc, char argv[])
{
int sd;
const int on = 1;
struct ip_mreq mreq;
struct sockaddr_in addr;
char buffer[1024];
int port=21000,port2=21000;
const char ip = “224.0.17.42”;
sd = socket (PF_INET, SOCK_DGRAM, 0);
if (sd < 0)
panic (“socket failed”);
bzero (&addr, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_port = htons (port);
addr.sin_addr.s_addr = INADDR_ANY;
if (bind (sd, (struct sockaddr *) &addr, sizeof (addr)) != 0)
panic (“bind failed”);
if (setsockopt (sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) !=
0)
panic (“Can’t reuse address/ports”);
if (inet_aton (ip, &mreq.imr_multiaddr) == 0)
panic (“address (%s) bad”, ip);
mreq.imr_interface.s_addr = INADDR_ANY;
if(setsockopt (sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof
(mreq)) !=0)
panic (“Join multicast failed”);
addr.sin_port = htons (port2);
if (inet_aton (ip, &addr.sin_addr) == 0)
panic (“inet_aton failed (%s)”, ip);
if (fork ())
Receiver (sd);
else
shutdown (sd, 0); /* close the input channel */
puts (“Server starting”);
do
{
sleep (1);
strcpy (buffer, “TESTING 1 2 3”);
puts (“Sending…”);
if(sendto(sd, buffer, strlen (buffer) + 1, 0, (struct sockaddr *)
&addr,sizeof (addr)) < 0)
perror (“sendto”);
} while (strcmp (buffer, “bye”) != 0);
sleep (6000);
close (sd);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
The example doesn’t work under QNX, either. I figure if I can
get the example working, I can get the system being ported to work.
Anyone have an idea what I’m doing wrong? I’m using a default
installation - nothing special done to tweak the OS. I only bring
this up because I read something about multiple protocol stacks being
available, and I’m using whatever stack was installed by default (and
I don’t know how I’d switch between stacks, either).
Many thanks!
-ed